PHP form values after POST in dropdown

Posted by FFish on Stack Overflow See other posts from Stack Overflow or by FFish
Published on 2010-03-24T01:07:59Z Indexed on 2010/03/24 1:13 UTC
Read the original article Hit count: 395

Filed under:
|
|
|
|

I have a form with 'selected' values pulled from the database.
Now I want the user to edit the values.
When the data is send I want to show the new values.

When I submit my form I always get the 'green' value?
What am I doing wrong here?

<?php
// pulled from db
$color = "blue";
// update
if (isset($_POST['Submit'])) {
    echo "write to db: " . $_POST['name'] . " + " . $_POST['color'];
}
?>

<html>
<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<label for="name">Name:</label>
<input type="text" name="name" size="30" value="<?php echo (isset($_POST['name'])) ? $_POST['name'] : ""; ?>">
<br />
<label for="color">Color:</label>
<select name="color">
    <option <?php echo (isset($_POST['color']) || $color == "red") ? 'selected="selected"' : ''; ?> value="red">red</option>
    <option <?php echo (isset($_POST['color']) || $color == "blue") ? 'selected="selected"' : ''; ?> value="blue">blue</option>
    <option <?php echo (isset($_POST['color']) || $color == "green") ? 'selected="selected"' : ''; ?> value="green">green</option>
</select>
<br />
<input type="submit" name="Submit" value="Update">
</form>
</html>

© Stack Overflow or respective owner

Related posts about php

Related posts about post