正则表达式验证一个字母多项选择

Regex Validate One Letter Multiple Choice

这里的新人 - 我有三个选择,想让用户只传递到下一页“4.html”,如果他们 select A,否则将它们发送到 google.com .这就是我到目前为止的进展:(

if(empty($_POST['choice'])){
    echo "Please select at least one choice..!!";
    //this should send them to google.com if they select none or the wrong one
}
else{
     foreach($_POST['choice'] as $choice){
        header('Location: /4.html');
    }
}


<form action="multichoice.php" method="post">
    <input id="checkbox" type="checkbox" name="choice[]" value="A" />A&#x29;Choice A
    <input id="checkbox" type="checkbox" name="choice[]" value="B" />B&#x29;Choice B
    <input id="checkbox" type="checkbox" name="choice[]" value="c" />C&#x29;Choice C
<input id="input"onclick="return myFunction()"  type="submit" value="Submit"></input>
</form>

非常感谢你们的帮助!

旁注:不能同时使用echo和header,否则会在header之前输出。

请参阅此(脚注)之后的 link 并打算 运行 在同一文件中:

复选框方法:(不同于下面的单选按钮)

<?php 

if(isset($_POST['submit'])){

if(isset($_POST['choice'])){

  foreach($_POST['choice'] as $choice){

    if($choice == "A"){
     echo "You chose A" . "\n";
   // header('Location: /4.html');
    // exit;

    }

    if($choice == "B"){
    echo "You chose B" . "\n";
    }

    if($choice == "C"){
    echo "You chose C" . "\n";
    }


    } // brace for foreach

} // brace for if(isset($_POST['choice']))

    // else for if(isset($_POST['choice']))
    else{
    echo "Please make a choice.";
    }

} // brace for if(isset($_POST['submit']))

?>

<form action="" method="post">
    <input id="checkbox" type="checkbox" name="choice[]" value="A" />A&#x29;Choice A
    <input id="checkbox" type="checkbox" name="choice[]" value="B" />B&#x29;Choice B
    <input id="checkbox" type="checkbox" name="choice[]" value="C" />C&#x29;Choice C
<input id="input" onclick="return myFunction()" name="submit"  type="submit" value="Submit">
</form>

单选按钮方法:(已编辑)并向提交按钮添加了名称属性。旁注:</input> 不是有效标签;它已被删除。

<?php 

$choice = $_POST['choice'];

if(isset($_POST['submit'])){

    if($choice == "A"){
    // echo "You chose A" . "\n";
     header("Location: /4.html");
     exit; // stop further execution
    }

    if($choice == "B"){
    echo "You chose B" . "\n";
    }

    if($choice == "C"){
    echo "You chose C" . "\n";
    }

if(empty($_POST['choice'])){

     header("Location: http://www.google.com/");
     exit; // stop further execution
}

} // submit conditional

?>

<form action="" method="post">
    <input id="radio" type="radio" name="choice" value="A" />A&#x29;Choice A
    <input id="radio" type="radio" name="choice" value="B" />B&#x29;Choice B
    <input id="radio" type="radio" name="choice" value="C" />C&#x29;Choice C
<input id="input" onclick="return myFunction()" name="submit" type="submit" value="Submit">
</form>

脚注:

header前输出的问题见下面:

  • How to fix "Headers already sent" error in PHP

此外,您可以使用单选按钮进行单选,而无需数组。


如果您遇到 errors/notices/warnings,那么下面的某个地方:

error reporting 添加到您的文件的顶部,这将有助于查找错误。

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

旁注:错误报告只应在试运行中进行,绝不能在生产中进行。