/    Sign up×
Community /Pin to ProfileBookmark

Select Box Script

Hi All,

I’m wanting to have 2 select boxes go to showproducts.php page. Below is my code. Do I need 2 functions, or, can I do an if in the function go()..?

Javascript Code:

<script lanaguage=”javascript”>
function go()
{
box = document.forms[0].navi;
destination = box.options[box.selectedIndex].value;
if (destination) location.href = destination;
}
</script>

HTML/PHP Code:

<form>
<table align=”center” border=”2″>
<tr>
<td><h3 align=center>PRODUCT TYPE</h3></td>
</tr>
<tr><td><center>
<?php
$db = mysql_connect(“localhost”, “root”, “grunger”);
mysql_select_db(“status”,$db);
$result = mysql_query(“SELECT ProductTypes.product_type_detail, ProductTypes.product_type_id FROM ProductTypes”,$db);
$num = mysql_num_rows($result);
?>
<select name=”slct_product_type” onChange=”go()”>
<option value””>Make A Selection</option>
<?php
for ($i=0; $i<$num; $i++){
$myrow=mysql_fetch_array($result);
$product_type=mysql_result($result,$i,”product_type_detail”);
$product_type_id=mysql_result($result,$i,”product_type_id”);
echo “<option value=”showproducts.php?id=$product_type_id”>$product_type</option><br>”;
}
?>
</select>
</center>
</td></tr>
</table>
<table align=”center” border=”2″>
<tr>
<td><h3 align=center>MAKER</h3></td>
</tr>
<tr><td><center>
<?php
$maker_result = mysql_query(“SELECT Makers.maker_id, Makers.maker_detail FROM Makers”,$db);
if (!$maker_result) die(mysql_error());
?>
<select name=”slct_maker” onChange=”go()”>
<option value=””>Make A Selection</option>
<?php
$maker_num = mysql_num_rows($maker_result);
for ($i=0; $i<$maker_num; $i++){
$myrow=mysql_fetch_array($maker_result);
$maker=mysql_result($maker_result,$i,”maker_detail”);
$maker_id=mysql_result($maker_result,$i,”maker_id”);
echo “<option value=”showproducts.php?id=$maker_id”>$maker</option><br>”;
}
?>
</select>
</center>
</td></tr>
</table>
</form>

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@FangMay 13.2005 — You need to reference the [I]select[/I].
&lt;select name="slct_product_type" onchange="go(this[this.selectedIndex].value)"&gt;
Then you get:
function go(destination) {
if (destination) location.href = destination;}
Copy linkTweet thisAlerts:
@KorMay 13.2005 — first at all rename your function. [b]go[/b] is a reserved (in fact an used) javascript specific word. Avoid using reserved words for denominating variable, parameters and functions

Now, yes, you may use [b]this[/b] self reference to simplyfy your code and to use a single function for all your boxes

function goLocation(s){

var destination = s.options[s.selectedIndex.value];

if (destination) {location.href = destination}

}

and

<select name="slct_product_type" onChange="goLocation([color=red]this[/color])">

...

<select name="slct_maker" onChange="goLocation([color=red]this[/color])">
Copy linkTweet thisAlerts:
@coolboarderguyauthorMay 16.2005 — Hi All,

thanx guys. Will make the suggested changes.
×

Success!

Help @coolboarderguy 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.27,
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,
)...