admin管理员组

文章数量:1300046

This JavaScript code uses POST method to send data from form to PHP where PHP checks in database if data is true. But I don't know how to send response from PHP back to JS that fetching was successful. Can someone explain?

JS:

$('#submit-btn').on('click', function() {

  var dataString = 'username=' + document.getElementById('username').value + '&password=' + document.getElementById('password').value + '&rememberMe=' + document.getElementById('rememberMe').value;

  $.ajax({
    type: "POST",
    url: "ajaxsubmit.php",
    data: dataString,
    cache: false,
    success: function(){
      //check if what response is   
    } 
  });

ajaxsubmit.php:

<?php
session_start();

//connect

$username =$_POST['username'];
$password =$_POST['password'];  

$sql = "SELECT * FROM user WHERE email ='$username' OR username ='$username' AND password = '$password'";
$result = mysqli_query($conn, $sql);

if(!$row = mysqli_fetch_assoc($result)){     
    //response error
} else{
     //response success
}

?>

This JavaScript code uses POST method to send data from form to PHP where PHP checks in database if data is true. But I don't know how to send response from PHP back to JS that fetching was successful. Can someone explain?

JS:

$('#submit-btn').on('click', function() {

  var dataString = 'username=' + document.getElementById('username').value + '&password=' + document.getElementById('password').value + '&rememberMe=' + document.getElementById('rememberMe').value;

  $.ajax({
    type: "POST",
    url: "ajaxsubmit.php",
    data: dataString,
    cache: false,
    success: function(){
      //check if what response is   
    } 
  });

ajaxsubmit.php:

<?php
session_start();

//connect

$username =$_POST['username'];
$password =$_POST['password'];  

$sql = "SELECT * FROM user WHERE email ='$username' OR username ='$username' AND password = '$password'";
$result = mysqli_query($conn, $sql);

if(!$row = mysqli_fetch_assoc($result)){     
    //response error
} else{
     //response success
}

?>
Share Improve this question edited Aug 20, 2017 at 13:08 Milan Chheda 8,2493 gold badges22 silver badges35 bronze badges asked May 12, 2017 at 18:43 user7698730user7698730 3
  • 3 Little Bobby says your script is at risk for SQL Injection Attacks. Learn about prepared statements for MySQLi. Even escaping the string is not safe! – Jay Blanchard Commented May 12, 2017 at 18:46
  • 2 Never store plain text passwords! Please use PHP's built-in functions to handle password security. If you're using a PHP version less than 5.5 you can use the password_hash() patibility pack. It is not necessary to escape passwords or use any other cleansing mechanism on them before hashing. Doing so changes the password and causes unnecessary additional coding. – Jay Blanchard Commented May 12, 2017 at 18:47
  • You have to echo something in your PHP to get information returned to AJAX. – Jay Blanchard Commented May 12, 2017 at 18:47
Add a ment  | 

3 Answers 3

Reset to default 4

Whatever you echo out in the php will be sent back to the ajax.

if(!$row = mysqli_fetch_assoc($result)){
    echo 0;
}
 else{
     echo 1;
}

success: function(response){
    //check if what response is
    console.log( response );
} 

You have to echo something in your PHP to get something returned:

if(!$row = mysqli_fetch_assoc($result)){
    //response error
    echo 'there is a problem';
} else {    
     //response success
     echo 'yippee!';
}

Then you can log the return as follows:

$('#submit-btn').on('click', function() {

    var dataString = 'username=' + document.getElementById('username').value + '&password=' + document.getElementById('password').value + '&rememberMe=' + document.getElementById('rememberMe').value;
    $.ajax({
        type: "POST",
        url: "ajaxsubmit.php",
        data: dataString,
        cache: false,
        success: function(data){ // 'data' is the variable holding the return from PHP's echo
        //check if what response is   
           console.log(data);
     } 
});

Make sure to watch the AJAX request / response in the browser's developer tools. Make sure you included the jQuery library in the project. The console will reveal errors. AJAX requires a web server.

WARNING Little Bobby says your script is at risk for SQL Injection Attacks. Learn about prepared statements for MySQLi. Even escaping the string is not safe!

DANGER Never store plain text passwords! Please use PHP's built-in functions to handle password security. If you're using a PHP version less than 5.5 you can use the password_hash() patibility pack. It is not necessary to escape passwords or use any other cleansing mechanism on them before hashing. Doing so changes the password and causes unnecessary additional coding.

You can exit method with response parameters. like:

ajaxsubmit.php

if(!$row = mysqli_fetch_assoc($result)){     
    exit('error');  //exit with response 'error'
} else{
     exit('success'); //exit with response 'success'
}

JS

$('#submit-btn').on('click', function() {

  var dataString = 'username=' + document.getElementById('username').value + '&password=' + document.getElementById('password').value + '&rememberMe=' + document.getElementById('rememberMe').value;

  $.ajax({
    type: "POST",
    url: "ajaxsubmit.php",
    data: dataString,
    cache: false,
    success: function(response){
      //check if what response is   
      console.log(response);
    } 
  });

本文标签: javascriptHow to send response from PHP to AjaxStack Overflow