Show selected value in dropdownbox after submit

Discussion in 'PHP' started by MSK7, Jun 5, 2009.

  1. #1
    Hello all,

    I have a page, named "Submit.php"

    in which there is a form called "form1" & its action ="Submit.php" .


    The form contains textbox for text , and dropdownbox for list.


    i used variables to store the entered values in thier fields as -

    $name = $_POST['name'] ,

    $age = $_POST['age'],

    $city = $_POST['city'],

    then i used this variables to show their values in their fields.

    using echo as-

    <input type = "text" name ="name" class="footerlink" value=" <? echo $name ?>" >

    <input type = "text" name ="age" class="footerlink" value=" <? echo $age ?>" >

    The dropdownbox is used to select the name of city.

    <select name="city" class="footerlink" value ="<? echo $city ?>" >

    <option value="">Please Select City</option>

    <option value="" selected="selected"></option>

    <option value="1">London</option>

    <option value="2">Paris</option>

    <option value="3">New York</option>

    </select>

    I have also used some other queries to check errors & after submit return

    the page with the error message .

    The error message works perfactly after submit if there is error in the field.

    The problem is that After Submit when error message is displayed

    the values entered in the text boxes before submit are shown in the

    text box well as it was entered,

    But dropdownbox does not save and show the value which was selected

    before submit.

    How can i keep the dropdown value also remain selected after submit when

    error message is returned , as it which was choosed before submit .

    please suggest any solution regarding these.

    Thanks & Regards.
     
    MSK7, Jun 5, 2009 IP
  2. givemeknol

    givemeknol Active Member

    Messages:
    22
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    88
    #2
    Try:

    <select name="city" class="footerlink" value ="<? echo $city ?>" >

    <option value="">Please Select City</option>

    <option value="" <?php if($_POST['city'] == '') echo 'selected="selected"' ?>></option>

    <option value="1" <?php if($_POST['city'] == '1') echo 'selected="selected"' ?>>London</option>

    <option value="2" <?php if($_POST['city'] == '2') echo 'selected="selected"' ?>>Paris</option>

    <option value="3" <?php if($_POST['city'] == '3') echo 'selected="selected"' ?>>New York</option>

    </select>
     
    givemeknol, Jun 5, 2009 IP
  3. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #3
    This would work (getting numeric value/city name from an array) (could just as easily been from a database):

    
    <form method="post" action="">
    <?php
    echo "<select name=\"city\" class=\"footerlink\">";
    $city_name = array(1 => 'London', 2 => 'Paris', 3 => 'New York');
    foreach ($city_name as $key => $value) {
    if ($key == $_POST['city']){
    $selected = "selected=\"selected\""; } else { $selected = ""; }
    echo "<option value=\"$key\" $selected>$value</option>";
    }
    echo "</select>";
    ?>
    <input type="submit" name="submit" value="Enter" />
    </form>
    
    PHP:
     
    PoPSiCLe, Jun 5, 2009 IP