繁体   English   中英

用php数组进行表单验证

[英]Form validation with a php array

我希望有人可以帮助我完成一些php代码(雅芳家伙已经很好地帮助了我,但是我仍然在最后一刻苦苦挣扎)。

就是这样,我有一个表格,其中有10个特定的数字序列,如果输入这些序列,则允许该表格重定向到下一页。 如果输入了其他任何内容,我希望页面拒绝访问并显示某种错误提示。

在php的顶部,在打印任何php之前的部分中,avon guy建议使用数组检查10个正确的序列。

$possibles = array('rva858', 'anothersequence', 'andanother'); 
$match = $_POST['nextpage']; 
if (array_search($match, $possibles) != false) { 
    //match code in here 
} else { 
    // fail code in here 
}

我不确定在这里将// match代码和在这里// fail代码放入什么,位。 最后有人可以帮我吗?

非常感谢

乔恩

如果您只是尝试使用php重定向到另一个页面,则可以使用header('Location: mypage.php'); 有关标题的更多信息,请参见此处

因此,对于您的代码示例(根据评论进行编辑)

invitation.php

<?php
//invitation.php
$possibles = array('rva858', 'anothersequence', 'andanother');
$match = $_POST['nextpage']; 
if (array_search($match, $possibles) === false) 
{  
    //If fail
    header('Location: formpage.php?errorMessage=Incorrect code!');
    exit();
} 
//If success:
//All of the invitation.php html and success code below

formpage.php

<?php
//formpage.php
if(!empty($_GET['errorMessage'])){
    echo '<span>' . $_GET['errorMessage'] . '</span>';
}
?>
<form action="invitation.php" method="post">
    <input name="rsvp" type="text" />
    <input type="submit" value="Submit" name="submit" />
</form>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM