38

So here's my issue, I am using AJAX (jQuery) to post a form to process.php but the page actually needs to echo out a response such as apple or plum. I'm not sure how to take the response from process.php and have it stored as a variable...

Here's the code I have so far:

<script type="text/javascript">
        function returnwasset(){
            alert('return sent');
            $.ajax({
                type: "POST",
                url: "process.php",
                data: somedata;
                success function(){
                    //echo what the server sent back...
                }
            });
        }
    </script>

Also will I need to echo the response in process.php in json? or will plain text be fine?

Sorry if this sounds like a stupid question, this is my first time doing something as such in Ajax.

PS: How do I name the POST request in the above code?

1
  • 11
    not a stupid question. Commented Feb 26, 2016 at 14:46

5 Answers 5

48

<?php echo 'apple'; ?> is pretty much literally all you need on the server.

as for the JS side, the output of the server-side script is passed as a parameter to the success handler function, so you'd have

success: function(data) {
   alert(data); // apple
}
Sign up to request clarification or add additional context in comments.

7 Comments

success function is missing a : after success. you can trivially check for syntax errors by opening your browser's JS/debug console.
thanks! I'm going to mark this as the answer, I appreciate it a lot (sorry about my obvious mistakes, I'm new to AJAX) umm also, where do I name the post request so the server will recognize it as $_POST['blah'] ?
@MarcB : can I get variables values,which I have in process.php??
I'm surprised how difficult it was to find out that the "return" simply needs to be an echo. The first time I tried to do Ajax I had the hardest time figuring this out; but it's so simple. Many tutorials skip past this vital info because, presumably, they think it's so obvious they don't need to say it.
use echo json_encode('apple'); instead of echo 'apple';
|
35

The good practice is to use like this:

$.ajax({
    type: "POST",
    url: "/ajax/request.html",
    data: {action: 'test'},
    dataType:'JSON', 
    success: function(response){
        console.log(response.blablabla);
        // put on console what server sent back...
    }
});

and the php part is:

<?php
    if(isset($_POST['action']) && !empty($_POST['action'])) {
        echo json_encode(array("blablabla"=>$variable));
    }
?>

1 Comment

I would agree most with this answer. You can also include multiple variables in the response, such as array("blablabla"=>$variable, "success"=>true, "user_message"="hello") then simply use them in the JavaScript e.g. response.success.
16
<script type="text/javascript">
        function returnwasset(){
            alert('return sent');
            $.ajax({
                type: "POST",
                url: "process.php",
                data: somedata;
                dataType:'text'; //or HTML, JSON, etc.
                success: function(response){
                    alert(response);
                    //echo what the server sent back...
                }
            });
        }
    </script>

3 Comments

thanks for the answer, I've given it an upvote, now I just need to decide who to mark as answer :)
"With great power comes great responsibility." -Ben Parker
sorry ethan, but since mark answered it first, I think he deserves me marking it as answer, plus he guided me through afterwards. I've still given you an upvote :) thanks anyways!
12

in your PHP file, when you echo your data use json_encode (http://php.net/manual/en/function.json-encode.php)

e.g.

<?php
//plum or data...
$output = array("data","plum");

echo json_encode($output);

?>

in your javascript code, when your ajax completes the json encoded response data can be turned into an js array like this:

 $.ajax({
                type: "POST",
                url: "process.php",
                data: somedata;
                success function(json_data){
                    var data_array = $.parseJSON(json_data);

                    //access your data like this:
                    var plum_or_whatever = data_array['output'];.
                    //continue from here...
                }
            });

Comments

1
var data="your data";//ex data="id="+id;
      $.ajax({
       method : "POST",
       url : "file name",  //url: "demo.php"
       data : "data",
       success : function(result){
               //set result to div or target 
              //ex $("#divid).html(result)
        }
   });

1 Comment

If you treat "data" as a string, then how the actual data will go ?

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.