2014-12-02 67 views
0

我從使用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 

} 
) 
}) 


}) 
+0

使用適當的數據結構和JSON或一些其它易於解析格式連載它。 – Bergi 2014-12-02 14:07:15

回答

1

如果你想幾個變量回到阿賈克斯,你將不得不返回一些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); 
    } 
}); 
0

在PHP中,以下將返回任何結果:

$email_error; 
$password_error; 

你不echo'ing值或任何東西。如果你想通過兩種不同的價值觀,我會返回一個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); 
+0

使用json時返回數組是否是必須的? – 2014-12-03 10:18:16

+0

查看我的編輯,有一個新的部分解釋如何使用對象 – jValdron 2014-12-03 12:18:42

2

返回值簡單地echo變量與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 

} 
+0

您忘記了PHP代碼中的'email_error'變量的's',這是行不通的。 – jValdron 2014-12-02 13:53:12

+0

在這裏的問題是它的php和email_errors中的js的email_error ...對不起,我只是將它粘貼在 – x1x 2014-12-02 14:06:54

+0

是啊,我沒有複製粘貼它,因此在我的答案中犯了一個錯誤:)對不起。 – jValdron 2014-12-02 14:07:53