0

Here's a link i saw but still confused over the answers provided for them : jQuery Ajax returns the whole page

My situation is something similar.

I have a index.php that calls for a popup login form, that tries to submit this login information to a ajax call to another php called login_check.php.

This is my index.php :

var dataString = 'email='+ email.val() + '&password=' + password.val();

$.ajax({
type: "POST",
url: "login_check.php",
data: dataString,
cache: false,
error: function (request, status, error) {
    alert("An error occured while trying to complete your request: " + error);
},
success: function(data)
{
    alert(data);

}
}); 

This is what is in my login_check.php :

session_start();

//input from login form 
$myemail = htmlspecialchars(trim($_POST['email']));
$mypassword = htmlspecialchars(trim($_POST['password']));

//selecting from database to check user account
$sql="SELECT * FROM user WHERE email='$myemail' and password='$mypassword'";
$result=mysql_query($sql);
$row=mysql_fetch_array($result);
$count=mysql_num_rows($result);

//If result matched $myemail and $mypassword, table row must be 1 row 
if($count==1)
{   
    echo 'true';
    session_register("myemail");
    $_SESSION['login_user']=$myemail;
}   
else if($count==0)
{
    echo 'false';
}

And the output of the alert box is the full html of the page i am currently in, i.e. index.php.

And i have no idea whyy. ):

Community
  • 1
  • 1
cokeby190
  • 609
  • 7
  • 14
  • Try changing the Ajax call type to `GET` as you are sending `GET` parameters. You'll also need to change your PHP code to use the `$_GET` variable. – Lix Mar 17 '12 at 13:59
  • Please try narrowing the problem down to the client side or server side. For client side you can inspect the outgoing ajax calls with the chrome developers tools. And for server side you could send yourself an email containing the entire `$_REQUEST` array... – Lix Mar 17 '12 at 14:06
  • jquery looks ok, so I'm guessing the issue is that the php page returns an entirely rendered response. – Davin Tryon Mar 17 '12 at 14:31
  • i just realised if i only put in my login_check.php it returns nothing to my ajax. ): no idea whyyyyy. – cokeby190 Mar 17 '12 at 15:18

3 Answers3

0

Same happen here, I figured it out it has a conflict with other plugin, deactivate your plugin and it should works.

Hope it helps on other looking for this solution.

Ryan S
  • 6,076
  • 1
  • 20
  • 14
0

You might also want to try this syntax of making an Ajax POST request.

$.post('login_check.php',{'email':email.val(),'password':password.val()},function(response){
  alert(response);
},"json");
Lix
  • 47,311
  • 12
  • 103
  • 131
0

No clue whether this solves your problem, but it's always a good idea to encode the data:

var dataString = encodeURIComponent('email='+ email.val() + '&password=' + password.val());

Maybe this solves your problem, too...

Simon
  • 371
  • 2
  • 11
  • hmm. as in i have no issue parsing the value over, i have checked that my login is actually successful. but i need to throw the user to another page depending on the login_check.php to see if login is successful, and show an error msg. so i guess its not the encoding part. But thanks anyway! (: – cokeby190 Mar 17 '12 at 14:21