How to display value in drop down list after form has been reloaded


Posted on 16th Feb 2014 07:03 pm by admin

Please bear with me as I am very new to php and html.

I have a form with several drop down menus, the one at the top shows a list that when an item is selected fills in the form below including the other drop downs. So when an item is selected the form gets info from a mysql database and then fills in the form. At this point the user can further modify the form and re-submit it to update the database. This means that the drop downs in the form not only need to display the relavent options from the database to reflect the existing choice but also when clicked on show a list to allow the user the change the selection from the list.

This means that the dropdown needs to:
show the words 'select item' when the form is first loaded and the form is empty
show the current selection when the defined by a selection in the top drodown
show all the available items when the list is dropped down
Curently my drop down does only this:
shows the nothing when the form is first loaded and the form is empty
shows the current selection when defined by a selection in the top drodown
show all the available items when the list is dropped down plus the one that was there before it was clicked on, therefore doubing the option up showing the same option twice

The form is here if you would like to see it in action: http://www.spencercarpenter.co.uk/portfolioAppFiles/simpleForm.php

And a snippet of code from one of th drop downs is here:

Code: <select name="img01" onChange="updateImg(this, 'thumbImg01')">
<option value=-"-1" ><?php echo $row_chosen_pItem['img01']; ?></option>
<?php buildFileList5('uploaded');?>
</select>
the code inside the 'buildFileList5' looks like this:

Code: <?php
function buildFileList5($theFolder)
{
// Execute code if the folder can be opened, or fail silently
if ($contents = @ scandir($theFolder))
{
// initialize an array for matching files
$found = array();
// Create an array of file types
$fileTypes = array('jpg','jpeg','gif','png');
// Traverse the folder, and add filename to $found array if type matches
$found = array();
foreach ($contents as $item)
{
$fileInfo = pathinfo($item);
if (array_key_exists('extension', $fileInfo) && in_array($fileInfo['extension'],$fileTypes))
{
$found[] = $item;
}
}
// Check the $found array is not empty
if ($found)
{
// Sort in natural, case-insensitive order, and populate menu
natcasesort($found);
$selectedImage = ""; //default image
foreach ($found as $filename)
{
if(!empty($_POST['img01']) && $_POST['img01'] == $filename)
{
$sel = "SELECTED";
$selectedImage = $filename;
}
echo "<option value='$filename' $sel>$filename</option>n";
}
}
}
}
?>



I hope that is enough info to explain it.

If anyone could tell how to get the dropdown working properly I would be very gratefull.

Thanks a lot

No comments posted yet

Your Answer:

Login to answer
241 Like 49 Dislike
Previous forums Next forums
Other forums

Can't shake the "Warning: include()" error
I've just uploaded my site to a new server and where I have PHP include tags in my HTML, the browser

IS Retail & Manufacturing
Hi

Can IS Retail and Manufacturing activities be done in same instance ie. in same client

MVC - Code review
I'm in the process of trying to wrap my head around MVC, and as part of that, I'm attempting to impl

an interesting question
Greetings folks, got to ask an interesting question...

let say
i have created a function s

List/Menu Box
On an edit page when you want something to select what a user has previously selected from the datab

TCP Sending Unsigned Char...
Hi...

I want to send over tcp/ip some data, i have the data in hexadecimal, and when i tr

NOOB needs help with upload file
The intent of this form is to create a folder for a client and upload an image into another folder '

Email/MMS to Site
Hey guys. I'm looking for a way for a user to upload images and/or text via their cell phones. It

PHP Error
On my .php page I have a drop down box that has several names in it. When a user clicks the name &am

Warning: mysql_num_rows() expects parameter 1
When i add quote at the end of this link
example:http://localhost/articlemania/category.php?cid=3

Sign up to write
Sign up now if you have flare of writing..
Login   |   Register
Follow Us
Indyaspeak @ Facebook Indyaspeak @ Twitter Indyaspeak @ Pinterest RSS



Play Free Quiz and Win Cash