我遇到了我的代码问题。我想通过在窗体有效时创建一个包含变量的数组来验证我的窗体。但要做到这一点,我需要使用isset方法来知道信息已发布。这里有一个简单的例子isset导致getjson返回undefined
http://richbaird.net/clregister
<?PHP
if(isset($_POST['username'])) {
$helloworld = array ("hello"=>"world","name"=>"bob");
print json_encode($helloworld);
};
if(!isset($_POST['username'])) {
echo json_encode(array('error' => true, 'message' => 'No username specified'));
?>
足够简单,如果用户名已经公布创建数组的HelloWorld。
我用下面的方法来获取JSON
<script>
//document ready
$(document).ready(function(){
var php = "helloworld.php";
//submit form
$("#loginform").ajaxForm
(
//on successful submission
function() {
//getjson
$.getJSON("helloworld.php",function(data) {
alert(data.message)
}) //close get json
.error(function(error) { alert(error.responsetext); })
.complete(function() { alert("complete"); });
} // close success
) // close submit
});
//end document ready
</script>
我使用jQuery插件的形式提交表单。
和我的形式看起来像这样
<form id="loginform" name="loginform" method="post" action="helloworld.php">
<label for="username">username</label>
<input type="text" name="username" id="username" />
<br />
<label for="password">password</label>
<input name="password" type="password" />
<br />
<input name="submit" type="submit" value="Login" id="subtn" />
</form>
网络控制台显示POST方法返回{你好:世界名:鲍勃}但GET返回指定的任何用户名这就是我在警报得到。它看起来像jquery试图获得代码之前,它有一个机会完全处理,我怎样才能防止这种情况?
良好的渔获,我加了引号,但仍然得到同样的错误。查看编辑 – richbai90 2013-03-07 16:35:53
我已经做了一些编辑!但最后一个音符可能是最重要的;-) – juco 2013-03-07 16:44:33
酷,所以我做了更新,并按照你说的做,我得到的错误没有指定用户名,这显然意味着它不会与邮件发送。我现在的问题是为什么不。我的控制台的网络标签清楚地说明了helloworld.php方法后。你知不知道发送了什么?预览还给了我{你好:“世界”名称:“鲍勃”} – richbai90 2013-03-07 16:56:28