douya2007 2015-03-27 01:47
浏览 34
已采纳

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

New guy here - I have three choices and want to let the user only pass to the next page "4.html" if they select A, else send them to google.com. This is where I've gotten so far :(

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>

I really appreciate your guys' help!

  • 写回答

1条回答 默认 最新

  • dr6673999 2015-03-27 02:42
    关注

    Sidenote: You can't use echo and header together, otherwise you will be outputting before header.

    Consult the link following this (footnotes) and is intended to be run inside the same file:

    Checkbox method: (which differs from the radio buttons below)

    <?php 
    
    if(isset($_POST['submit'])){
    
    if(isset($_POST['choice'])){
    
      foreach($_POST['choice'] as $choice){
    
        if($choice == "A"){
         echo "You chose A" . "
    ";
       // header('Location: /4.html');
        // exit;
    
        }
    
        if($choice == "B"){
        echo "You chose B" . "
    ";
        }
    
        if($choice == "C"){
        echo "You chose C" . "
    ";
        }
    
    
        } // 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>
    

    Radio buttons method: (edited) and added a name attribute to the submit button. Sidenote: </input> isn't a valid tag; it's been removed.

    <?php 
    
    $choice = $_POST['choice'];
    
    if(isset($_POST['submit'])){
    
        if($choice == "A"){
        // echo "You chose A" . "
    ";
         header("Location: /4.html");
         exit; // stop further execution
        }
    
        if($choice == "B"){
        echo "You chose B" . "
    ";
        }
    
        if($choice == "C"){
        echo "You chose C" . "
    ";
        }
    
    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>
    

    Footnotes:

    See the following on the subject of outputting before header:

    Also, you can use radio buttons for single choices and without the array.


    If you're faced with errors/notices/warnings, somewhere down the line:

    Add error reporting to the top of your file(s) which will help find errors.

    <?php 
    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    
    // rest of your code
    

    Sidenote: Error reporting should only be done in staging, and never production.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?