Jump to content

[SOLVED] <select> drop down and window.location


vinpkl

Recommended Posts

hi all

 

i have two dynamic drop downs of dealer id and category id which work properly with window.location

 

var dealerid;
function getList(xyz)
{
window.location='manage_products.php?category_id=' + xyz;
}
function getProducts(dealer_id)
{
var catid=document.form1.category.value;
window.location='manage_products.php?dealer_id=' + dealer_id + "&category_id="+catid ;
}

 

but now i want to add static drop down of sub category and make use of window.location which i m not able to do

 

<select name="sub_catg" onchange="getSb(this.value)"  id="sub_catg">
<option>Select Sub Category</option>
<option value="Batteries">Batteries</option>
<option value="Leather & PU Cases">Leather & PU Cases</option>
<option value="Crystal & Rubber Coated Cases">Crystal & Rubber Coated Cases</option>
<option value="Car Mounts & USB Cradles">Car Mounts & USB Cradles</option>
<option value="AC Chargers & Car Chargers">AC Chargers & Car Chargers</option>
</select>

 

This is function that is not working

<script language="javascript">
var subid=document.form1.sub_catg.value;
function getSb(subid)
{
window.location='manage_products.php?dealer_id=' + dealer_id + "&category_id="+catid + "&sub_catg=" + subid ;
}
</script>

 

vineet

 

 

Link to comment
Share on other sites

are you getting any JS errors? where are dealer_id and catid defined?

 

hi rhodes

 

yes i m getting JS error

"dealer_id" is undefined

But i dont get any error if i select dealer and category drop downs only. This error i get on selecting the third static drop down i created that is of sub category.

 

Here is dynamic drop down for category

<select name="category" onchange="getList(this.value)" id="category">
<option value="0">Select Main Category</option>
<?php
$qry1="select * from category_table";
$result1=mysql_query($qry1,$conn);
if(mysql_num_rows($result1)>0)
{
while($row1=mysql_fetch_array($result1))
{
echo "<option value=" .$row1['Category_id']; ?>
}}
?>
</select>

 

This is dynamic drop down for dealer

<select name="dealer" onchange="getProducts(this.value)" id="dealer">
<option>Select Dealer</option>
<?php
$category_id=$_REQUEST['category_id'];
$qry="select * from dealer_table where category_id=$category_id";
$result=mysql_query($qry);
while($row=mysql_fetch_array($result))
{
echo "<option value = " . $row['dealer_id'];?>
}
?>

 

</select>

 

vineet

 

Link to comment
Share on other sites

right, but inside the function getSb(), those two variables aren't defined. so, let's define them:

 

<script type="text/javascript">
function getList(category_id)
{
  window.location='manage_products.php?category_id=' + category_id;
}
function getProducts(dealer_id)
{
  var category_id = document.form1.category.value;
  window.location = 'manage_products.php?dealer_id=' + dealer_id + "&category_id="+catid ;
}
function getSb(sub_catg)
{
  var category_id = document.form1.category.value;
  var dealer_id = document.form1.dealer.value;
  window.location = 'manage_products.php?dealer_id=' + dealer_id + "&category_id="+category_id + "&sub_catg=" + sub_catg ;
}
</script>

Link to comment
Share on other sites

hi rhodes

 

thanks. its works great as needed.

 

now the problem is that some products dont have sub category. so they needed to be displayed on selecting first two drop downs of dealer_id and category_id.

 

but the script that you have provided me doesnt works with only two drop downs. it applies window.location only if i select all three drop downs.

 

so is there any solution that the product can be displayed on selecting two drop downs and also on selecting three drop downs or do i need to add some null value to third drop down.

 

vineet

Link to comment
Share on other sites

after selecting the product, the URL should have the category and the product. At that point the server side code should check for sub categories...if there are some, show the sub categories dropdown, otherwise, show the products

Link to comment
Share on other sites

after selecting the product, the URL should have the category and the product. At that point the server side code should check for sub categories...if there are some, show the sub categories dropdown, otherwise, show the products

 

hi rhodes

 

thanks for the reply. i will check it server side for sub categories.

 

the last thing i would like to ask that how can i make the selected option remain selected in the drop down. can it be done with javascript.

 

vineet

Link to comment
Share on other sites

that would be server side:

<select name="category" onchange="getList(this.value)" id="category">
<option value="0">Select Main Category</option>
<?php
  $qry1="select * from category_table";
  $result1=mysql_query($qry1,$conn);
  if(mysql_num_rows($result1)>0)
  {
    while($row1=mysql_fetch_array($result1))
    {
      echo '<option value="' . $row1['Category_id']. '" ';
      if(isset($_GET['category_id']) && $_GET['category_id'] == $row1['Category_id'])
        echo 'SELECTED ';
      echo '>' . $row1['name'] . '</option>';
    }
  }
?>
</select>

replace $row1['name'] with whatever the correct variable is

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.