繁体   English   中英

jQuery Ajax从php返回一些值

[英]jQuery Ajax return few values from php

我正在使用ajax从php文件中选择错误,我遇到了一些麻烦。 像在php文件中一样,我将错误放入$ email_error和$ password_error中,因此我想将错误报告返回给ajax,并将$ email_error分配给id =“ email_errors”,将$ password_error分配给id =“ password_errors”。 也许有人可以解释我如何指定要返回的变量以及应使用的ID。我将在下面留下一些注释的代码。 谢谢!

的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

}
)
})


})

要返回值,只需使用json_encode()来回显变量,例如

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

在javascript函数(数据){}中:

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

}

如果要向ajax返回多个变量,则必须返回一些json

PHP的:

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

请注意,json_encode需要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);
  }
});

在PHP中,以下内容不会返回任何内容:

$email_error; 
$password_error;

您不回显值或任何东西。 如果要传递两个不同的值,我将返回一个JSON对象,如下所示(在PHP中):

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

然后在JavaScript中,您的数据现在应该是一个JavaScript对象,因为jQuery应该解析JSON对象并将其理解为对象。 这样您就可以在JavaScript中执行以下操作:

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

如果您不想使用数组,则可以创建一个新对象,然后将该对象传递给json_encode

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

echo json_encode($obj);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM