从具有动态元素名称的表单中检索post值

时间:2013-11-14 10:46:43

标签: php mysql html5 forms

我正在尝试从一个表单中检索post值,其中元素名称基于记录集中的值。这是表格:

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


?>

每种产品可以有任意数量的颜色。任何有关使用每种颜色/尺寸选择数量来显示下一页的值的帮助都将非常受欢迎。

感谢

3 个答案:

答案 0 :(得分:1)

首先我会改变

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

答案 1 :(得分:0)

您将PHP代码放在一个字符串中,它不会被PHP解析:

echo '<input name="xs_<?php echo $colour_row[colour_name]; ?>">';

这应该像这样连接:

echo '<input name="xs_' . $colour_row[colour_name] . '">';

同样如此
echo '<input id="xs_<?php echo $row[colour_name]; ?>">';

需要转换为:

echo '<input id="xs_' . $row[colour_name] . '">';

输入字段的id属性中“$ row”可能必须是“$ colour_row”吗?

还有一些其他明显的改进,但这些改进超出了你的问题范围。


我正在关闭我的Netbeans并发现它仍然坐在那里,我希望它有用(不是100%保证可以开箱即用):

表单php将成为:

<?php
$recordID = $_GET["recordID"];

// I adjusted the query somewhat.
// The colours tabel is joined with a USING() command
// The recordId is escaped
$colour_result = mysqli_query($con, "
    SELECT colour_variation.*, colours.*
    FROM colour_variation INNER JOIN colours USING(colour_id)
    WHERE product_code = '". mysqli_escape_string($con, $recordID)."'
");

// I'm using an alternative syntax for the while control structure ( I am
// using a colon instead of braces ):
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>
        <!-- I'm using an alternative syntax for the if control structures here -->
        <!-- I also left out the else, since it did not print anything -->
        <!-- I used an array for the name notation, to easily read out the data -->
        <td><center><?php if ($colour_row['xs'] !== ''): ?><input type="text" size="2" name="quantity[<?php echo $colour_row['colour_name']; ?>][xs]" placeholder="Qty"><?php endif; ?> </center></td>
        <td><center><?php if ($colour_row['s'] !== ''): ?><input type="text" size="2" name="quantity[<?php echo $colour_row['colour_name']; ?>][s]" placeholder="Qty"><?php endif; ?> </center></td>
        <td><center><?php if ($colour_row['m'] !== ''): ?><input type="text" size="2" name="quantity[<?php echo $colour_row['colour_name']; ?>][m]" placeholder="Qty"><?php endif; ?> </center></td>
        <td><center><?php if ($colour_row['l'] !== ''): ?><input type="text" size="2" name="quantity[<?php echo $colour_row['colour_name']; ?>][l]" placeholder="Qty"><?php endif; ?> </center></td>
        <td><center><?php if ($colour_row['xl'] !== ''): ?><input type="text" size="2" name="quantity[<?php echo $colour_row['colour_name']; ?>][xl]" placeholder="Qty"><?php endif; ?> </center></td>
        <td><center><?php if ($colour_row['xxl'] !== ''): ?><input type="text" size="2" name="quantity[<?php echo $colour_row['colour_name']; ?>][xxl]" placeholder="Qty"><?php endif; ?> </center></td>
        <td><center><?php if ($colour_row['xxxl'] !== ''): ?><input type="text" size="2" name="quantity[<?php echo $colour_row['colour_name']; ?>][xxxl]" placeholder="Qty"><?php endif; ?> </center></td>
        <td><center><?php if ($colour_row['one_size'] !== ''): ?><input type="text" size="2" name="quantity[<?php echo $colour_row['colour_name']; ?>][one_size]" placeholder="Qty"><?php endif; ?> </center></td>
    </tr>
<?php endwhile; ?>
</table>
<input type="hidden" name="product_code" value="<?php echo $colour_row_products['product_code']; ?>" >

接收端将是这样的:

<?php

    $iProductCode = $_POST['product_code'];

    foreach($_POST['quantity'] as $sColor => $aColourQuantityData) {
        foreach($aColourQuantityData as $sSize => $iQuantity) {
            echo "Product #$iProductCode, colour $sColor, size $sSize: $iQuantity pieces<br>";
        }
    }

?>

答案 2 :(得分:0)

好的,我现在使用以下代码在第二页上显示为列表:

<td><center><?php if($colour_row['xxl'] !== '') { echo('<input type="text" size="2" name="xxl_' . $colour_row['colour_name'] . '" placeholder="Qty">');
 } else { echo(''); } ?> </center></td>

然后:

<?php 
$product_code=$_POST["product_code"];

foreach($_POST as $key=>$value)
{
  echo "$key=$value";
  echo "<br />";
}
?>

在第二页。

谢谢大家的帮助,你的建议让我走上正轨,非常感谢。