[英]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.