【发布时间】:2011-09-03 04:45:47
【问题描述】:
我在这里找到了一个教程:http://tutorialzine.com/2009/08/creating-a-facebook-like-registration-form-with-jquery/(请看一下)
这是一个很好的教程,我遵循了那里的所有内容并删除了我不想要的额外内容,例如带有 generate_function 选项的 functions.php,因为我不需要生日等内容。
我想要的只是一个 NAME(usrname) , EMAIL(email) , Password(password) ,当用户点击“REGISTER”按钮(这是表单提交按钮)时,我从教程中得到的脚本将发送将数据转移到“regprocess.php”,其中包含验证检查代码,例如检查提交的表单数据是否为空。
但是当我单击 REGISTER 时,数据不会从“regprocess.php”或成功消息发送回(错误消息)。 当我检查我的萤火虫时,JSON 响应显示完整的 php 代码,如下所示(向下滚动)。
这是我的代码: HTML-
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js">
</script>
<script type="text/javascript" src="register.js"></script>
<form id="regForm" action="regprocess.php" method="post">
<label for="usrname">Name:</label>
<input id="usrname" name="usrname" type="text" value="" class="nor">
<label for="email">Email:</label>
<input id="email" name="email" type="text" value="" class="nor">
<label for="password">Password:</label>
<input id="password" name="password" type="password" value="" class="nor">
<table><tr><td style="width:290px;"><div id="error"> </div></td><td><input name="register" type="submit" value="Register" id="regbtn"><center><img id="loading" src="images/load.gif" alt="Registering..." /></center></td></tr></table>
</form>
好的,Ajax 脚本在上面的“register.js”中。
Ajax 脚本(register.js)-
$(document).ready(function(){
$('#regForm').submit(function(e) {
register();
e.preventDefault();
});
});
function register()
{
hideshow('loading',1);
hideshow('regbtn',0);
error(0);
$.ajax({
type: "POST",
url: "regprocess.php",
data: $('#regForm').serialize(),
dataType: "json",
success: function(msg){
if(parseInt(msg.status)==1)
{
window.location=msg.txt;
}
else if(parseInt(msg.status)==0)
{
error(1,msg.txt);
}
hideshow('loading',0);
hideshow('regbtn',1);
}
});
}
function hideshow(el,act)
{
if(act) $('#'+el).css('visibility','visible');
else $('#'+el).css('visibility','hidden');
}
function error(act,txt)
{
hideshow('error',act);
if(txt) $('#error').html(txt);
}
CSS: Regbtn 是提交按钮,它的可见性设置为可见 加载设置为隐藏 错误设置为隐藏
当用户点击 Regbtn 时,加载可见性将变为可见,而 Regbtn 隐藏(visibility:hidden)。
这是在 Ajax 脚本(register.js)中完成的。
好的,现在php: PHP(regprocess.php)-
if(empty($_POST['usrname']) || empty($_POST['email']) || empty($_POST['password']))
{
die('{status:0,"txt":"Fill in All Fields"}');
}
if(!(preg_match("/^[\.A-z0-9_\-\+]+[@][A-z0-9_\-]+([.][A-z0-9_\-]+)+[A-z]{1,4}$/", $_POST['email'])))
die('{status:0,"txt":"Please Provide a Valid Email"}');
echo '{status:1,txt:"registered.html"}';
这将检查用户名,电子邮件和密码数据是否为空,如果是,则返回一条消息,该消息将显示在 Error(#error in html) 中,它还检查提供的电子邮件是否有效。
如果其他一切都正确,用户将被引导到 registered.html
但我认为脚本无法从 php 中获取错误消息。
我希望有人可以帮助我。谢谢。 祝你有美好的一天。
【问题讨论】:
-
和答案?答案在哪里?
-
json 答案.. 哦!你在萤火虫中看到了什么?
-
JSON 响应显示的是完整的 PHP 代码,而不是状态,例如:{status:0,"txt":"Fill in All Fields"}
-
那不是他完整的php代码,这就是答案...
-
是的,这是我完整的php代码,看教程,一样的,我只是拿出生日和一些。