简体   繁体   中英

Null response in PHP AJAX call

I have a working login script on another site that loads a PHP script via AJAX. I can't seem to figure out why I am getting a null response when it should either be simply false or an array of values.

When I manually put the values into the php script (hard coded) it working. I can see in the console that the variables are being sent from the form. However, nothing is being returned. Can anyone spot what I am missing?

Thanks for any help or ideas.

Login PHP

<?php 
$login = $_POST['login'];
$password = $_POST['password'];

require_once("DB.php");
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!"); 
mysql_select_db($db) or die ("Unable to select database!"); 

$query = "SELECT * FROM members WHERE login='$login' AND passwd='".md5($_POST['password'])."'";
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); 
if (mysql_num_rows($result) > 0) { 
$output = "true";
     while(list($member_id, $firstname, $lastname, $login, $passwd, $City, $State, $bday, $approved, $organization, $school, $trainingdate, $Subscriber, $wscoordinator, $Position, $subdate, $enddate, $notice, $book, $trial)  = mysql_fetch_row($result)) { 
echo json_encode(array('memberid' => $member_id, 'firstname' => $firstname, lastname => $lastname, approved => $approved, subscriber => $Subscriber, position => $Position, school => $school, login => $login, book =>$book, ws => $wscoordinator, trial => $trial, enddate => $enddate));
}
} else {
$output = "false";
echo json_encode($output);
}


?>

AJAX, using jQuery.

$('#loginForm #loginButton').click(function(){
        var $form = $('#loginForm'),
    $inputs = $form.find("input"),
    serializedData = $form.serialize();
    var login = $('#login').text('');
    var password = $('#password').text('');
    $.ajax({
      type: 'POST',
      url: 'Scripts/php/login.php',
      data: serializedData,
      success: function(response){
        console.log("Response: "+response);
        if(response != "false") 
        {
        //window.location.href='toolstart.html';
        $.cookie('lastname', response.lastname, { expires: 365, path: '/' });
        } else {
        alert("Your email and password combination did not match.");
        }

        },      
  dataType: 'json'
});    

(Yes, I know I need to move from MD5; just haven't gotten there yet.)

Simply replace .text(); to .val() as text() gives blank result for text input types. val() will give actual value. Secondly use var_dump($_POST); die; var_dump($_POST); die; . Ahh...check whether your function:

$('#loginForm #loginButton').click(function(){
        var $form = $('#loginForm'),
    $inputs = $form.find("input"),
    serializedData = $form.serialize();
    var login = $('#login').text('');
    var password = $('#password').text('');
    $.ajax({
      type: 'POST',
      url: 'Scripts/php/login.php',
      data: serializedData,
      success: function(response){
        console.log("Response: "+response);
        if(response != "false") 
        {
        //window.location.href='toolstart.html';
        $.cookie('lastname', response.lastname, { expires: 365, path: '/' });
        } else {
        alert("Your email and password combination did not match.");
        }

        },      
  dataType: 'json'
});**});**

is missing some curly braces, as I found. Do respond with your var_dump($_POST);die; message.

After several days, I did a little more research. It turns out, the server my client is using, only supports up to PHP 5.1 and json_encode is only supported in 5.1. Found this awesome script to make it work. http://www.boutell.com/scripts/jsonwrapper.html

Thanks for all the help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM