当我点击提交时,我希望将所有表单数据发布到process.php。然后在process.php上我想回显出POST数据,最后将结果div中的所有内容替换为process.php中的内容。
<script type="text/javascript">
$(document).ready(function(){
$("#myform").submit( function () {
$.ajax({
type: "POST",
dataType: "html",
cache: false,
url: "process.php",
success: function(data){
$("#results").html(data);
}
});
return false;
});
//$("#myform").submit( function () {
//$('#results').html("yay");
//}
// });
//} );
});
</script>
<form name="myform" id="myform" action="" method="POST">
<!-- The Name form field -->
<label for="name" id="name_label">zoom</label>
<input type="text" name="zoom" id="zoom" size="30" value=""/>
<br>
</select>
<!-- The Submit button -->
<input type="submit" name="submit" value="Submit">
</form>
<!-- FORM END ---------------------------------------- -->
<!-- RESULTS START ---------------------------------------- -->
<div id="results">nooooooo<?PHP $_SESSION[''] ?><div>
<!-- <input type="image" name="mapcoords" border="0" src="mapgen.php"> ---- -->
<!-- RESULTS END ---------------------------------------- -->
答案 0 :(得分:33)
您可以调用$.post
传递表格数据序列化。像这样:
<script type="text/javascript">
$(document).ready(function(){
$("#myform").submit( function () {
$.post(
'process.php',
$(this).serialize(),
function(data){
$("#results").html(data)
}
);
return false;
});
});
</script>
答案 1 :(得分:6)
$("#myform").submit( function () {
$.ajax({
type: "POST",
data : $(this).serialize(),
cache: false,
url: "process.php",
success: function(data){
$("#results").html(data);
}
});
return false;
});
测试
答案 2 :(得分:3)
只有另一种方法可以做到这一切。此示例使用jQuery validate 插件。在这里,所有表单字段都会自动验证。 从这里下载 jquery :
https://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js
从这里下载验证插件:
http://jquery.bassistance.de/validate/jquery-validation-1.10.0.zip
<script type="text/javascript" src="jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="jquery.validate.1.7.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#customForm").validate({
debug: false,
rules: {
name: {required:true,minlength:8,maxlength:8},
email: {required:true,email:true},
},
submitHandler: function(form) {
// do other stuff for a valid form
$('#rsltx').html('<img src="WhiteLoading.gif"> Processing... please wait');
$.post('post.php', $("#customForm").serialize(), function(data) {
$('#rsltx').html(data);
});
}
});
});
</script>
<form method="post" id="customForm" action="">
<div>
<label for="name">Name</label>
<input id="name" name="name" type="text" autocomplete="off" required/>
</div>
<div>
<label for="email">E-mail</label>
<input id="email" name="email" type="email" autocomplete="off" required/>
</div>
<div>
<input id="send" name="send" type="submit" value="Send" />
</div>
</form>