Jump to content

PHP and a pre-selected option in a form


Nuggit

Recommended Posts

I wasn't quite sure whether I should have posted this in the HTML forum or here, but I'm posting it here and a mod can move it as he/she sees fit. :'P (thanks!)

 

I've got a little text editor thing on my site, and to open a file, you input the file name and choose the folder from a drop-down list and click the Open button. The default filename is "index" and that file keeps track of all the files contained in that folder. It works okay. What's annoying is that regardless of what folder you're messing around in, the drop down list always goes back to showing the default folder name as the page is reloaded. I want it to be preselected to the folder it's in.

 

It seems simple enough--the folder you're in is $_GET['dir']. But I've read some HTML form how-tos, and I can only see that you can preselect using the tabindex="" or selected attribute.

And I don't know how I could efficiently work that with info gained via PHP.

Link to comment
https://forums.phpfreaks.com/topic/47600-php-and-a-pre-selected-option-in-a-form/
Share on other sites

Here's an example. Post your code for a more specific solution:

 

<?php

echo "<select name="folderList">";

$select_list = array ('Folder1', 'Folder2', 'Folder3', 'Folder4');
foreach ($select_list as $item) {
  $selected = ($item==$_GET['dir']) ? 'selected' : '';
  echo '<option value="'.$item.'">'.$item.'</option>';
}

echo "</select>";

?>

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.