Jump to content

Issues with "Remembering the selected option of dropdown in PHP"


g_pmattoo

Recommended Posts

Here is what i tried but it is not working. Any clue why or anyone has any code snippets to pass across. Also the fields in this

 

form are transported to an xml and i get the index value(0,1,2 or 3) of selected option in test dropdown rather than it's

 

value(No,abc,xyz or efg)

 

$option = array(0 => 'No', 1 => 'abc', 2 => 'xyz', 3 => 'efg');

        $selected = (isset($_POST['test']) && intval($_POST['test']) < 4) ? $_POST['test'] : ''; //index of dropdown

        echo "<select name=\"test\">\n";

        echo createOptionFromArray($option,$selected);

        echo "</select>\n"; 

       

       

      function createOptionFromArray($myArray,$selected) {

      if(!is_array($myArray)) {

            return false;

        }

        $returned = $select = '';

        foreach($myArray as $key => $value) {

            if($selected == $key) {

                $select = ' selected';

            }

            $returned .= "<option value=\"$key\"$select>$value</option>";

            $select = '';

        }

        return $returned;

    } 

$option = array(0 => 'No', 1 => 'abc', 2 => 'xyz', 3 => 'efg');
        $selected = (isset($_POST['test']) && intval($_POST['test']) < 4) ? $_POST['test'] : ''; //index of dropdown
        echo "<select name=\"test\">\n";
        echo createOptionFromArray($option,$selected);
        echo "</select>\n"; 
       
       
       function createOptionFromArray($myArray,$selected) {
       if(!is_array($myArray)) {
            return false;
        }
        $returned = $select = '';
        foreach($myArray as $key => $value) {
            if($selected == $key) {
                $select = ' selected';
            }
            $returned .= "<option value=\"$key\"$select>$value</option>";
            $select = '';
        }
        return $returned;
    } 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.