【问题标题】:Creating a conditional redirect PHP form创建条件重定向 PHP 表单
【发布时间】:2014-10-30 17:46:55
【问题描述】:

我正在尝试创建一个表单,确认用户已输入允许的电子邮件地址之一,然后将用户重定向到页面“NewPost.php”;否则(如果用户没有输入允许的电子邮件地址之一),他们将停留在此页面上。目前,如果我更改,我可以将主菜传递到下一页

<form action="Verify.php">

<form action="NewPost.php"> 

但这忽略了条件。形式如下:

<form action="Verify.php" method="post" name="VerifyEmail" id="VerifyEmail">
<p>Please enter your student email address</p>
<p> <input type="text" name="Email" size="40" maxlength="40" required/></p>
<p><input type="submit" name="Post" value="Post"/></p>
</form>

还有 PHP; $allowed 变量是学校的数组,其 .edu 电子邮件地址应满足允许用户输入其电子邮件地址重定向到 NewPost.php 的条件

<?php
$Email = trim($_POST['Email']);
$allowed = array ('columbia.edu', 'nyu.edu', 'fitnyc.edu', 'pratt.edu', 'newschool.edu', 'stjohns.edu', 'cooper.edu', 'manhattan.edu', 'pace.edu', 'bard.edu', 'berkeleycollege.edu', 'qc.cuny.edu');
$split = explode($Email, "@");
    $name = $split['0'];
    $school = $split['1'];
if ($school == $allowed) {
header('Location: /NewPost.php');
}
else{echo "<p>Sorry your school is not supported yet</p>";}
?>

为了澄清,我希望仅当他们的电子邮件地址在 $allowed 数组中时,输入他们的电子邮件地址的访问才被重定向到 NewPost.php,否则他们会卡在当前页面上(验证.php)。 如果有人想自己尝试,地址是here

谢谢!

【问题讨论】:

    标签: php forms redirect conditional


    【解决方案1】:

    您正在将字符串与数组进行比较。你不能那样做。您需要查看该字符串是否在 in array 中。使用in_array()isset()

    if (in_array($school, $allowed)) {
    

    if (isset($allowed[$school])) {
    

    【讨论】:

    • 确保这些变量中的值符合您的预期。一个前导或尾随的空白字符会抛出一些东西。还要去掉数组键周围的引号: $name = $split[0]; $school = $split[1];
    • TRIM 应该处理空白区域吗?不幸的是,它仍然无法正常工作。
    • 您还有一个错误。您正在使用参数向后调用explode。 $split = explode("@",$Email);
    猜你喜欢
    • 2014-02-07
    • 2013-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多