/    Sign up×
Community /Pin to ProfileBookmark

javascript onchange

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>

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@JonaMar 28.2003 — I dunno.. maybe onchange="this.form.submit();" I don't think Javascript: is required.
Copy linkTweet thisAlerts:
@cawhite325authorMar 28.2003 — I tried that and it is still defaulting back to PLease select in the top list box. Any other suggestions?
Copy linkTweet thisAlerts:
@DaiWelshMar 28.2003 — 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
Copy linkTweet thisAlerts:
@cawhite325authorMar 28.2003 — It works now. Thank you for your help
×

Success!

Help @cawhite325 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...