-1

I have written a php script:

cch.php:

$stmtcheck = $mysqli->prepare("SELECT id,email,location FROM members WHERE email=? AND unlock_code=?");
$stmtcheck->bind_param("si", $_SESSION['unlockemail'], $_POST['code']);
$stmtcheck->execute();
$stmtcheck->bind_result($id,$email,$location);
$stmtcheck->fetch();
         $array=array($id,$email,$location);    
        json_encode($array);
$stmtcheck->close();

And jquery for submitting form is

recover.php:

$("#formUnlock").submit(function(e)
{
   e.preventDefault();

   $.ajax(
   {
        url: '../scripts/cch.php',
        method: 'POST',
        data: $(#formUnlock).serialize(),
        dataType: 'JSON',
        success: function()
        {

        }
   }); 
});

The script is returning more than one variable and the returned data should be in JSON format.

How do I read the data in jQuery?

3
  • 1
    you need to echo the json on the PHP side, the response is returned to the success callback function in JS. Commented Nov 13, 2016 at 12:46
  • echo json_encode($array); Commented Nov 13, 2016 at 12:46
  • 2
    Same question as here: stackoverflow.com/questions/40572547/… -You can edit question there itself Commented Nov 13, 2016 at 12:48

2 Answers 2

1
    $("#formUnlock").submit(function(e)
    {
       e.preventDefault();

       $.ajax(
       {
            url: '../scripts/cch.php',
            method: 'POST',
            data: $(#formUnlock).serialize(),
            dataType: 'JSON',
            success: function(data) //parameter missing
            {
    var json = $.parseJSON(data);
console.log(json);
//in console you will get the result response        
            }
       }); 
    });
Sign up to request clarification or add additional context in comments.

Comments

1

Hello your php code should echo the JSON output like this:

<?php
$stmtcheck = $mysqli->prepare("SELECT id,email,location FROM members WHERE email=? AND unlock_code=?");
$stmtcheck->bind_param("si", $_SESSION['unlockemail'], $_POST['code']);
$stmtcheck->execute();
$stmtcheck->bind_result($id,$email,$location);
$stmtcheck->fetch();
$array=array($id,$email,$location);
$stmtcheck->close();
echo json_encode($array);
?>

Now, in your javascript, you can use JSON.parse method to get the JSON. Also, specifying your response type as JSON will automatically return a JSON object.

$("#formUnlock").submit(function (e) {
    e.preventDefault();
    $.ajax({
        url: '../scripts/cch.php',
        method: 'POST',
        data: $('#formUnlock').serialize(),
        dataType: 'JSON',
        success: function (json_string) {
            var res = JSON.parse(json_string);
            console.log(res);//This should output your JSON in console..
        }
    });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.