0

i'm trying to build a signup form there i need to check that the value of load function of ajax is true or false.

i stuck in this problem here is my code

$("button").click(function(){
   $("#error").load("newEmptyPHP.php",{email:mail});
})

newEmptyPHP.php

<?php
        $mail=$_POST["email"];
        $db=new PDO("mysql:host=localhost;dbname=the_scops","root","");
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $STH=$db->prepare("SELECT email FROM signup WHERE email=?");
        $STH->execute([$mail]);
        if($STH->rowCount() == 1){
            //echo "<script>$('#error').html('Email alreday exist')</script>";
            return false;
        }
        else{
          return true;
        }

**

1
  • When you do a request to newEmptyPHP.php you expect a response. A response has headers and a body. You can use them to provide the "result" of your operation. (i.e: with http status code, using 200 OK and 4xx).
    – Federkun
    Aug 27, 2017 at 7:40

2 Answers 2

2

You know, an ajax call returns the output of a function or a script not its evaluation. I strongly suspect that you will have an empty response when you call your code by hand, and the same is for the javascript issuing the ajax request.

If your are showing all the code involved in your program and do not have any middleware to decorate your return value, you should modify your script in a way similar to this:

<?php
    $mail=$_POST["email"];

    $db=new PDO("mysql:host=localhost;dbname=the_scops","root","");
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $STH=$db->prepare("SELECT email FROM signup WHERE email=?");
    $STH->execute([$mail]);
    $result = [];
    if($STH->rowCount() == 1){
        // echo "<script>$('#error').html('Email alreday exist')</script>";
        $result["success"] = false;
        $result["message"] = 'Email already exists';
    } else{
      $result["success"] = true;
    }
    // comunicate to the client that the response is json encoded
    header('Content-type:application/json');
    // output the response
    echo json_encode($result);

The javascript part have to change too (i'm not used to jquery, take it carefully):

$("button").click(function(){
   $.get("newEmptyPHP.php",{email:mail}, function(data) {
       if (data.success == false) {
           $('#error').html(data.message);
       } else {
         // do wathever you need in case of data.success
       }
    })
})
1

you must echo true or false in json format. sth like this.

<?php

     $result = array();
     $mail=$_POST["email"];
     $db=new PDO("mysql:host=localhost;dbname=the_scops","root","");
     $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
     $STH=$db->prepare("SELECT email FROM signup WHERE email=?");
     $STH->execute([$mail]);
     if($STH->rowCount() == 1){    
         $result['status'] = false;
     }
     else{
         $result['status'] = true;
     }

     echo json_encode($result);
     exit(0);

?>

Edit : you can handle response like this.

$("button").click(function(){
   $("#error").load("newEmptyPHP.php",{email:mail},function(response){
       result = $.parseJSON(response);
       if(result.status){
           //true
       } else {
           //false
       }
   });
});
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.