我想使用 php 和 ajax jquery 创建一个带有验证的表单页面。我从http://malsup.com/jquery/form/
表单页面
下载了 jquery :
<html>
<head>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/jquery.form.js"></script>
<script type="text/javascript">
function checkAll(){
var errmsg = ""
document.getElementById("errmsg").style.display = "none";
if(document.getElementById("txtbox1").value == ""){
errmsg += "Type your title<br/>";
}
if(document.getElementById("file1").value == ""){
errmsg += "Upload your picture<br/>";
}
if(errmsg != ""){
document.getElementById("errmsg").style.display = "block";
document.getElementById("errmsg").innerHTML = errmsg;
return false;
}
}
$(document).ready(function() {
var options = {
target: '#div2', //Div tag where content info will be loaded in
url:'uploadfile.php', //The php file that handles the file that is uploaded
beforeSubmit: checkAll,
success: function() {
//Here code can be included that needs to be performed if Ajax request was successful
}
};
$('#Form1').submit(function() {
$('#Form1').ajaxSubmit(options);
return false;
});
});
</script>
</head>
<body>
<span id="errmsg"></span>
<div id="div2"></div>
<form name="Form1" id="Form1" method="post" action="next.php" enctype="multipart/form-data">
Title <input type="text" name="txtbox1" id="txtbox1" />
Picture <input type="file" name="file1" id="file1" /><br/>
<input type="submit" value="Submit" />
</form>
</body>
</html>
上传文件.php:
<?php
if (((@$_FILES["file1"]["type"] == "image/gif") || (@$_FILES["file1"]["type"] == "image/png")
|| (@$_FILES["file1"]["type"] == "image/jpeg")
|| (@$_FILES["file1"]["type"] == "image/pjpeg"))
&& (@$_FILES["file1"]["size"] < 30720))
{
}
else{
echo "Check your file";
}
?>
我的问题:
我想为每个表单组件创建验证,包括文件上传(文件类型和大小检查),完成表单后,用户将被重定向到 next.php(参见表单操作)。
由于使用 ajax jquery 验证文件上传,操作更改为 uploadfile.php,如果用户完成表单,页面将无处可去。
请帮我解决这个问题。