The best way is to use JSON as the dataType.
In PHP, all you need to use is the json_encode() function to convert a
PHP array to JSON. It's like an easy way to pass back an array full of
data back to the client that you can manipulate as regular Javascript
variables.
For example, your PHP code would look like:

$myArray = array(
     'firstName' => 'John',
     'lastName' => 'Doe',
     'error' => 0
);
echo json_encode($myArray);  // echo your response back
exit;

Then in your $.ajax success callback function:
function(data) {
     alert(data.firstName); // -> alerts 'John'
}

On Mar 27, 12:34 pm, Link <ryanwjack...@gmail.com> wrote:
> So I am working on an AJAX call to a PHP script which validates the
> data submitted in a form.  My question concerns the best way to get
> the data from PHP back to the jQuery script.  Should the dataType
> field be html or script?  I tried html and I know I could parse out
> what I need, but in the documentation it says script runs the code as
> a javascript script, but I'm not sure of the scope of this script.  I
> tried setting a variable name in the script and changing a global
> variable value, but I saw no change when I displayed the value with
> alert().  Am I missing something?
>
> Code below:
>
> $.ajax({
>                         type: "POST",
>                         url: "/register/index.php?action=validate",
>                         data: "email="+ email + "& pass1=" + pass1,
>                         dataType: "script",//"html",
>                         complete: function (XMLHttpRequest, textStatus) {
>                                 alert("complete");
>                         },
>                         success: function(del){
>                                 alert("success");
>                                 alert(del);
>                                 alert(email);
>                                 $("#email").addClass("error");
>                                 /*$('form#submit').hide();
>                                 $('div.success').fadeIn();*/
>                         },
>                         error: function (xhr, desc, exceptionobj) {
>                                 alert("error");
>                                 //alert(xhr.responseText);
>                                 alert(exceptionobj);
>                         }
>                 });
>
> I can make the PHP file return whatever I want.  What is the best in
> this situation?
>
> Thanks!

Reply via email to