duangu4980 2014-09-12 17:29
浏览 54
已采纳

PHP POST返回Array而不是select选项的值?

I'm trying to allow users to select multiple items and post the selected items to the next page using PHP.

The process above works fine but the problem that I have right now is each item has a dropdown menu with some colours in it. the colours are the same.

what I need to do is to allow the users to select 1 colour for each selected item and send it to the next page.

This should be simple but for some reason all i get in the next page is "Array" instead of the value of selected option!

This is my code:

First page:

    $products_list .= '<div align="center" style="width:150px; height:100px; float:left; border:solid 1px #666; margin-left:20px; margin-bottom:10px;">
    <input style="float:left;" type="checkbox" name="check_list[]" value="'.$id.'" />
    <img width="67" src="../inventory_images/'.$id.'.jpg"  /><br />
    '.$product_name.'<br />
    <select name="colours[]">
    <option >Choose a Colour</option>
    <option value="Black">Black</option>
    <option value="White">White</option>
    <option value="Red">Red</option>
    <option value="Blue">Blue</option>
    </select>
    </div>';


echo $products_list;

and on the second page i have this:

if(isset($_POST['submit'])){//to run PHP script on submit
if(!empty($_POST['check_list'])){

// Loop to store and display values of individual checked checkbox.
$products_list = "";
foreach($_POST['check_list'] as $selected){

$colours = $_POST['colours'];


//MYSQL QUERIES ETC GOES HERE....

$products_list .= ''.$product_name.' and '.$colours.'';
}

echo $products_list ;

on the second page the $product_name for all the selected items gets echo-ed properly and correctly but the $colours show Array.

could someone please advise on this ?

Thanks

EDIT:

Please note that $_POST['colours']; is returning Array on the second page.

  • 写回答

1条回答 默认 最新

  • dpb56083 2014-09-12 17:44
    关注

    To keep the checkbox tied to the select you will need to hardcode the index numbers (1 in this example) since it is possible that a checkbox will not be checked and therefor not submitted.

    <input typ="checkbox" name="check_list[1]">
    <select name="colours[1]">
    <option >Choose a Colour</option>
        <option value="Black">Black</option>
        <option value="White">White</option>
        <option value="Red">Red</option>
        <option value="Blue">Blue</option>
    </select>
    

    Then just:

    foreach($_POST['check_list'] as $key => $selected) {
        echo $_POST['colours'][$key]; // use the key from the associated checkbox
    }
    

    If any of the selects will be a multiple select then you will need to do something like this:

    <select name="colours[1][]" multiple>
    <select name="colours[2]">
    

    Then:

    foreach($_POST['check_list'] as $key => $selected) {
        echo is_array($_POST['colours'][$key]) ? implode(',', $_POST['colours'][$key]) : $_POST['colours'][$key];
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算