简体   繁体   中英

How to insert multiple HTML checklist values into PHP?

I am trying to insert multiple checklist values into an if-else condition. I seem to only get a single value inserted in the condition even if I check multiple values. My code is:

<?php

    $db_host = 'localhost'; // Server Name
    $db_user = 'root'; // Username
    $db_pass = ''; // Password
    $db_name = 'assign'; // Database Name

    $conn = mysqli_connect($db_host, $db_user, $db_pass, $db_name);
    if (!$conn) {
        die ('Failed to connect to MySQL: ' . mysqli_connect_error());  
    }
?>


<html>

<body>
    <div class="form-style-8">
    <center><h2>Make a Choice</h2></center>
    <form action="#" method="post">
    <input type="checkbox" name="choice[]" value="bat">Batting</input>
    <input type="checkbox" name="choice[]" value="bwl">Bowling</input>
    <input type="checkbox" name="choice[]" value="fld">Fielding</input>
    <input type="submit" name="submit" value="Submit"/>
    </form>
    </div>
</body>


<?php
    if(isset($_POST['submit'])){
        if(!empty($_POST['choice'])){
            foreach($_POST['choice'] as $selected){
                if($selected == "bat"){
                    header('Location: Batting.php');
                }
                else if($selected == "bwl"){
                    header('Location: Bowling.php');
                }
                else if($selected == "fld"){
                    header('Location: Fielding.php');
                }
                else if($selected == "batbwl" || $selected == "bwlbat"){
                    header('Location: BatBwl.php');
                }
            }
        }
    }
?>

</html>

As you can see, I am trying to get to values together as "batbwl" as that is what echoes if I do "echo $selected;" but its only passing only bwl onto the condition. Can anyone help me fix this?

Don't use for such situations. try this

<html>

<body>
    <div class="form-style-8">
    <center><h2>Make a Choice</h2></center>
    <form action="#" method="post">
    <input type="checkbox" name="choice[]" value="bat">Batting</input>
    <input type="checkbox" name="choice[]" value="bwl">Bowling</input>
    <input type="checkbox" name="choice[]" value="fld">Fielding</input>
    <input type="submit" name="submit" value="Submit"/>
    </form>
    </div>
</body>


<?php
    if(isset($_POST['submit'])){
        if(!empty($_POST['choice'])){
                if((@$_POST['choice'][0] == "bat") && (@$_POST['choice'][1] == "bwl")){
                    header('Location: BatBwl.php');
                }
                else if($_POST['choice'][0] == "bwl"){
                    header('Location: Bowling.php');
                }
                else if($_POST['choice'][0] == "fld"){
                    header('Location: Fielding.php');
                }
                else if($_POST['choice'][0] == "bat"){
                    header('Location: Batting.php');
                }
        }
    }
?>

</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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