techker Posted October 3, 2011 Share Posted October 3, 2011 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 } ?> Quote Link to comment Share on other sites More sharing options...
awjudd Posted October 3, 2011 Share Posted October 3, 2011 Your select for brand's name is 'brand' not 'Brand_N', so the value will never be viewed as posted. ~juddster Quote Link to comment Share on other sites More sharing options...
techker Posted October 3, 2011 Author Share Posted October 3, 2011 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>"); } ?> Quote Link to comment Share on other sites More sharing options...
awjudd Posted October 3, 2011 Share Posted October 3, 2011 Now you aren't setting the variables. All you needed to change in your last post was the posted value you were looking for (i.e. 'Brand_N' to 'brand'). To get that value. ~juddster Quote Link to comment Share on other sites More sharing options...
techker Posted October 3, 2011 Author Share Posted October 3, 2011 sorry im rusted out..i don't get it..brand is the db name. echo ("<option value=\"$row[brand_id]\" " . ($Brand_N == $row["Brand_id"]? " selected" : "") . ">$row[brand_N]</option>"); ??? Quote Link to comment Share on other sites More sharing options...
awjudd Posted October 3, 2011 Share Posted October 3, 2011 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 Quote Link to comment Share on other sites More sharing options...
techker Posted October 3, 2011 Author Share Posted October 3, 2011 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.. Quote Link to comment Share on other sites More sharing options...
awjudd Posted October 3, 2011 Share Posted October 3, 2011 $_POST [ 'Brand_N' ] should be referenced as $_POST [ 'brand' ] since that is the name of the drop down list being submitted. ~juddster Quote Link to comment Share on other sites More sharing options...
techker Posted October 3, 2011 Author Share Posted October 3, 2011 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"]; } Quote Link to comment Share on other sites More sharing options...
awjudd Posted October 3, 2011 Share Posted October 3, 2011 add print_r ( $_POST ); to your code and you can see everything being posted. Edit: I said $_POST [ 'brand' ] not $_POST [ 'Brand' ] ... please note ... the array is case sensitive. ~juddster Quote Link to comment Share on other sites More sharing options...
techker Posted October 3, 2011 Author Share Posted October 3, 2011 odd select box (Brand) Array ( [brand] => 1 ) Quote Link to comment Share on other sites More sharing options...
awjudd Posted October 3, 2011 Share Posted October 3, 2011 <select name="brand" onChange="autoSubmit();"> Please Note: lowercase "brand" as in the name attribute of the select box. ~juddster Quote Link to comment Share on other sites More sharing options...
techker Posted October 3, 2011 Author Share Posted October 3, 2011 typo error in the select box by default it is writen brand when i select a brand it just resets to brand Quote Link to comment Share on other sites More sharing options...
awjudd Posted October 3, 2011 Share Posted October 3, 2011 Huh? Quote Link to comment Share on other sites More sharing options...
techker Posted October 3, 2011 Author Share Posted October 3, 2011 ok i got your point..lol i changed it to uppercase like all of it.now it shows the second drop down but empty Array ( [brand] => 1 [Product] => Product ) Quote Link to comment Share on other sites More sharing options...
awjudd Posted October 3, 2011 Share Posted October 3, 2011 Post updated code? ~juddster Quote Link to comment Share on other sites More sharing options...
techker Posted October 3, 2011 Author Share Posted October 3, 2011 <?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 );?> Quote Link to comment Share on other sites More sharing options...
awjudd Posted October 3, 2011 Share Posted October 3, 2011 $sql = "SELECT * FROM products WHERE Brand_id = $Brand_id "; That $Brand_id should be $Brand_N. ~juddster Quote Link to comment Share on other sites More sharing options...
techker Posted October 3, 2011 Author Share Posted October 3, 2011 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.. Quote Link to comment Share on other sites More sharing options...
awjudd Posted October 3, 2011 Share Posted October 3, 2011 Please expand ... ~juddster Quote Link to comment Share on other sites More sharing options...
techker Posted October 3, 2011 Author Share Posted October 3, 2011 when i select brand it jumps to products but brand goes back to Brand instead of staying on the selected brand? maybe the link would help http://x81.tech-design.ca/script/test_m.php Quote Link to comment Share on other sites More sharing options...
awjudd Posted October 3, 2011 Share Posted October 3, 2011 $Brand_N == $row["Brand_N"]? " selected" : "" That should be looking at the Brand_Id because that is the value that gets submitted. ~juddster Quote Link to comment Share on other sites More sharing options...
techker Posted October 3, 2011 Author Share Posted October 3, 2011 can i print in a input box the selected brand and product ? cause i get what your saying but it still does not stay loaded on the selected brand and product.. Quote Link to comment Share on other sites More sharing options...
techker Posted October 3, 2011 Author Share Posted October 3, 2011 look at this one http://x81.tech-design.ca/script/test_mp.php i added <? print_r ( $_POST["Product"]);?> <? print_r ( $_POST["Brand"]);?> Quote Link to comment Share on other sites More sharing options...
awjudd Posted October 3, 2011 Share Posted October 3, 2011 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.