saco
saco

Reputation: 21

trying to make php select drop down retain its values on submit

Am a php novice and in need of some help. Basically I have a script that populates a drop down select bar using php. I want to retain the value the user selects in this drop down after submission, so the user does not have to select it again, here is the snippet of the script that I am trying to work with.

   <?php
// Start the session
session_start();

$_SESSION["dir"] = $_POST['hiddenVal'];
//echo "hi at last" .$_SESSION["dir"];
?>

<script>
function loadValues()
{
var $x=obj.options[obj.selectedIndex].value;
document.getElementById("hiddenVal").value = $x;
//alert(document.getElementById("hiddenVal").value);
}
</script>

<script>

  function country(obj){
  //alert(obj.options[obj.selectedIndex].value); //if you want to show in alart
  //or put in a variable 
  var $x=obj.options[obj.selectedIndex].value;
  document.getElementById("hiddenVal").value = $x;
  alert($x);

  }

 </script>

 <body onload="loadValues();">

 <form action="dir20.php" method="post">
 <input type="hidden"  id="hiddenVal" name = "hiddenVal"/>
 <input type="submit" name="submit" id="submit" value="Submit" />
 </form>
 </body>













<?php
$path = '/docdownloads';
//echo  'php_'.$abc;

$dirs = array();

// directory handle
$dir = dir($path);

while (false !== ($entry = $dir->read())) {
    if ($entry != '.' && $entry != '..') {
       if (is_dir($path . '/' .$entry)) {
            $dirs[] = $entry; 
            //echo "$entry</br>";

       }
    }
}










?>

<select name="country" id="country"  onChange="country(this)" required>
<!--<option value="">-----------------</option>-->
<?php
    asort($dirs);
    reset($dirs); 
    foreach($dirs as $p => $w):
        echo '<option value="'.$w.'">'.$w.'</option>'; //close your tags!!
    endforeach;
?>
</select>







<?php
$tex = $_SESSION["dir"];

//$def = "docdownloads";

//$dir = "c:/".$tex;
$dir = "c:/docdownloads/".$tex;
//echo "tex is". "$dir";
echo "</br>";
//if ($handle = opendir('.')) {
    if ($handle = opendir($dir)) {
    while (false !== ($entry = readdir($handle))) {

        if ($entry != "." && $entry != "..") {

            echo "$entry</br>";
        }
    }

    closedir($handle);
}
?>

Upvotes: 0

Views: 54

Answers (5)

saco
saco

Reputation: 21

Blockquote You need to put your select element inside your form. Otherwise it won't submit

problem solved, script now works, many thanks!

Upvotes: 0

Bhaskar Jain
Bhaskar Jain

Reputation: 1691

get the submitted value of country by GET or POST(depend on your form method) and compare it in foreach loop

$countryId = isset($_POST['country']) ? $_POST['country'] : ''; //or $_GET['country'];
foreach($dirs as $p => $w):
    $selected = $countryId==$w ? 'selected' : '';
        echo '<option value="'.$w.'" '.$selected.'>'.$w.'</option>';
    endforeach;

Assuming your form action is on same page, if different, store country value in session and retrieve it from session in this page

Upvotes: 0

Jacek Dziurdzikowski
Jacek Dziurdzikowski

Reputation: 2265

To achieve that you should save the value the user chose into session and always when displaying the form, you should check if the value exists in the session and if it exists - display it as selected option.

Upvotes: 0

You will need to assign a ID for user if you havent already, then save the submitted option in your database with corresponding ID. Then when you load you'r dropdown box insert a IF clause in your script to check if user has already saved a option.

Upvotes: 0

Juakali92
Juakali92

Reputation: 1143

You're going to want to check the $_GET var against the selected option. If the current option exists and it has been selected, then give the option the selected attribute.

-----------------

<?php
    asort($dirs);
    reset($dirs); 
    foreach($dirs as $p => $w):

        $selected = isset($_GET['country']) && $_GET['country'] === $w ? "selected" : "";
        echo '<option value="'.$w.'"'.$selected.'>'.$w.'</option>';
    endforeach;
?>
</select>

Upvotes: 3

Related Questions