簡體   English   中英

如何在PHP中插入多個HTML清單值?

[英]How to insert multiple HTML checklist values into PHP?

我試圖在if-else條件下插入多個清單值。 即使我檢查多個值,我似乎也只能在條件中插入一個值。 我的代碼是:

<?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>

如您所見,我試圖將所有值都當作“ batbwl”,因為如果我執行“ echo $ selected”,這就是回聲。 但它只會傳遞bwl到條件上。 誰能幫我解決這個問題?

不要在這種情況下使用。 嘗試這個

<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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM