Alexis Alexis - 21 days ago 5
Javascript Question

checking returned data from PHP script through jquery

I have written a php script

cch.php

$stmtcheck = $mysqli->prepare("SELECT id FROM members WHERE email=? AND unlock_code=?");
$stmtcheck->bind_param("si", $_SESSION['unlockemail'], $_POST['code']);
$stmtcheck->execute();
$stmtcheck->bind_result($id);
$stmtcheck->fetch();
$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(),
success: function()
{
alert("unlocked");
}
});
});


Now what I want to check whether
$id
has some value or not! How would I fetch the
$id
variable to my main script?

Ima Ima
Answer

In cch.php, if you want to pass only an id to the javascript, you can print id

echo $id;

What ever data is received on ajax response will be passed as parameter to the success callback function. Either you have to execute the success actions inside the succes call back OR You have to write a function to be executed on ajax success and call that function and pass the params

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

   $.ajax(
   {
        url: '../scripts/cch.php',
        method: 'POST',
        data: $(#formUnlock).serialize(),
        success: function(responseData)
        {
            if (responseData == '' )
            {
                 alert("Sorry failed");
            }
            else
            {
                alert("Success");
            }
        }
   }); 
});
Comments