簡體   English   中英

循環中的PHP POST選擇框未按正確順序顯示值

[英]PHP POST select boxes in loop not displaying values in correct order

我有一個復選框和一個選擇下拉菜單來選擇一個晚餐課程,該課程將循環顯示4個房間。 當我選擇多個房間時,將得到以下輸出:

大象房

三道菜

兩道菜

獅子室

三道菜

兩道菜

但是我想要這個輸出:

如果我為大象房選擇了兩道菜,我會這樣:

大象房

兩道菜

如果我為獅子房選擇三道菜,我想要這樣:

獅子室

三道菜

我試着通過與眾不同的方式以幾種方式修改了foreach循環。 我還不是循環專家,而且還沒有循環專家。

HTML

<?php while ( have_posts() ) : the_post(); ?>

<form name="book-room-form" action="" id="contactForm" method="post">

    <div>
        <label class="form-check-label selected-label" for="room-selected">Select</label>
        <input type="checkbox" class="form-check-input" id="room-selected" name="room-selected[]" value="<?php echo $room_name; ?>">
    </div>

    <select name="dinner-select[]" required>
      <option value="0" selected>Select Dinner Course</option>
      <option value="120">Two Course Dinner</option>
      <option value="200">Three Course Dinner</option>
    </select>

    <button name="submit-request" type="submit" class="btn btn-primary">Submit</button>

</form>

<?php endwhile; // end of the loop. ?>

PHP

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

    $room_selected = $_POST['room-selected'];
    $numPeople = $_POST['people-select'];
    $dinnerSelect = $_POST['dinner-select'];

    $roomoption = isset($room_selected) ? $room_selected : false;
    if ($roomoption) {
        foreach ($room_selected as $room){
            if($room){
                echo $room . '<br/>';

                $dinneroption = isset($dinnerSelect) ? $dinnerSelect : false;

                if ($dinneroption) {
                    foreach ($dinnerSelect as $dinn){
                        if($dinn >= 200){
                            echo 'three course <br/>';
                        }
                        elseif($dinn == 120) {
                            echo 'two course <br/>';
                        }
                    }
                }
            }
        }
    }
}

知道我該如何處理嗎?

似乎很奇怪,您將循環表單,並且在每個表單中僅包含單個文本輸入和單個選擇菜單-似乎更有意義的是,您將具有單個表單然后在輸入元素周圍循環,如下所示:

<!-- can leave the ID for the form as it is outside the loop -->
<form name="book-room-form" id="contactForm" method="post">

    <?php while ( have_posts() ) : the_post(); ?>

    <!--- to segregate the looped form elements -->
    <fieldset>

        <div>
            <label class="form-check-label selected-label" for="room-selected">Select</label>
            <!-- remove id attribute from input -->
            <input type="checkbox" class="form-check-input" name="room-selected[]" value="<?php $room_name; ?>">
        </div>

        <select name="dinner-select[]" required>
          <option value="0" selected>Select Dinner Course
          <option value="120">Two Course Dinner
          <option value="200">Three Course Dinner
        </select>

    </fieldset>

    <?php endwhile;?>

    <!-- a single "submit" button rather than however many were generated by the loop -->
    <button name="submit-request" type="submit" class="btn btn-primary">Submit</button>

</form>

我無法輕松測試其中的PHP部分,因此可能存在問題,但您可以對其進行一些修改

if( $_SERVER['REQUEST_METHOD']=='POST' && isset( $_POST['submit-request'], $_POST['room-selected'], $_POST['people-select'], $_POST['dinner-select'] ) ){

    $room_selected = filter_input( INPUT_POST, 'room-selected', FILTER_SANITIZE_STRING );
    $numPeople = filter_input( INPUT_POST, 'people-select', FILTER_SANITIZE_NUMBER_INT );
    $dinnerSelect = filter_input( INPUT_POST, 'dinner-select', FILTER_SANITIZE_NUMBER_INT );

    if( $room_selected & $numPeople & $dinnerSelect ){
        foreach( $room_selected as $room ){

            if( $room ){

                echo $room . '<br/>';

                foreach( $dinnerSelect as $value ){
                    switch( intval( $value ) ){
                        case 120:echo 'two course <br/>';break;
                        case 200:echo 'three course <br/>';break;
                    }
                }
            }
        }
    }
}

暫無
暫無

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

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