gpt4 book ai didi

php - 从具有动态元素名称的表单中检索帖子值

转载 作者:行者123 更新时间:2023-11-29 13:27:11 25 4
gpt4 key购买 nike

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

$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']; ?>" >

我尝试了这个方法来将它们取出,但它不起作用:

<?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;
}


?>

每个产品可以有任意数量的颜色。如果您能帮助我们将值显示在下一页上,并提供每种颜色/尺寸选择的数量,我们将不胜感激。

谢谢

最佳答案

首先我会改变

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 );
}
}

关于php - 从具有动态元素名称的表单中检索帖子值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19975624/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com