如果HTML表单使用数组,如何从$ _POST获取变量?

时间:2013-06-18 13:49:53

标签: php html forms

如果我在html表单中使用数组,如何从$ _POST获取变量?

我知道这不正确,但这是我最好的猜测:

$ item = $ _POST [$ key]; $ price = $ _POST [$ value];

<?php
// array begins

$database = array(
    'Sportscar'         => array(
            'price'     => 11.95,
            'shipping'  => 0.4,
            'ID'        => 1),

    'Diamonds'      => array( 
            'price'     => 44.99,
            'shipping'  => 0.10,
            'ID'        => 4),
            );

?>
<p>Select an item from the list.</p>

<form method="GET" action="add.php">

<select name="choices">

<? while(list($key,$value) = each($database)) {
echo "<option value='$key'>" . $key . " - " ; 

while(list($key,$value) = each($value)) { 
    echo money_format("$%i", $value);

echo "</option>"; }} echo "</select>

<input type='submit' value='Add to cart' />
</form>
?>

1 个答案:

答案 0 :(得分:2)

1)您在表单中使用GET,而不是POST。改变这个:

<form method="GET" action="add.php">

为:

<form method="POST" action="add.php">

2)通过上述更改,您将在$ _POST ['choices']中获得您的价值(即您选择的$ key)