1

Using ajax+php in my signup form. There are 2 validations: JS side frontend and PHP side backend. Created special function called response in PHP side: It sends PHP side error as JSON data.

The problem is I can't get any response from PHP side.

Analyzed page in firebug: getting error message responseData is null. (responseData = jQuery.parseJSON(data))

JS part looks like that

  //check the form is not currently submitting
  if ($(this).data('formstatus') !== 'submitting') {

 var form = $(this),
    formData = form.serialize() + '&formID=' + form.attr('id'),
    formUrl = form.attr('action'),
    formMethod = form.attr('method');



 //add status data to form
 form.data('formstatus', 'submitting');

 if (validate()) {
    //send data to server for validation
    $.ajax({
       url: formUrl,
       type: formMethod,
       data: formData,
       success: function (data) {

          //setup variables
          var responseData = jQuery.parseJSON(data),
             cl, text;

          //response conditional
          switch (responseData.status) {
          case 'error':
             cl = 'error';
             text = responseData.message;
             break;
          case 'success':
             cl = 'success';
             text = 'Qeydiyyat uğurla başa çatdı';
             break;
          }


          $.notifyBar({
             cls: cl,
             html: text
          });

       }
    });

 }
 form.data('formstatus', 'idle');


 }

And here is PHP part

    <?php
require '../common.php';

function checkIfEmailExists($email, $stmt)
{
        if ($stmt = $db->prepare("SELECT id FROM TABLE WHERE email=? LIMIT 1")) {
                $stmt->bind_param("s", $email);
                $stmt->execute();
                $stmt->bind_result($count);
                $stmt->close();
        }

        return ($count > 0 ? true : false);
}


if ($_POST['formID'] == 'signup_form') {
        // Setting vars
        $lname        = $_POST['lname'];
        $fname        = $_POST['fname'];
        $mname        = $_POST['mname'];
        $email        = $_POST['email'];
        $pass         = $_POST['pass'];
        $confirm_pass = $_POST['confirm_pass'];

        //===================== 
        //Server side validation >>


        //First name, middle name, last name check >>
        if (!$lname) {
                response('error', 'Familiyanı daxil edin');
        }
        if (!$fname) {
                response('error', 'Adı daxil edin');
        }
        if (!$mname) {
                response('error', 'Atanızın adını daxil edin');
        }
        //<<

        //Pass check >>
        if (strlen($pass) > 2) {
                if ($pass == $confirm_pass) {
                        return true;
                } else {
                        response('error', 'Şifrənin təkrarlanmasında səhv');
                }
        } else {
                response('error', 'Şifrədə simvolların sayı 4-dən çox olmalıdır');
        }

        //<<


        //email validation >>
        if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
                if (!checkIfEmailExists($email, $stmt)) {
                        return true;
                } else {
                        response('error', 'Bu ünvanla qeydiyyata alınmış başqa istifadəçi var.');
                }
        } else {
                response('error', 'Email ünvanını düzgün daxil edin');
        }

        //<<

        // Create statement object
        $stmt = $db->stmt_init();

        // Create a prepared statement
        if ($stmt->prepare("INSERT INTO `users` (`fname`, `mname`, `lname`, `email`, `pass`, `reg_dt`) VALUES (?, ?, ?, ?, ?, NOW())")) {
                // Binding vars

                $rc = $stmt->bind_param('sssss', $fname, $lname, $mname, $email, $pass) or die('bind_param() failed: ' . htmlspecialchars($stmt->error));

                // Execute query
                $rc = $stmt->execute();
                if ($rc) {
                        response('success', 'Qeydiyyat uğurla başa çatdı');
                } else {
                        response('error', htmlspecialchars($stmt->error));
                }


                // Close statement object
                $stmt->close();

        } else {
                response('error', htmlspecialchars($dv->error));
        }



}
else {response('error', 'Qeydiyyatda problem');}

        //return json response
        function response($status, $message)
        {
                $data = array(
                        'status' => $status,
                        'message' => $message
                );
                echo json_encode($data);
                die();
        }
?>
16
  • Are you sure $_POST['formID'] == 'signup_form'? It also looks like you have some return true; calls spread through there rather than printing something or just continuing on. Commented Nov 8, 2011 at 13:02
  • Analyzed page in firebug: getting error message responseData is null. (responseData = jQuery.parseJSON(data)) … That doesn't mean you aren't getting a response. What is data? Is it empty? Commented Nov 8, 2011 at 13:03
  • Tried to simply check whether your script responses anything by adding echo "test"; below your include for example? Commented Nov 8, 2011 at 13:03
  • Is $_POST['formID'] == 'signup_form'? Commented Nov 8, 2011 at 13:04
  • @Quentin data is response from PHP Commented Nov 8, 2011 at 13:05

1 Answer 1

1

You need to add

dataType: "json",

In your $.Ajax method.

Sign up to request clarification or add additional context in comments.

7 Comments

Try alert(data); into success function. What it gives?
try do do some debugging. Try to output something from your php-script to make sure the javascript is not the problem. if the javascript get the debug-response, this is a php-related error somewhere in your script
I didn't downvote:) @OptimusCrime i have both if else statements, it, must response something in any case
@OptimusCrime also i tried to remove the submit function and to post data directly to php. It gave me message. PHP side works
what result do you get if you simply dump your $_POST in the php-file? Are the post-variables sent with ajax? echo json_encode($_POST); try that in your script
|

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.