PDA

View Full Version : <select> option selected onload



thenajsays
10-17-2009, 04:30 PM
im having a bit of trouble working out a problem... i have a PHP form with a drop down. if the form is called to add a new record into the database, the default select option is set. if the form is called to edit a pre-existing record, then the selected option depends on what is already in the record. whats the easiest, cleanest way to do this?

jscheuer1
10-17-2009, 04:49 PM
If your page is outputting the HTML via PHP, have PHP add the selected attribute to the item that should be selected. Example output HTML:


<select name="whatever">
<option value="whatever">something</option>
<option value="whatever">another</option>
<option value="whatever">yet another</option>
<option value="whatever" selected>still more</option>
</select>

thenajsays
10-17-2009, 05:19 PM
that doesnt really make sense to me? heres my script:
<?php

$action = $_REQUEST['action'];
//$id = $_REQUEST['id'];
echo "<h2>Edit Listing Type</h2>\n";
if ($action == 'delete')
{
$id = $_REQUEST['id'];
$query = "DELETE FROM listtype WHERE id = $id";
$result = mysql_query($query) or die(mysql_error());
if ($result)
{
echo "You have successfully deleted that listing type.<br>\n";
echo "<a href=\"index.php?content=types\">Return Listing Types</a>\n";
exit;
} else
{
echo "Sorry, there was a problem deleting that listing type.<br>\n";
echo "<a href=\"index.php?content=types\">Return Listing Types</a>\n";
exit;
}
}
else if($action == 'add')
{
$type = $_POST['type'];
$query = "INSERT INTO `listtype` (`type`) VALUES ('$type')";
$result = mysql_query($query) or die(mysql_error());
if ($result)
{
echo "You have successfully added '$type.'<br>\n";
echo "<a href=\"index.php?content=types\">Return Listing Types</a>\n";
exit;
} else
{
echo "Sorry, there was a problem adding '$type.'<br>\n";
echo "<a href=\"index.php?content=types\">Return Listing Types</a>\n";
exit;
}
}
else if($action == 'update')
{
$id = $_REQUEST['id'];
$type = $_POST['type'];
$query = "UPDATE listtype SET type='$type' WHERE id = $id";
$result = mysql_query($query) or die(mysql_error());
if ($result)
{
echo "You have successfully updated '$type.'<br>\n";
echo "<a href=\"index.php?content=types\">Return Listing Types</a>\n";
exit;
} else
{
echo "Sorry, there was a problem updating '$type.'<br>\n";
echo "<a href=\"index.php?content=types\">Return Listing Types</a>\n";
exit;
}
}
?>

jscheuer1
10-17-2009, 06:34 PM
No, I meant the PHP which outputs the select element and its options. But I did say (emphasis added):


If your page is outputting the HTML via PHP, have PHP add the selected attribute to the item that should be selected.

So either you have other PHP code that does this (other than that in your posts so far), or you don't. If not, you should consider using it, as it is more certain (users may turn off javascript) and more secure (users may easily hack your javascript).

However, it doesn't sound like security is much of an issue here - for just selecting an option element in a select element - though it may be. Certainly though certainty may be an issue - otherwise why would you want to have this happen in the first place?

That said, there are a number of ways to select an option onload using javascript, here's one:


window.onload = function(){
var f = document.forms.formName, s = f.elements.selectName;
s.options[optionNumber].selected = true;
};

The two highlighted names should be hard coded as the actual name of the form and the actual name of the select element on the page. The highlighted and red number should be replaced with a PHP token that will resolve the option number (numbered 0 to whatever) of the option you want to be selected.

thenajsays
10-18-2009, 02:23 PM
thanks... completely obvious to me now! i was having a doorknob moment! :) sorry!