2013-02-10 70 views
0

我想创建一个表单页面,使用php和ajax jquery进行验证。
我从网上下载http://malsup.com/jquery/form/

表单页面的jQuery的:PHP与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> 

Uploadfile.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,并且如果用户填写了表单,页面将无处可用。
请帮我解决这个问题。

回答

0

在uploadfile.php,当一切OK,你可以输出一个JavaScript重定向:

{ 
    echo '<script type="text/javascript">window.location.replace("next.php");</script>'; 
} 

顺便说一句,请注意检查文件类型是不可靠的,因为它是由浏览器给定(这不是强制性的),并且很容易被伪造。一种更安全的方法来检查文件是否真的是一个映像,检查getimagesize不会失败。

+0

tmuguet,谢谢你的回复。 – 2013-02-11 14:37:11