从具有动态元素名称的表单中检索帖子值 [英] retrieving post values from form with dynamic element names

查看:63
本文介绍了从具有动态元素名称的表单中检索帖子值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从表单中检索发布值,其中元素名称基于记录集中的值.形式如下:

I'm trying to retrieve the post values from a form where the element names are based on values from a recordset. Here is the form:

$recordID = $_GET["recordID"];
$colour_result = mysqli_query($con,"SELECT colour_variation.*, colours.* FROM colour_variation INNER JOIN colours ON colour_variation.colour_id=colours.colour_id WHERE product_code='$recordID'");
while($colour_row = mysqli_fetch_array($colour_result))
  {
 ?> 
<tr><td valign="middle"><img src="resources/images/colours/<?php echo $colour_row['colour_image']; ?>" width="35" height="35"></td><td width="100"><?php echo $colour_row['colour_name']; ?></td>
    <td><center><?php if($colour_row['xs'] !== '') { echo('<input type="text" size="2" name="xs_<?php echo $colour_row[colour_name]; ?>" id="xs_<?php echo $row[colour_name]; ?>" placeholder="Qty">');
     } else { echo(''); } ?> </center></td>
    <td><center><?php if($colour_row['s'] !== '') { echo('<input type="text" size="2" name="s_<?php echo $colour_row[colour_name]; ?>" id="s_<?php echo $row[colour_name]; ?>" placeholder="Qty">');
     } else { echo(''); } ?> </center></td>
    <td><center><?php if($colour_row['m'] !== '') { echo('<input type="text" size="2" name="m_<?php echo $colour_row[colour_name]; ?>" id="m_<?php echo $row[colour_name]; ?>" placeholder="Qty">');
     } else { echo(''); } ?> </center></td>
    <td><center><?php if($colour_row['l'] !== '') { echo('<input type="text" size="2" name="l_<?php echo $colour_row[colour_name]; ?>" id="l_<?php echo $row[colour_name]; ?>" placeholder="Qty">');
     } else { echo(''); } ?> </center></td>
    <td><center><?php if($colour_row['xl'] !== '') { echo('<input type="text" size="2" name="xl_<?php echo $colour_row[colour_name]; ?>" id="xl_<?php echo $row[colour_name]; ?>" placeholder="Qty">');
     } else { echo(''); } ?> </center></td>
    <td><center><?php if($colour_row['xxl'] !== '') { echo('<input type="text" size="2" name="xxl_<?php echo $colour_row[colour_name]; ?>" id="xxl_<?php echo $row[colour_name]; ?>" placeholder="Qty">');
     } else { echo(''); } ?> </center></td>
    <td><center><?php if($colour_row['xxxl'] !== '') { echo('<input type="text" size="2" name="xxxl_<?php echo $colour_row[colour_name]; ?>" id="xxxl_<?php echo $row[colour_name]; ?>" placeholder="Qty">');
     } else { echo(''); } ?> </center></td>
    <td><center><?php if($colour_row['one_size'] !== '') { echo('<input type="text" size="2" name="one_size_<?php echo $colour_row[colour_name]; ?>" id="one_size_<?php echo $row[colour_name]; ?>" placeholder="Qty">');
     } else { echo(''); } ?> </center></td>

    </tr>
    <?php } ?>
</table>
<input type="hidden" name="product_code" value="<?php echo $row_products['product_code']; ?>" >

我试图通过这种方法将它们释放出来,但无法正常工作:

I tried this to get them out but its not working:

<?php 
$product_code=$_POST["product_code"];
 foreach ($_POST as $key => $value) {
        if (substr($key, 0, 2) == "xs_") {
            $colour_name[str_replace("xs_", "", $key)] = $value;
        }
        echo $key;
        echo '<br />';
        echo $value;
    }


?>

每种产品可以有任何数量的颜色.对于将值显示在下一页并按颜色/尺寸选择数量的任何帮助,将不胜感激.

there could be any number of colours per product. Any help on getting the values to display on next page with the qty selected per colour/size would be much appreciated.

谢谢

推荐答案

首先我要更改

name="s_<?php echo $colour_row[colour_name]; ?>"

等到

name="attributes[s_<?php echo $colour_row[colour_name]; ?>]"

并使用以下PHP

if( !empty($_POST['attributes']) ) {
    foreach( $_POST['attributes'] as $sKey => $iQty ) {
        var_dump( $sKey );
        var_dump( $iQty );
    }
} else {
    die( 'Just for debuging. attributes-array was empty' );
}

甚至更好

使用

name="attributes[xxl][color]" eg. name="attributes[xxl][<?php echo $colour_row[colour_name]; ?>]"

还有

if( !empty($_POST['attributes']) ) {
    foreach( $_POST['attributes'] as $sSize => $aData ) {
        var_dump( $sSize );
        var_dump( $aData );
    }
}

这篇关于从具有动态元素名称的表单中检索帖子值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆