Click to See Complete Forum and Search --> : javascript onchange


cawhite325
03-27-2003, 10:25 PM
Hello,

I am having trouble with my onchange list box. In my first list box you select a category which populates the list box below with the appropriate job types. This works fine. But the value that I selected in the first list box does not stay selected. I need this value to stay selected because when the form is complete, there will be a submit button that needs the selected information from both list boxes. Here is my code:

<HTML>
<HEAD>
<TITLE>Search Jobs</TITLE>
</HEAD>
<BODY>

<?php REQUIRE("db_3rjobs.inc");

if (isset($Select1))
{


$sql = "SELECT job_type FROM type WHERE category_id = '$Select1'";
$result = mysql_query($sql);

}
?>

<FORM>
<SELECT NAME="Select1" SIZE="1" onChange="javascript:document.forms[0].submit()">
<OPTION VALUE="">-- please select --</OPTION>
<OPTION value="2">Teaching, Professor
<OPTION value="3">Teaching, Middle/High School
<OPTION value="4">Teachgin, Elementary
<OPTION value="5">Teaching, Special Ed
<OPTION value="7">Administrative, Management




</SELECT><BR>
</FORM>

<FORM>
<SELECT NAME="Select2" SIZE="5">"
<?php
while ($row = mysql_fetch_row($result))
{
foreach ($row as $field)
{
print "<OPTION value='$field'";
print ">$field</OPTION>";
}
}

?>
</SELECT>
</FORM>
</body>
</html>

Jona
03-27-2003, 10:44 PM
I dunno.. maybe onchange="this.form.submit();" I don't think Javascript: is required.

cawhite325
03-28-2003, 09:55 AM
I tried that and it is still defaulting back to PLease select in the top list box. Any other suggestions?

DaiWelsh
03-28-2003, 10:02 AM
As suggested, this is not actually a javascript question, but a php question, but still, the answer is that you need to add 'selected' to the right option in the first select when the page is resubmitted. Something like this should work.

<OPTION VALUE="">-- please select --</OPTION>
<OPTION value="2"<?=($Select1 == 2)?' selected':''?> >Teaching, Professor
<OPTION value="3"<?=($Select1 == 3)?' selected':''?> >Teaching, Middle/High School
<OPTION value="4"<?=($Select1 == 4)?' selected':''?> >Teachgin, Elementary
<OPTION value="5"<?=($Select1 == 5)?' selected':''?> >Teaching, Special Ed
<OPTION value="7"<?=($Select1 == 7)?' selected':''?> >Administrative, Management

Though you should really default $Select to something like 0 if it is not set to avoid undefined warnings....

HTH,

Dai

cawhite325
03-28-2003, 10:48 AM
It works now. Thank you for your help