2

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?

2
  • By main script do you mean the javascript portion? Commented Nov 13, 2016 at 9:55
  • echo !empty($id)? 'y' : 'n' Commented Nov 13, 2016 at 9:56

1 Answer 1

1

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");
            }
        }
   }); 
});
Sign up to request clarification or add additional context in comments.

1 Comment

success: function(responseData) In this line responseData can be any variable. This variable wii be assigned with any response from server.

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.