我從使用ajax的php文件中選擇錯誤,而且我面臨一些麻煩。就像在php文件中,我將錯誤導入$ email_error和$ password_error,因此我想要將錯誤報告返回給ajax,並將$ email_error分配給id =「email_errors」和$ password_error以id =「password_errors」。也許有人可以解釋我如何指定我想返回的變量以及它應該採用什麼樣的ID。我會在下面留下一些註釋的代碼。謝謝!jQuery Ajax從php返回一些值
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或一些其它易於解析格式連載它。 – Bergi 2014-12-02 14:07:15