0

I hope this isn't a duplicate; the other similar questions I read didn't help me solve my problem.

I'm receiving a blank response (i.e. data = "") from a jQuery Ajax call to my PHP script, used to validate a user's submitted CAPTCHA value. I'm using Cryptographp for my CAPTCHA, and it works as expected, so I'm thinking it's most likely an error either in my Ajax call or the PHP script.

Firebug showing correct POST values ('code' is the submitted CAPTCHA value to test):

code      a
email     [email protected]
emailtext a
firstname a
lastname  a
phone     

Ajax function called onsubmit to determine whether or not to submit the form:

function validateCaptcha()
{
  // Assume an invalid CAPTCHA
  var valid = false;

  // The form containing the CAPTCHA value
  var data_string = $('form#emailform').serialize();

  // Make the Ajax call
  $.ajax({
    url: "captcha.php",
    data: data_string,
    type: "POST",
    async: false,
    success: function (data) {
      if (data == "true")
      {
        valid = true;
      }

      alert ("data: " + data);
    }
  });

  return valid;
}

captcha.php

<?
  $cryptinstall="crypt/cryptographp.fct.php";
  include $cryptinstall;

  // Begin the session
  session_start();

  //Check if CAPTCHA values match
  if(chk_crypt($_POST["code"]))
    return true;
  else
    return false;
?>

My expectation is that the above snippet should return a response of simply "true" or "false," but perhaps this is not the case.

Any help pointing out my error would be greatly appreciated!

1
  • As an aside, I've noticed many people criticize making synchronous calls with Ajax (duh, Ajax, I know!). Any explanations why I would want my form submission to be asynchronous/non-blocking would be welcome. Commented Aug 15, 2012 at 19:04

2 Answers 2

4

You need to use "echo" instead of "return" and write is as a string. return is for returning results of functions.

    <?
      $cryptinstall="crypt/cryptographp.fct.php";
      include $cryptinstall;

      // Begin the session
      session_start();

      //Check if CAPTCHA values match
      if(chk_crypt($_POST["code"]))
        echo "true";
      else
        echo "false;
    ?>
Sign up to request clarification or add additional context in comments.

1 Comment

D'oh! Dumb mistake on my part. I've just been working with functions for too long recently. Works as expected now. :)
2

From your captcha.php you are not echoing/printing anything so it's returning nothing. Just replace your return true; and return false; with echo.

Browser can only receive something when you'll print something from the script.

if(chk_crypt($_POST["code"])) echo true; // 1
else echo false;// 0

or

if(chk_crypt($_POST["code"])) echo 'true'; // true
else echo 'false';// false

1 Comment

Yes, I just now realized my mistake. Thanks for the response. :)

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.