douwen9345 2013-03-28 10:59
浏览 35

单选按钮数组获取选定值和相关内容(相同ID隐藏元素)

I'm having problems with extracting the data from a radio button form.

It looks like this:

<?php
## color
$stripe = 1;
$att_color=mysql_query("SELECT * FROM product_attributes WHERE products_id ='".$row['products_id']."' AND att_head='colors' AND att_show='1' ORDER BY att_order asc");
if(mysql_num_rows($att_color)!=0)
{
echo "
<table>
  <tr>
    <td width=\"90%\" align=\"left\" bgcolor=\"#EDEDED\">Color</td>
<td width=\"10%\" align=\"center\" bgcolor=\"#891531\">
<a href=\"#2\" title=\"Yta / Behandling\"><img src=\"images/info.png\"></a>
</td>
</tr>";

while($color_row=mysql_fetch_array($att_color)){
  if ($color_row['att_standard'] == 1) { $standard = "checked"; }
echo "
<tr class=".(($stripe++%2==1) ? 'odd' : 'even').">
  <td colspan=\"2\" width=\"100%\">
<input name=\"color_name[]\" value=\"".$color_row['att_name']."\" id=\"".$color_row['att_order']."\" type=\"hidden\" />
<input type=\"radio\" name=\"color_selected[]\" id=\"".$color_row['att_order']."\" value=\"".(round($color_row['att_val']*0.85))."\" ".$standard.">
<img src=\"images/colors/".$colorimg."\"> &nbsp;".$color_row['att_name']."</td>
 </tr>";
unset($standard); }
echo "</table>"; }
# END color
?>

Only ONE option is to be allowed. When someone hits submit I* need to get the value AND the corresponding name from that row. However I can't seem to figure the foreach syntax out.

if( isset($_POST['submit']) ) {   
    $c_selected = $_POST['color_selected'];
    $c_name = $_POST['color_name'];
    foreach($c_selected as $key => $n) {
    echo "<br><br>". $c_selected[$key] ." ".$c_name[$key];
 }

The current code gives me the correct value, but the wrong colorname. (always 1st option) How can I get this to work?

  • 写回答

1条回答 默认 最新

  • dongwo2222 2013-03-28 13:08
    关注

    This is broke:

    <input type=\"radio\" name=\"color_selected[]\" id=\"".$color_row['att_order']."\" value=\"".(round($color_row['att_val']*0.85))."\" ".$standard.">
    
     name=\"color_selected[]\" should not be an array. 
    

    Instead just

     name=\"color_selected\" will provide the chosen color.
    

    So the value of the radio button should be the color, not the cost of the item. You should make another hidden field with the value of the item, unless each color is a different price.

    I think the problem is more complicated because you are trying to send an order array rather than a simple form or something. If this is the case, maybe you have to rethink the way you are doing this.

    You seem to have a products id field, it should be unique for each color and can be used on the other side to get all the data values from your database.

    <input name=\"att_order\"
           value=\"".$color_row['att_order']."\" 
           type=\"hidden\" />
    <input type=\"radio\" 
           name=\"color_selected\" 
           value=\"".$color_row['products_id']."\" 
           ".$standard.">
    
    评论

报告相同问题?

悬赏问题

  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件
  • ¥15 不同系统编译兼容问题
  • ¥100 三相直流充电模块对数字电源芯片在物理上它必须具备哪些功能和性能?
  • ¥30 数字电源对DSP芯片的具体要求
  • ¥20 antv g6 折线边如何变为钝角
  • ¥30 如何在Matlab或Python中 设置饼图的高度
  • ¥15 nginx中的CORS策略应该如何配置
  • ¥30 信号与系统实验:采样定理分析
  • ¥100 我想找人帮我写Python 的股票分析代码,有意请加mathtao