【发布时间】:2011-10-17 08:41:38
【问题描述】:
这一切都在一个 php 文件中
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>JQuery Form Example</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script><!--need at least one instance of jquery-->
<script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery.validate/1.7/jquery.validate.min.js"></script><!--ajax to use with jquery-->
<script type="text/javascript">
$(document).ready(function(){//when the document has loaded
$("#myform").validate({//Q: what is validate?, myform gets validated
debug: false,
rules: {
name: "required",//name and email are the only things passed
email: {
required: true,
email: true//I guess this defines the input as an email
}
},
messages: {//at what point is this message displayed?
name: "Please let us know who you are.",
email: "A valid email will help us get in touch with you.",
},
submitHandler: function(form) {//submit the form
// do other stuff for a valid form
$.post('process.php', $("#myform").serialize(), function(data) {//Q:what is serialization? myform gets serialized and assigned an action
$('#results').html(data);//what is this?
});
}
});
});
window.onload=function( )
{
}
</script>
<style>
label.error { width: 250px; display: inline; color: red;}
</style>
</head>
<body>
<form name="myform" id="myform" action="" method="POST">
<!-- The Name form field -->
<label for="name" id="name_label">Name</label>
<input type="text" name="name" id="name" size="30" value="Charlie"/>
<br>
<!-- The Email form field -->
<label for="email" id="email_label">Email</label>
<input type="text" name="email" id="email" size="30" value="obesemuleaccount@yahoo.com"/>
<br>
<!-- The Submit button -->
<input type="submit" name="submit" value="Submit">
</form>
<!-- We will output the results from process.php here -->
<div id="results"><div>
</body>
</html>
<!--ok, so I want to first try some stuff out to get a feel for how the form works-->
<!--upload the form to the site somewhere-->
<!--access and play around with it-->
<!--then figure out how to submit to multiple php forms-->
所以我使用的是 Jquery 和 Ajax,我需要自动提交表单。
我尝试过使用诸如 document.myform.submit 之类的东西,但这似乎不起作用。当我不使用 ajax 时它可以工作,但这会导致页面刷新到 php 文件的目标。
我想我只是缺少一些语法。我进行了搜索,发现了一些类似的主题: Ajax auto form submit
但我更像是一个业余设计师,所以我不太明白他们在说什么。我不会把修复我的代码的任务放在其他人身上,但我很确定这个变化很小,而且我已经有一段时间试图解决这个问题。非常感谢任何有帮助的人!请在您的指示中提供非常详细的信息,实际上最好是直接在代码中提供修复。
编辑:请不要对我的 cmets 笑得太厉害,我现在(经过多次谷歌搜索)更好地理解了代码,这是几个小时前的修订版。
双重编辑:这是一个名为 process.php 的示例 php 脚本,您需要对其进行编辑以查看自动提交是否有效。如上所述,您可以使用 MySQL 数据库执行此操作。
print "Form submitted successfully: <br>Your name is <b>".$_POST['name']."</b> and your email is <b>".$_POST['email']."</b><br>";
【问题讨论】: