jQuery Ajax从php返回几个值

I'm picking errors from php file using ajax and i face few troubles. Like in php file i take errors into $email_error and $password_error so i want to return error reports to ajax and assign $email_error to id = "email_errors" and $password_error to id = "password_errors". Maybe someone could explain how i specify what variables i want to return and what id should it take .I will leave some commented code below. Thanks!

php

<?php


if (isset($_POST['email']) && isset($_POST['password1']) && isset($_POST['password2'])) {

$email = trim ($_POST['email']);
$password1 = trim ($_POST['password1']);
$password2 = trim ($_POST['password2']);

}

$email_error = 'No errors<br>';
$password_error = 'No errors<br>';

if (empty($email))
$email_error = ('Pleas fill in email field<br>');

if ($email == 'example')
$email_error =('There already is user with this email<br>');

if (empty($password1))
$password_error =  ('Please fill in password fields<br>'); 

if (empty($password2))
$password_error = ('Please fill in password fields<br>');

$email_error; //this variable must be returned to ajax and assigned to id "email_errors"
$password_error; //this variable must be returned to ajax and assigned to id "password_errors"

?>

javascript

$(document).ready(function ()   {

$('#push_button').click(function() {

$.post('php.php',
{
email : $('#email').val(), // i take these variables to php
password1 : $('#password1').val(),
password1 : $('#password2').val()
} ,
function ( data ) { //what do i return here?

$('#email_errors').val(data); //how to assign $emaill_error to #email_errors
$('#password_errors').val(data); //how to assign $password_error to #password_errors

}
)
})


})

If you want to return several variables to ajax, you would have to return some json

PHP :

// .. your php code
$ret = array("email_error" => $email_error, "password_error" => $password_error);
echo json_encode($ret);

Be careful, json_encode needs PHP >= 5.2

JS :

$.ajax({
  url: "php.php",
  type: "POST",
  dataType: "json", // The type of data that you're expecting back from the server
  data: {
    email: $("#email").val(),
    password1: $("#password1").val(),
    password2: $("#password2").val() // careful, you had "password1" as variable name 2 times
  },
  success: function(obj) {
    // obj is your php array, transformed into a js object
    // you may want to use .html() instead of .val() since your errors are strings with html tags - depends if #email_errors / #password_errors are inputs or divs
    $("#email_errors").html(obj.email_error);
    $("#password_errors").html(obj.password_error);
  }
});

In PHP, the following will return nothing:

$email_error; 
$password_error;

Your not echo'ing the values or anything. If you want to pass two different values, I'd return a JSON object like so (in PHP):

echo json_encode(array(
    'email_error' => $email_error,
    'password_error' => $password_error
));

And then in JavaScript, your data should now be a JavaScript object, as jQuery should parse the JSON object and understand it as an object. So you'll be able to do like this in JavaScript:

$('#email_errors').val(data.email_error); 
$('#password_errors').val(data.password_error);

If you don't want to use an array, you could create a new object and then pass that object to json_encode.

$obj = new stdClass;
$obj->email_error = $email_error;
$obj->password_error = $password_error;

echo json_encode($obj);

to return value simply echo the variable with json_encode() e.g.

$return_array = new array();
$return_array['email_error'] = $email_error;
$return_array['password_errors'] = $password_errors;
echo json_encode($return_array);

in the javascript function (data){}:

function ( data ) { //what do i return here?

    $('#email_errors').val(data['email_error']); //how to assign $emaill_error to #email_errors
    $('#password_errors').val(data['password_errors']); //how to assign $password_error to #password_errors

}