1

I have here my AJAX code:

$.ajax({
    url: url,
    type: type,
    data: data,
    success: function(response){
        if ($returnValue == "success") {
            alert("+");
        }
        else {
            alert("-");
        }
    }
});

The code works. But how can i get a value in $returnValue via PHP? Something like this dont't work:

PHP

<?php return $returnValue; ?>

4 Answers 4

1

Your php needs to output what you want to return:

echo $returnValue;

or, for multiple values (this needs to be parsed in javascript):

echo json_encode($returnValue);

And then the value will be available in response:

    ...
    success: function(response){
       console.log(response);
       ...
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the answer! I tried is, but i don't work. If you want to know what i mean, look at the comments below Rory's answer.
@Name What is the exact output you get when you use console.log(response);?
I wrote a comment under Rory's answer.
1

Firstly you need to echo the value you want to return in PHP:

<?php echo $returnValue; ?>

Then in JS it will be assigned to the parameter you defined in the success handler function, in your case response. Try this:

$.ajax({
    url: url,
    type: type,
    data: data,
    success: function(response) {
        if ($.trim(response) == "success") {
            alert("+");
        } else {
            alert("-");
        }
    }
});

13 Comments

Thanks for the fast answer! I tried it alot now, but i don't work. I wrote this in my PHP file now $returnValue = "success";echo $returnValue; and i have copied your AJAX code.
Have you checked the console for any error messages?
Yes, firebug give no errors. I could write my whole php file into the question, but i think its to much? And its useless (i think so), because i added this 2 line's code at the end of the php file.
@Name You should check the exact response of the ajax call in the console. Any other output (like spaces, new-lines or php warnings) will cause your if condition to fail.
@jeroen good point. I updated my example to trim whitespace from the received text.
|
0

This should work for you.

$.ajax({
            url: url,
            type: type,
            data: data,
            success: function(data){
                if(data.response == "success"){
                    alert("+");
                }
                else{
                    alert("-");
                }
            }
        });

1 Comment

Note that OP is attempting to return a singular text value, not an object
0

In your Js file :

$.ajax({
url: "file.php", //Point your php file
dataType: "text",
    success: function(response) {
    if ($.trim(response) == "success") {
        alert("+");
    } else {
        alert("-");
    }
}

});

In your file.php

header("Content-Type: text/plain");
echo "success";

4 Comments

Thanks for the answer, but it don't work too. Is it important, that the AJAX type = post?
Try to console.log the answer from your php file
I wrote console.log(response). And the output is correct. But something with the if else is wrong.
Test the value of response var : success: function(response) { console.log(response) } If it's okay, add an third equal sign '=' that will assure that the type of response is text. if ($.trim(response) === "success")

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.