PDA

View Full Version : Sencha touch php/mysql login



iaghidro
29 Aug 2011, 10:25 AM
Hi, I'm pretty new to sencha touch, and I'm having trouble connecting my mysql database to verify a login. Can someone please direct me on the right path on how I can accomplish this?
thanks in advance

Mthor
30 Aug 2011, 8:33 AM
php page: this is what the ajax request below will call to do, if you have a successfull login then you can call another function to retrieve certain data or do what ever you want.



<?php
ob_start();

// Table name
$tbl_name = "users";

include("../config.php");

// Define $myusername and $mypassword
$myusername = $_POST['username'];
$mypassword = $_POST['password'];

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);

$mypassword = md5($mypassword); // this is an encrypting the password

$sql = "SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result = mysql_query($sql);

// Mysql_num_row is counting table row
$count = mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row

if ($count == 1) {
echo "success";
} else {
echo "failed";
}

ob_end_flush();
?>




ajax request:



// gets the values from the form
var values = this.getValues();

Ext.Ajax.request({
url: 'login.php',
method: 'post',
params: values,
scope: this,
failure : function(response){
console.log(response.responseText);
Ext.Msg.alert('Login failed', 'Try Again', Ext.emptyFn);
},
success: function(response, opts) {
this.myMask.show();
var responseText = response.responseText;
var values = this.getValues();
if (responseText === 'success'){
localStorage.setItem("loginstatus", true);
localStorage.setItem("username", values.username);

// then i call another function to do all my loading of forms and crap for my web app.
this.submitLoginCache();
}
}
});



good luck, hope that helped.

mmjrules
29 Mar 2012, 10:07 AM
Hi!

I'm trying to do the same. Can you please tell me what config.php does? Does is hold info about connecting to the db?

Thanks!



<?php
// Table name
$tbl_name = "users";

include("../config.php");

Ntenis
11 Nov 2013, 2:39 AM
Hi,
the config file in this case , helps to connect your database(opening a connection to your database actually), please have a look at the script below.


config.php

<?php
$con=mysqli_connect("example.com","peter","abc123","my_db");

// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

mysqli_close($con);
?>