Board index   FAQ   Search  
Register  Login
Board index php forum :: Database mySQL & php coding

Need drop down menu to have existing value as default

Codes here !

Moderators: macek, egami, gesf

Need drop down menu to have existing value as default

Postby seanryan10 » Thu Apr 17, 2014 3:00 am

I am currently working on what I consider a basic Helpdesk system
in order to try and improve my coding skills.I'm not overly experienced.

I have come across the following problem.


I currently see a list of incidents assigned to me on the home page once logged in.
I can click on a button to edit the details of a specific incident.
This brings me to a new page showing all the details, some of which I can change with a drop down menu.

To change the status of an incident I use the following code...


echo "<input name='status_ro' value='$seperateDetails[status]' size=10 readonly></input>";
echo "<select name='status'>";
echo status_dropdown();
echo "</select>";

The status_dropdown() function exists in a seperate page (functions.php)

function status_dropdown()
{

$options = mysql_query("select * from status");
while($single_option = mysql_fetch_array($options))
{echo "<option value='" . $single_option['status_id'] ."'>" . $single_option['status'] ."</option>";}
}

This all works but I have spotted a issue which could lead to problems down the road.

When I edit an incident and try to change the status of that incident (e.g.from 'Assigned' to 'Closed')
The dropdown menus do not have the existing value ('Assigned') selected as default but show the first value ('Unassigned') generated by the while loop from the 'Status' table.

This means if I want to edit a specific incident I have to check each dropdown menu to make sure they are set to the correct value before I save the changes.

I hope this makes sense and you can help me.

Thanks,
Seán
seanryan10
New php-forum User
New php-forum User
 
Posts: 1
Joined: Thu Apr 17, 2014 2:22 am

Re: Need drop down menu to have existing value as default

Postby seandisanti » Tue Apr 22, 2014 12:47 am

This is accomplished when you're building the select box. The code looks a little different depending on whether you're doing object based approach or procedural, but basically, when you're building your select box, if you have a stored value for that field, its id should match the value of the same selection in the select box. So populating the select box would look something like this:
Code: Select all
echo '<select name="whatever"><option>Pick something already</option>';
While (true){ 

// just using true for example, but you'd want to use a 
// real condition that will be false after the last item 
// from your query has been handled

$selected = ($row['id'] == $current_value)?' selected="selected" ':'';
echo '<option value=$row['id']' . $selected . '/>' . $row['description'] . '</option>';
}
echo 
'</select>';
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm


Return to mySQL & php coding

Who is online

Users browsing this forum: No registered users and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.