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 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?