简体   繁体   中英

Ajax Call From PHP File TO a PHP File

Here is a ajax call i did in my php file :

$.ajax({
                type: "POST",
                url: "../includes/filters.php",
                data: {"email" : email},
                dataType: "json",
                success: function(data) 
                {
                    if(data.equals('sent'))
                     {  
                        alert(data);
                     }
                    else
                    {
                        alert("There Was Some Problem Please Try Again!");
                    }

                },
                    error: function(xhr, status, text) {
                        console.log(xhr);
                        console.log(status);
                        console.log(text);
                    }
              });

and here is the filters.php file :

$email = $_POST['email'];
        $email = filter_var($email, FILTER_VALIDATE_EMAIL);
        $query = "---------some query------";
        $stmt = $mysqli->prepare($query);
        $stmt->bind_param("s", $email);                 
        $stmt->execute();
        $stmt->store_result();
        $temp = $stmt->num_rows;
        if ($temp == 1)         
        return json_encode('true');
        else 
        return json_encode('false');

here is the error i am getting in the consol

Object {readyState: 4, responseText: "", status: 200, statusText: "OK"}
(index):120 parsererror
(index):121 SyntaxError: Unexpected end of input
    at Object.parse (native)
    at n.parseJSON (http://localhost/pedagoge/assets/plugins/jquery/jquery-2.1.4.min.js:4:5497)
    at ub (http://localhost/pedagoge/assets/plugins/jquery/jquery-2.1.4.min.js:4:7521)
    at x (http://localhost/pedagoge/assets/plugins/jquery/jquery-2.1.4.min.js:4:10935)
    at XMLHttpRequest.n.ajaxTransport.k.cors.a.crossDomain.send.b (http://localhost/pedagoge/assets/plugins/jquery/jquery-2.1.4.min.js:4:14765)

please help me out, since i have already tried all the JSON conversion, i even tried converting a php variable with a string in it, no lead

You need to echo the JSON response, not return it.

echo json_encode($temp == 1 ? 'true' : 'false');

Also, your Javascript code expects the response to be sent when it's successful, not true or false .

And in Javascript, you compare things with == , not .equals() :

if (data == "sent")

There is no output from filters.php ; you need to echo instead of return .

Without using JSON:

    $email = $_POST['email'];
    $email = filter_var($email, FILTER_VALIDATE_EMAIL);
    $query = "---------some query------";
    $stmt = $mysqli->prepare($query);  // XXX $mysqli is not initialized
    $stmt->bind_param("s", $email);                 
    $stmt->execute();
    $stmt->store_result();
    echo $stmt->num_rows == 1 ? "sent" : "not-sent";

I am pretty sure that the result of your json_encode('true'); does not actually constitute valid JSON, which you have specified in your ajax call as the dataType you are expecting the result to be returned as, which is why you are getting the parse error.

echo json_encode('true');

Gives

"true"

Not a json string by any stretch of the imagination.

Try this instead

$result = array();
$result['status'] = 'not sent';

if ($temp == 1) {
    $result['status'] = 'sent';
}

echo json_encode($result);

Now in your javascript success function

success: function(data) 
{
    if(data.status == 'sent') {  
        alert(data.status);
    } else {
        alert("There Was Some Problem Please Try Again!");
    }

},

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM