MCG
MCG

Reputation: 124

Retain select and multiple select array values on form POST

I am missing something obvious I'm sure, but I cannot get select values and multiple select values to be retained on form POST, when the form is submitted to itself.

Here is the basic form:

    <?php
    // Setup Fields, Initially Empty
    $first_name = $last_name = $age = $fruit = "";

    // Start session
    session_start();

    // Register
    session_register('first_name');
    session_register('last_name');
    session_register('age');
    session_register('fruit');

    // Populate
    $_SESSION['first_name'] = $first_name;
    $_SESSION['last_name'] = $last_name;
    $_SESSION['age'] = $age;
    $_SESSION['fruit'] = $fruit;

    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
        </head>
        <body>
            <div>

            <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" name="submit_data" method="POST" id="submit_data">

                        <p><label for="first_name">First Name</label><input type="text" id="first_name" size="20" maxlength="100" value="<?php echo $first_name;?>" name="first_name" />
                        </p>

                        <p><label for="last_name">Last Name</label><input type="text" id="last_name" size="20" maxlength="100" value="<?php echo $last_name;?>" name="last_name" />
                        </p>

                        <p><label for="age">Are you:</label>
                            <select id="age" name="age">
                                <option value=""></option>
                                <option value="0">Under 50</option>
                                <option value="1">50+</option>
                            </select>
                        </p>

                        <p><label for="fruit">Pick some fruit</label>
                            <select id="fruit" name="fruit[]" multiple="multiple">
                                <option value="Apple">Apple</option>
                                <option value="Banana">Banana</option>
                                <option value="Orange">Orange</option>
                            </select>
                        </p>

                <input type="submit" name="submit" value="Submit" />
            </form>
            </div>
        </body>
    </html>

The "age" select is a single select option, where as the "fruit" select is a multiple select list (which also is an array, hence the "fruit[]" name).

If the form is submitted, I want the select values to be selected on page refresh, in the same way as the first name and last name fields are, via the PHP session.

I'm sure there is a method along the lines of the following, but can't get it to work:

    if (isset($_POST['fruit'])){
        $chosen = $_POST['fruit'];
        foreach($chosen as $selected)
            echo 'selected'.$selected;
    }

Please can someone point me in the right direction for this basic form requirement?

Thanks

Upvotes: 1

Views: 5275

Answers (2)

Sean
Sean

Reputation: 12433

Inline you would do something like

// for 'age' (single select)
<option value="0"<?php if(isset($_POST['age']) && $_POST['age']==0) echo ' selected'; ?> >Under 50</option>

//for 'fruit' (multiple select)
<option value="Apple"<?php if(isset($_POST['fruit']) && in_array('Apple',$_POST['fruit'])) echo ' selected'; ?>>Apple</option>

You would need to do this for each select <option>

If you want to do this is a php loop you could do

// for 'age' (single select)
<select id="age" name="age">
    <option value=""></option>
    <?php 
      $opts = array(0=>'Under 50',1=>'50+');
      foreach($opts as $k=>$v) {
              $s = (isset($_POST['age']) && $_POST['age']==0) ? ' selected': '';
              echo "<option value=\"{$k}\"{$s}>{$v}</option>";
      }
    ?>
</select>

//for 'fruit' (multiple select)
<select id="fruit" name="fruit[]" multiple="multiple">
    <?php 
      $opts = array('Apple','Banana','Orange');
      foreach($opts as $v) {
              $s = (isset($_POST['fruit']) && in_array($v,$_POST['fruit'])) ? ' selected': '';
              echo "<option value=\"{$v}\"{$s}>{$v}</option>";
      }
    ?>
</select>

Upvotes: 0

RiggsFolly
RiggsFolly

Reputation: 94682

Because you are using register_globals these variables will be auto created for you, before your code gets to run.

$first_name, $last_name, $age, $fruit

So when you do this in the first line of your code

// Setup Fields, Initially Empty
$first_name = $last_name = $age = $fruit = "";

You set there values to ''. Not surprisingly when you try and put them in $_SESSION they are empty. Ditto when you try and place them back into the input fields.

Stop using the register_globals feature, reference the variables coming from the submitted form using $_POST['xxx'] or $_GET['xxx'] or if absolutely necessary $_REQUEST['xxx']. It will make your code easier to understand later and FAR MORE PORTABLE.

Upvotes: 0

Related Questions