简体   繁体   中英

Callback from client-side not working

I have html files clientside (part of a cross-platform app) that go out to the php file and need to get a varible or string back, it is correctly posting the data but not recieving it back.

This is triggered on submit (client side .html):

 var $form = $( this ),
         $submit = $form.find( 'button[type="submit"]' ),
         username_value = $form.find( 'input[name="username"]' ).val(),
         password_value = $form.find( 'input[name="password"]' ).val(),

         url =  $form.attr('action');

     /* Send the data using post */

      $.post(url, {username: username_value, password: password_value},function (data) {

        alert(data);
    });

this is the php file:

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

$DBusername = 'root';
$DBpassword = '';
$project = mysql_connect('localhost',$DBusername, $DBpassword);

mysql_select_db('PickMeUpDatabase', $project);

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

$count=mysql_num_rows($result);


if($count==1){
echo 'console.log("hi");'; 
        $message = "saved successfully";
        echo $message;
}
else
{
    $sql = "Insert into users Values('me','myself','I','".$username."','Irene')";
mysql_query($sql);
        echo "Helooo";
}
    ?>

I know the php file doesnt really make sense at this point I changed a lot for debugging, but the mysql query in the "else" statement is being passed and added to the database but the echo right below it isnt being returned to where it will be alerted in the html. In fact I can change the alert in the html to alert anything and it wont happen.

There could be a lot of reasons why this is happening.

Do you have error reporting turned on? If not, put this after the php delimiter:

ini_set( "display_errors", true );

Also, is the data being sent cross domain? Put this after the above line,

 header('Access-Control-Allow-Origin: *');

Now, It might be POSTing, but that doesn't necessarily mean the data is usable on the server end. Whenver I use the post() function in JQuery, I prefer strings for the data parameters. It looks like a few variables are undefined, too.

So I would rewrite it this way:

 var $form = $( this ),
         $submit = $form.find( 'button[type="submit"]' ),
         username_value = $form.find( 'input[name="username"]' ).val(),
         password_value = $form.find( 'input[name="password"]' ).val(),

          /* Turn the variables into strings- for simplicity! */
         url =  "http://localhost/location/of/php_file.php";


      var data='username='+username_value+'&password='+password_value;
      $.post(url, data, function (data) {

        alert(data);
    });

That should clean it up a little more.

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