1

How can I get the variables from $_POST if I used an array in the html form?

I know this isn't correct, but it's my best guess:

$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 Answer 1

3

1) You're using GET in your form, not POST. Change this:

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

to:

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

2) With the change above, you'll get your value (i.e. the selected $key in your case) in $_POST['choices']

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! How would I call the item and value separately?
You shouldn't have to submit the value of your select box... you could try obtaining the key you need in the server script, and load its associated value on the server script from the database again. This is also a security concern: what will happen if a user modifies your form and submits it with values that AREN'T in your database? You should never ever trust user input

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.