我在这里找到了一个教程:http://tutorialzine.com/2009/08/creating-a-facebook-like-registration-form-with-jquery/(请看看)
这是一个很好的教程,我遵循了那里的一切,并删除了我不想要的额外的东西,像函数。php与generate_function选项,因为我不需要生日等东西。
所有我想要的是一个名称(usrname),电子邮件(电子邮件),密码(密码),当用户点击"注册"按钮(这是表单提交按钮),我从教程中得到的脚本将发送数据到"regprocess.php",其中包含验证检查代码,如检查提交的表单数据是否为空。
但是当我单击REGISTER时,数据没有从"regprocess.php"或成功消息发回(错误消息)。当我检查我的firebug时, 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"}';
检查用户名,电子邮件和密码数据是否为空,如果是,返回一条消息,该消息将显示在错误中(html中的# Error),它还检查提供的电子邮件是否有效。
如果一切正常,用户将被引导到registered.html
但是我认为脚本无法从php获取错误信息。
我希望有人能帮助我。谢谢。祝你过得愉快。嗯,不是太多的答案,但我做我的表单是一个我通过ajax提交,并把结果从php页面在表单的父。下面是插件的代码。默认情况下,当窗体是div的子窗体时,它会工作。
(function($){
$.fn.extend({
//pass the options variable to the function
ajaxForm: function(options)
{
//Set the default values, use comma to separate the settings, example:
var defaults =
{
target: 'div'
}
var options = $.extend(defaults, options);
return this.each(function()
{
var o=options
$(this).submit(function(event)
{
event.preventDefault();//stop from submiting
//set needed variables
var $form = $(this)
var $div = $form.parent(o.target)
$url = $form.attr("action");
//submit via post and put results in div
$.post( $url, $form.serialize() , function(data)
{ $div.html(data) })
})
});
}
});
})(jQuery);
注意这将对每个表单运行,所以您可以根据需要更改它。只要在PHP页面上显示您想要显示的内容即可。此外,这是为post和PHP页面将访问任何东西,就像任何其他形式与post。而且,如果你觉得有必要将其发送为json,也不难修改。
您需要在php代码周围放置php标记,如下所示:
<?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"}';
?>