2014-12-04 36 views
1

我正在学习ajax,而且我面临着一个巨大的问题,我无法解决。我发送了一些变量到php中,我在那里做了各种检查,并将字符串值返回给ajax。我可以将这些显示为textare一个(奇怪的结果即刻消失)和输入字段。我想要做的就是将这些显示到div s。任何人都可以解释我该怎么做,我会在下面留下一些评论的代码。谢谢! P.S这是关于为用户显示注册表单错误,也许我可以使用不同的方法?随意提出一些建议。再次感谢!把json asnwer放入html标记

php.php

<?php 


//various checking here 
echo json_encode(array(
'email_error' => $email_error, 
'password_error' =>$password)); 
?> 

HTML

<html> 
<head> 

<link rel="stylesheet" type="text/css" href="mystyle.css"> 

</head> 
<body> 


<form> 
Your email: 
<input type = "text" id = "email"><br> 
Your password: 
<input type = "password" id = "password1"><br> 
Repeat password: 
<input type = "password" id = "password2"><br> 
<button type = "submit" id = "push_button">PushMe</button> 
</form> 
<div id ="email_errors"> </div> <--I want to put variable $email_errors here --> 
<div id = "password_errors"> </div> <--I want to put variable $password_errors here --> 

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js" type =       "text/javascript" ></script> 
<script type="text/javascript" src="java_script.js"></script> 
</body> 
</html> 

java_script.js

$(document).ready(function() { 

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

$.ajax({ 
url: "php.php", 
type: "POST", 
dataType: "json", 
data: { 
email: $("#email").val(), 
password1: $("#password1").val(), 
password2: $("#password2").val() 
}, 
success: function(data) { 

$("#email_errors").val(data.email_error); // i return these values (i tried html data type either 
$("#password_errors").val(data.password_error); 
} 
}); 

}); 
}) 
+0

什么是JSON的样子? – sbonkosky 2014-12-04 20:26:03

+0

'div'没有'value'属性。你想使用'.html()' - >'$(“#email_errors”)。html(data.email_error); $(“#password_errors”)。html(data.password_error);' – Sean 2014-12-04 20:29:12

+0

还是没有,我可以看到我的字符串只有当我改变输入字段的div。 – 2014-12-04 20:53:25

回答

0

你只希望字符串变量的div里面说明了什么?如果这是你想做的事,你必须使用的,而不是“VAL”“HTML”:如果你的数据回来的JSON赤贫的形式

success: function(data) { 

    $("#email_errors").html(data.email_error); 
    $("#password_errors").html(data.password_error); 
} 

,并要显示整个对象,你可以做是这样的:

success: function(data) { 

    $("#email_errors").html(JSON.stringify(data)); 
} 

ALSO:(我不知道,如果它在PHP中一样),你$('#push_button').click()可能会产生回传,从而消除了div的内部HTML。尝试把return false在你点击功能月底阻止发生回传:

$(文件)。就绪(函数(){

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

    $.ajax({ 
     url: "php.php", 
     type: "POST", 
     dataType: "json", 
     data: { 
      email: $("#email").val(), 
      password1: $("#password1").val(), 
      password2: $("#password2").val() 
     }, 
     success: function (data) { 

      $("#email_errors").html(data.email_error); // i return these values (i tried html data type either 
      $("#password_errors").html(data.password_error); 
     } 
    }); 

    return false; // this should stop the postback 
}); 

})

+0

我如何指定$(“#email_errors”)。html将包含来自php文件的'email_error“?顺便说一下,我的数组只包含1个元素 – 2014-12-04 20:43:05

+0

我的经验仅限于.NET Aspx,所以我不知道如何从php后端分配'email_error',我相信其他人可以帮助你解决这个问题。 – fictus 2014-12-04 20:58:31