Jump to content

Auto populate multiple list box..


techker

Recommended Posts

hey guys i can't seem to get my second list box to work?

 

the first one is good it shows the brands but the second one does not show at all?

 

i have 2 DB Brands and others products

 

so in brands i have id and name ..

 

second i have product_id-product_Nand Brand

 

this is my query(select the brand then it list the products that are associated with the brand)

 

<?php 
$Brand_N = $Product_N = null; //declare vars 

$conn =  mysql_connect("localhost", "2", "2"); 
$db = mysql_select_db('2',$conn); 

if(isset($_POST["Brand_N"]) && is_numeric($_POST["Brand_N"])) 
{ 
$Brand_N = $_POST["Brand_N"]; 
} 

if(isset($_POST["Product_N"]) && is_numeric($_POST["Product_N"])) 
{ 
$Product_N = $_POST["Product_N"]; 
} 
?> 

<script language="JavaScript"> 
function autoSubmit() 
{ 
var formObject = document.forms['theForm']; 
formObject.submit(); 
} 
</script> 

<form name="theForm" method="post"> 


<select name="brand" onChange="autoSubmit();"> 
<option value="Brand">Brand</option> 
<?php 
$sql = "SELECT * FROM Brand"; 
$Brand = mysql_query($sql,$conn); 
while($row = mysql_fetch_array($Brand)) 
{ 
echo ("<option value=\"$row[brand_id]\" " . ($Brand_N == $row["Brand_N"]? " selected" : "") . ">$row[brand_N]</option>"); 
} 
?> 
</select> 

<?php 
if($Brand_N!= null && is_numeric($Brand_N)) 
{ 
?> 

<select name="Product" onChange="autoSubmit();"> 
<option value="Product">Product</option> 

<?php 
$sql = "SELECT * FROM products WHERE Brand = $Brand_N "; 
$Products = mysql_query($sql,$conn); 
while($row = mysql_fetch_array($Products)) 
{ 
echo ("<option value=\"$row[Product_id]\" " . ($Product_N == $row["Product_N"]? " selected" : "") . ">$row[Products_N]</option>"); 
} 
?> 
</select> </form>
<?php 
} 
?>

Link to comment
Share on other sites

so it's just this part?i modified it but still..

 

<?php 
$sql = "SELECT * FROM Brand"; 
$Brand = mysql_query($sql,$conn); 
while($row = mysql_fetch_array($Brand)) 
{ 
echo ("<option value=\"$row[brand_id]\" " . ($Brand_N == $row["Brand_id"]? " selected" : "") . ">$row[brand_N]</option>"); 
} 
?> 
</select> 

<?php 
if($Brand_N!= null && is_numeric($Brand_N)) 
{ 
?> 

<select name="Product" onChange="autoSubmit();"> 
<option value="Product">Product</option> 

<?php 
$sql = "SELECT * FROM products WHERE Brand_id = $Brand_N "; 
$Products = mysql_query($sql,$conn); 
while($row = mysql_fetch_array($Products)) 
{ 
echo ("<option value=\"$row[Product_id]\" " . ($Product_N == $row["Product_N"]? " selected" : "") . ">$row[Products_N]</option>"); 
} 
?> 

Link to comment
Share on other sites

In your previous code you had:

 

<select name="brand" onChange="autoSubmit();"> 

 

PHP uses this name when posted in the $_POST array.  Because of that, you should be looking for $_POST['brand'] instead of $_POST['Brand_N'].

 

~juddster

Link to comment
Share on other sites

ok so im started to get it..lol

 

if(isset($_POST["Brand_N"]) && is_numeric($_POST["Brand_N"])) 
{ 
$Brand_N = $_POST["Brand"]; 
} 

if(isset($_POST["Product_N"]) && is_numeric($_POST["Product_N"])) 
{ 
$Product_N = $_POST["Product"]; 
}

 

so i need to change the product post to..

Link to comment
Share on other sites

ok so i changed it all to the select name..but still nothing?is there a way to echo the results so that i can see when i select a brand the output?

 

if(isset($_POST["Brand"]) && is_numeric($_POST["Brand"])) 
{ 
$Brand_N = $_POST["Brand"]; 
} 

if(isset($_POST["Product"]) && is_numeric($_POST["Product"])) 
{ 
$Product_N = $_POST["Product"]; 
} 

Link to comment
Share on other sites

<?php 
$Brand_N = $Product_N = null; //declare vars 

$conn =  mysql_connect("localhost", "1", "2"); 
$db = mysql_select_db('3',$conn); 

if(isset($_POST["Brand"]) && is_numeric($_POST["Brand"])) 
{ 
$Brand_N = $_POST["Brand"]; 
} 

if(isset($_POST["Product"]) && is_numeric($_POST["Product"])) 
{ 
$Product_N = $_POST["Product"]; 
} 
?> 

<script language="JavaScript"> 
function autoSubmit() 
{ 
var formObject = document.forms['theForm']; 
formObject.submit(); 
} 
</script> 

<form name="theForm" method="post"> 


<select name="Brand" onChange="autoSubmit();"> 
<option value="Brand">Brand</option> 
<?php 
$sql = "SELECT * FROM Brand"; 
$Brand = mysql_query($sql,$conn); 
while($row = mysql_fetch_array($Brand)) 
{ 
echo ("<option value=\"$row[brand_id]\" " . ($Brand_N == $row["Brand_N"]? " selected" : "") . ">$row[brand_N]</option>"); 
} 
?> 
</select> 

<?php 
if($Brand_N!= null && is_numeric($Brand_N)) 
{ 
?> 

<select name="Product" onChange="autoSubmit();"> 
<option value="Product">Product</option> 

<?php 
$sql = "SELECT * FROM products WHERE Brand_id = $Brand_id "; 
$Products = mysql_query($sql,$conn); 
while($row = mysql_fetch_array($Products)) 
{ 
echo ("<option value=\"$row[Product_id]\" " . ($Product_N == $row["Product_N"]? " selected" : "") . ">$row[Products_N]</option>"); 
} 
?> 
</select> 

<?php 
} 
?>
</form>
<? print_r ( $_POST );?>

Link to comment
Share on other sites

oK

 

so now it works i had another typo Products_Nneeded to be product_N

 

echo ("<option value=\"$row[Product_id]\" " . ($Product_N == $row["Product_N"]? " selected" : "") . ">$row[Products_N

 

but now it works but when i select the product it resets the form back to brand?

 

i need to keep it selected..

Link to comment
Share on other sites

If you look at it, after you select a value from the first drop down list and it auto posts back, the drop down list is back to 'Brand' which isn't correct.  As I mentioned you need to change the condition where you are marking it as selected to be based on the ID rather than the name because that is the value being returned.

 

Because of this, you are seeing the issue you mentioned.

 

~juddster

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.