我正在学习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);
}
});
});
})
什么是JSON的样子? – sbonkosky 2014-12-04 20:26:03
'div'没有'value'属性。你想使用'.html()' - >'$(“#email_errors”)。html(data.email_error); $(“#password_errors”)。html(data.password_error);' – Sean 2014-12-04 20:29:12
还是没有,我可以看到我的字符串只有当我改变输入字段的div。 – 2014-12-04 20:53:25