0

I'm new to web development, so please pardon any silly mistakes!

I'm using the following code send a jQuery Ajax request:

var d = "email=" + email + "&password=" + password;

        $.ajax({
            url: 'login/c_login_form.php',
            data: d,
            dataType: "text", //expected type from server
            type: "POST",
            success: function(str, status) {
                        console.log(str + ": " + status);
                     },
            error: function(obj) {
                        alert("Fatal error. Failed to send Ajax request.");
                     }
        });

For simplicity, let us assume that login/c_login_form.php is currently blank (but has the php tags, of course) and is to return a set data every time.

I was under the impression that whatever I echo from the server will be passed to the success function, but it's not working that way. What is the way to send data from PHP script back to Ajax? Although I've used text datatype, I'd be happy if somebody can tell me how to return JS objects.

7
  • what is in the login_form.php Commented Dec 1, 2014 at 10:40
  • @JqueryKing It has PHP code that queries the database, but for now assume it to be a blank file that just returns a fixed data every time. What should the format be? Something like echo "1, 2, 3";? Commented Dec 1, 2014 at 10:42
  • 2
    you should use dataType: 'json', and encode php response $data=array( "status"=>1, "err"=>"example_error_code", ); echo json_encode($data); Commented Dec 1, 2014 at 10:45
  • 1
    This is how it should be used, i don't see the problem. If you want your data to be readable from JS, you can json_encode() an array or your result, then use JSON dataType (or do $.parseJSON(str) in the success function). Commented Dec 1, 2014 at 10:46
  • @FC' and if I'm interested in plain text, then will the good old echo do? Commented Dec 1, 2014 at 10:47

2 Answers 2

1

To summarize comments in a concrete answer:

<?php
    header('Content-Type: application/json');

    $data = array("status" => 1, "err" => "example_error_code");
    echo json_encode($data);
?>

Please note, as Anthony Grist suggests, that you need, on your Javascript:

dataType: "json"

instead of:

dataType: "text"

See also jQuery ajax docs:

...
dataType (default: Intelligent Guess (xml, json, script, or html))
...
Sign up to request clarification or add additional context in comments.

3 Comments

Not a great summary, since you've totally ignored some pretty crucial information on what changes they'd need to make to their JavaScript.
PO question is: "What is the way to send data from PHP script back to Ajax?", so I suppose he knows handling the JS side... However I'll add a note, it can be useful... :-)
@MarcoS Well, thanks! That does seem like the way ahead. Let me try this and post another question in case something goes wrong.
1

You can refer this answer for Ajax post request.

To send back data from PHP file to JS you need to use echo.

If you are passing more than one variable then you can use echo json_encode($array_of _variables);. If there is only one variable than please you can use just echo $variable.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.