2011-08-15 39 views
-3

在我的网站上,我正在做服务器端的表单检查。当一个意外的输入来自用户时,我想将表单发回来。我在php.net上发现了一些用户定义的类和函数,但它们不起作用。任何人都可以帮我解释一下吗?PHP发布表格回头

假设我有一个addCompany.php页面,它有一个表单。我有第二页action.phpaddCompany.php中的表格发送至action.php页面。我查看表单进行验证,发现错误并且我想将表单发回到addCompany.php。通常我可以通过querystring发送它,但由于字符限制它不安全。因此,我希望在重定向时寄回表格header('Location:addCompany.php')

回答

0

通常的过程是在同一页面上处理表单,如果验证成功,您重定向到下一个阶段,否则继续回到表格。

这里有一个pseudocodish例如:

<?php 
// form.php 

if(isset($_POST[ 'submit' ]) && form_validates($_POST)) { 
    // do what you want with the data 
    // redirect to the next stage 
    header('Location: next.php'); 
    die(); 
} 

// didn't validate or no submit yet, show the form again 

if(isset($_POST[ 'email' ])) { 
    $email = $_POST[ 'email' ]; 
} 
else { 
    $email = ''; 
} 
?> 
<form action="form.php"> <!-- submit to same page --> 
<input type="text" name="email" value="<?php echo $email; ?>" /> 
<?php if(/* email field didn't validate */) { 
    echo 'Please enter a valid email address'; 
} ?> 
<input type="submit" name="submit" /> 
</form> 
+0

可以再次阅读问题我已经编辑。我认为有一个误解 –

+0

对,我的建议是,你将action.php和addCompany.php结合到一个脚本中。这样,您就不必考虑验证错误后重定向页面,因为您已经在那里;让脚本再次显示表单。 – JJJ

+0

我的系统并不那么简单,我无法按照您的建议修改我的系统。 –