我已经设法使以下代码能够使用 AJAX 提交表单并更新我网站的结果 div,它工作正常......
<script type="text/javascript">
$(document).ready(function(){
$("#myform").validate({
debug: false,
rules: {
guess: {
min: 0,
number: true
}
},
messages: {
guess: "Please enter a valid guess.",
},
submitHandler: function(form) { // do other stuff for a valid form
$.post('process.php', $("#myform").serialize(), function(data) {
$('#results').html(data);
});
}
});
});
</script>
如果我有另一个表单要发送到更新网页的不同部分的“process2.php”,我是否正确地认为我需要有相同脚本的另一个副本,但有效地调用我的第二个表单“form2”并更改#myform --> #myform2 和#results --> #new_div ??
我已经尝试让它在一个全新的页面上工作,当我要求它从 process2.php 打印一些东西到屏幕时它工作正常,但是当我尝试让它对传递的变量做一些事情时(在这种情况下,姓名和电子邮件)它甚至不再打印 php 的结果。
我的 process.php 将猜测(一个数字)与 member_id(也是一个数字)一起插入到猜测数据库中..
我不知道我做错了什么。似乎是当我在我的 process2.php 中包含 wp_blog_header.php 时(但是该 header 在 process.php 中工作正常).. 如果我不包含该行,我与数据库的连接将失败并且我无法插入到表...
我不知道我还能做什么或我需要什么额外的信息?
我的新表格是
姓名 邮箱
我的新 javafunction 变成了
script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery.validate/1.7/jquery.validate.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#myform2").validate({
debug: false,
rules: {
name: "required",
email: {
required: true,
email: true
}
},
messages: {
name: "Please let us know who you are.",
email: "A valid email will help us get in touch with you.",
},
submitHandler: function(form) {
// do other stuff for a valid form
$.post('process2.php', $("#myform2").serialize(), function(data) {
$('#results').html(data);
});
}
});
});
</script>
这是我的编码问题,还是与我的 process2.php(几乎与我的 process.php 完全相同)有关,除了我要插入到不同的数据库表中,一个名称和一个电子邮件...但它似乎不起作用...
所以 PHP 中的代码就像 "INSERT into 'new_table' ('name', 'email') VALUES (". $_POST['name'] ","$_POST['email'] . ")";
但只要我有线路
include(wp-blog-header.php) 它似乎折叠了......嗯嗯 - 我迷路了:(
最佳答案
如果它在没有 AJAX 的情况下工作,这个插件可以非常容易地将普通表单转换为使用 AJAX:http://jquery.malsup.com/form/
关于jquery - AJAX/jQuery 表单提交困难,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8926788/