I have script from login page which through ajax sends data into php, and php returns if successful or not, I just wonder how can I change my scripts so it would send back also 2 variables and receive it in my login js script so I can create cookies? here are my js and php scripts:
js:
$(document).on('pageinit',function(){
$("#login").click(function(){
username=$("#usr").val();
password=$("#psw").val();
$.ajax({
type: "POST",
url: "http://imes.***********/php/login_check.php",
data: "name="+username+"&pwd="+password,
success: function(html){
//in case of success
if(html=='true')
{
$("#login_message").html("Logged in, congratulation.");
$.mobile.changePage("http://imes.***********/userpanel.php");
}
//in case of error
else
{
$("#login_message").html("Wrong username or password");
}
},
beforeSend: function() { $.mobile.showPageLoadingMsg(); }, //Show spinner
plete: function() { $.mobile.hidePageLoadingMsg() } //Hide spinner
});
return false;
});
php:
<?php
session_start();
$username = $_POST['name'];
$password = $_POST['pwd'];
include('mysql_connection.php');
mysql_select_db("jzperson_imesUsers", $con);
$res1 = mysql_query(
"SELECT * FROM temp_login WHERE username='$username' AND password='$password'"
);
$num_row = mysql_num_rows($res1);
if ($num_row == 1) {
echo 'true';
}
else {
echo 'false';
}
?>
I have script from login page which through ajax sends data into php, and php returns if successful or not, I just wonder how can I change my scripts so it would send back also 2 variables and receive it in my login js script so I can create cookies? here are my js and php scripts:
js:
$(document).on('pageinit',function(){
$("#login").click(function(){
username=$("#usr").val();
password=$("#psw").val();
$.ajax({
type: "POST",
url: "http://imes.***********./php/login_check.php",
data: "name="+username+"&pwd="+password,
success: function(html){
//in case of success
if(html=='true')
{
$("#login_message").html("Logged in, congratulation.");
$.mobile.changePage("http://imes.***********./userpanel.php");
}
//in case of error
else
{
$("#login_message").html("Wrong username or password");
}
},
beforeSend: function() { $.mobile.showPageLoadingMsg(); }, //Show spinner
plete: function() { $.mobile.hidePageLoadingMsg() } //Hide spinner
});
return false;
});
php:
<?php
session_start();
$username = $_POST['name'];
$password = $_POST['pwd'];
include('mysql_connection.php');
mysql_select_db("jzperson_imesUsers", $con);
$res1 = mysql_query(
"SELECT * FROM temp_login WHERE username='$username' AND password='$password'"
);
$num_row = mysql_num_rows($res1);
if ($num_row == 1) {
echo 'true';
}
else {
echo 'false';
}
?>
Share
Improve this question
edited Jan 23, 2013 at 14:52
Maxim Shoustin
77.9k29 gold badges210 silver badges228 bronze badges
asked Jan 23, 2013 at 14:28
Jakub ZakJakub Zak
1,2326 gold badges33 silver badges53 bronze badges
3
- Make the cookies in PHP. – Marcus Recck Commented Jan 23, 2013 at 14:31
- 2 This is not related to your question, but you have a serious SQL injection vulnerability in your PHP script. You should escape your input parameters before building our SQL statement or even better use prepared statements. – helmbert Commented Jan 23, 2013 at 14:33
- oh yeah I know about all those security issues, this is so far just to find out how to work with certain things. but thx... – Jakub Zak Commented Jan 23, 2013 at 14:36
3 Answers
Reset to default 2Try to use Json as response:
PHP side
...
$json = new Services_JSON();
echo $json->encode("{a:1,b:2}");
JS
...
success:function(data){
if(data)
{
console.log(data.a);
console.log(data.b);
}
....
You can encode any array in PHP and pass it as ajax callback.
Json class you can get there
You can use JSON to return values to your JS script. In your PHP code return something like:
{
"result": true,
"your_variable": "variable_value"
}
You can use json_encode function. And then in JS parse it with jQuery.parseJSON()
You can use a JSON formatted response to return values from PHP to JS (look at json_encode() PHP function : http://php/manual/en/function.json-encode.php ).
Or just set the value in the cookie from PHP ( http://php/manual/en/function.setcookie.php )