naykidd Posted August 24, 2009 Share Posted August 24, 2009 Basically, im trying to get 2 values from a PHP dropdown menu, to then use in a MySql query, but im having trouble retrieving them... I have Makes and Models, and currently, I can retrieve Make, and echo it so i know it exists, but i can't retrieve the Model, it just echos make again... The 2 tables are dynamically linked (Once a model is selected,the page refreshes and a make can be chosen depending on which model) I know how to take the variables and put them into the MySQL table, its just retrieving them! Heres what i have so far: require_once("Connections/connection.php"); session_start(); $make = $_POST['make']; if ($make){ $query = sprintf("SELECT * FROM car_model where car_model_id='$make'"); $result = @mysql_query($query); $rowModel = mysql_fetch_array($result); } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <form id="form1" name="form1" method="post" action="<?php $_SERVER['PHP_SELF']; ?>"> <select name="make" onChange="document.forms[0].submit()"> <option value="" selected="selected">Select Make</option> <option value="AC" <?php if(!(strcmp(AC, $make))){echo "selected";}?>>AC</option> <option value="ALFA" <?php if(!(strcmp(ALFA, $make))){echo "selected";}?>>ALFA</option> <option value="AUDI" <?php if(!(strcmp(AUDI, $make))){echo "selected";}?>>AUDI</option> <option value="AUSTIN" <?php if(!(strcmp(AUSTIN, $make))){echo "selected";}?>>AUSTIN</option> <option value="AUSTIN-HEALEY" <?php if(!(strcmp(AUSTIN-HEALEY, $make))){echo "selected";}?>>AUSTIN-HEALEY</option> <option value="BMW" <?php if(!(strcmp(BMW, $make))){echo "selected";}?>>BMW</option> <option value="CATERHAM" <?php if(!(strcmp(CATERHAM, $make))){echo "selected";}?>>CATERHAM</option> <option value="CHEVROLET" <?php if(!(strcmp(CHEVROLET, $make))){echo "selected";}?>>CHEVROLET</option> <option value="CHRYSLER" <?php if(!(strcmp(CHRYSLER, $make))){echo "selected";}?>>CHRYSLER</option> <option value="CITROEN" <?php if(!(strcmp(CITROEN, $make))){echo "selected";}?>>CITROEN</option> <option value="DAEWOO" <?php if(!(strcmp(DAEWOO, $make))){echo "selected";}?>>DAEWOO</option> <option value="DAIHATSU" <?php if(!(strcmp(DAIHATSU, $make))){echo "selected";}?>>DAIHATSU</option> <option value="FIAT" <?php if(!(strcmp(FIAT, $make))){echo "selected";}?>>FIAT</option> <option value="FORD" <?php if(!(strcmp(FORD, $make))){echo "selected";}?>>FORD</option> <option value="HONDA" <?php if(!(strcmp(HONDA, $make))){echo "selected";}?>>HONDA</option> <option value="HYUNDAI" <?php if(!(strcmp(HYUNDAI, $make))){echo "selected";}?>>HYUNDAI</option> <option value="ISUZU" <?php if(!(strcmp(ISUZU, $make))){echo "selected";}?>>ISUZU</option> <option value="IVECO" <?php if(!(strcmp(IVECO, $make))){echo "selected";}?>>IVECO</option> <option value="JAGUAR" <?php if(!(strcmp(JAGUAR, $make))){echo "selected";}?>>JAGUAR</option> <option value="JEEP" <?php if(!(strcmp(JEEP, $make))){echo "selected";}?>>JEEP</option> <option value="JEEP" <?php if(!(strcmp(JEEP, $make))){echo "selected";}?>>JEEPA</option> <option value="LANCIA" <?php if(!(strcmp(LANCIA, $make))){echo "selected";}?>>LANCIA</option> <option value="LAND-ROVER" <?php if(!(strcmp(LAND-ROVER, $make))){echo "selected";}?>>LAND-ROVER</option> <option value="LDV" <?php if(!(strcmp(LDV, $make))){echo "selected";}?>>LDV</option> <option value="LEXUS" <?php if(!(strcmp(LEXUS, $make))){echo "selected";}?>>LEXUS</option> <option value="LOTUS" <?php if(!(strcmp(LOTUS, $make))){echo "selected";}?>>LOTUS</option> <option value="MAZDA" <?php if(!(strcmp(MAZDA, $make))){echo "selected";}?>>MAZDA</option> <option value="MCC" <?php if(!(strcmp(MCC, $make))){echo "selected";}?>>MCC</option> <option value="MERCEDES-BENZ" <?php if(!(strcmp(MERCEDES-BENZ, $make))){echo "selected";}?>>MERCEDES-BENZ</option> <option value="MG" <?php if(!(strcmp(MG, $make))){echo "selected";}?>>MG</option> <option value="MINI" <?php if(!(strcmp(MINI, $make))){echo "selected";}?>>MINI</option> <option value="MITSUBISHI" <?php if(!(strcmp(MITSUBISHI, $make))){echo "selected";}?>>MITSUBISHI</option> <option value="NISSAN" <?php if(!(strcmp(NISSAN, $make))){echo "selected";}?>>NISSAN</option> <option value="OPEL" <?php if(!(strcmp(OPEL, $make))){echo "selected";}?>>OPEL</option> <option value="PERODUA" <?php if(!(strcmp(PERODUA, $make))){echo "selected";}?>>PERODUA</option> <option value="PEUGEOT" <?php if(!(strcmp(PEUGEOT, $make))){echo "selected";}?>>PEUGEOT</option> <option value="PORSCHE" <?php if(!(strcmp(PORSCHE, $make))){echo "selected";}?>>PORSCHE</option> <option value="PROTON" <?php if(!(strcmp(PROTON, $make))){echo "selected";}?>>PROTON</option> <option value="RELIANT" <?php if(!(strcmp(RELIANT, $make))){echo "selected";}?>>RELIANT</option> <option value="RENAULT" <?php if(!(strcmp(RENAULT, $make))){echo "selected";}?>>RENAULT</option> <option value="ROVER" <?php if(!(strcmp(ROVER, $make))){echo "selected";}?>>ROVER</option> <option value="SAAB" <?php if(!(strcmp(SAAB, $make))){echo "selected";}?>>SAAB</option> <option value="SEAT" <?php if(!(strcmp(SEAT, $make))){echo "selected";}?>>SEAT</option> <option value="SKODA" <?php if(!(strcmp(SKODA, $make))){echo "selected";}?>>SKODA</option> <option value="SSANGYONG" <?php if(!(strcmp(SSANGYONG, $make))){echo "selected";}?>>SSANGYONG</option> <option value="SUBARU" <?php if(!(strcmp(SUBARU, $make))){echo "selected";}?>>SUBARU</option> <option value="SUZUKI" <?php if(!(strcmp(SUZUKI, $make))){echo "selected";}?>>SUZUKI</option> <option value="TALBOT" <?php if(!(strcmp(TALBOT, $make))){echo "selected";}?>>TALBOT</option> <option value="TOYOTA" <?php if(!(strcmp(TOYOTA, $make))){echo "selected";}?>>TOYOTA</option> <option value="TRIUMPH" <?php if(!(strcmp(TRIUMPH, $make))){echo "selected";}?>>TRIUMPH</option> <option value="TVR" <?php if(!(strcmp(TVR, $make))){echo "selected";}?>>TVR</option> <option value="VAUXHALL" <?php if(!(strcmp(VAUXHALL, $make))){echo "selected";}?>>VAUXHALL</option> <option value="VOLVO" <?php if(!(strcmp(VOLVO, $make))){echo "selected";}?>>VOLVO</option> <option value="VOLKSWAGEN" <?php if(!(strcmp(VOLKSWAGEN, $make))){echo "selected";}?>>VOLKSWAGEN</option> <option value="WESTFIELD" <?php if(!(strcmp(WESTFIELD, $make))){echo "selected";}?>>WESTFIELD</option> </select> <? $makch = $_POST['make']; ?> <select name="model"> <option value="">Select Model</option> <?php do { ?> <option value="<?php echo $rowModel['car_model_id']; ?>"><?php echo $rowModel['car_model']; ?></option> <?php }while ($rowModel = mysql_fetch_array($result)); ?> </select> <?php ?> <input class="button" type="submit" name="search" value="Search" /> </form> <br /> <? if (isset($_POST['model'])) $making = $_GET['rowModel']; echo "$makech"; ?><br><? echo "$model; session_destroy(); ?> Thanks Quote Link to comment https://forums.phpfreaks.com/topic/171656-variables-from-dropdown-menus/ Share on other sites More sharing options...
mikesta707 Posted August 24, 2009 Share Posted August 24, 2009 your $model variable never seems to be set in the following code block if (isset($_POST['model'])) $making = $_GET['rowModel']; echo "$makech"; ?><br><? echo "$model; session_destroy(); try setting it $model = $_POST['model']; echo $model Quote Link to comment https://forums.phpfreaks.com/topic/171656-variables-from-dropdown-menus/#findComment-905134 Share on other sites More sharing options...
naykidd Posted August 24, 2009 Author Share Posted August 24, 2009 I changed it, its still just repeating the make and model the same.. <? if (isset($_POST['model'])) $model = $_POST['model']; echo $makech; ?><br><? echo $model; ?><br /><? session_destroy(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/171656-variables-from-dropdown-menus/#findComment-905137 Share on other sites More sharing options...
mikesta707 Posted August 24, 2009 Share Posted August 24, 2009 hmm, well I see a different problem that is perhaps unrelated. Your if statement if (isset($_POST['model'])) doesn't do anything. There are no opening or closing brackets, so it just executes the line below, which is empty. Im assuming the 4 lines below that statement are meant to be in the if's code block, so try surrounding that stuff with brackets if (isset($_POST['model'])){ $model = $_POST['model']; echo $makech; ?><br><? echo $model; ?><br /><? session_destroy();//don't know why this is here so this may or may not belong inside the code block } beyond that, assuming your form is posing correctly, and you have the form names right (which it appears you do) I can't really spot a different problem Quote Link to comment https://forums.phpfreaks.com/topic/171656-variables-from-dropdown-menus/#findComment-905151 Share on other sites More sharing options...
naykidd Posted August 24, 2009 Author Share Posted August 24, 2009 Thanks for looking Still doing the same, rather frustrating! Im not sure if itl help, but my MySQL has a tables which matches the name you pick to a column of models (so my table has about 700 listings in total, but only displays 20/30 at a time due to the make you select) each model has the make next to it (e.g. 312, 314, 316 will each have BMW next to it) The idea of the second statement is to pull through the model number when the make is set... It does this fine, as i can select any make, and the model will appear in the second box after refreshing (The page refreshes itself upon change of the first dropdown) Maybe this is why it's pulling the make through a second time? Or could this be due to the fact i have 2 option boxes in 1 form, although i've tried splitting it up and it just forgets everything when i press submit... Quote Link to comment https://forums.phpfreaks.com/topic/171656-variables-from-dropdown-menus/#findComment-905159 Share on other sites More sharing options...
naykidd Posted August 24, 2009 Author Share Posted August 24, 2009 Not sure if anyones had any ideas? I'm on the verge of rewriting the entire combo box form in a different method... Quote Link to comment https://forums.phpfreaks.com/topic/171656-variables-from-dropdown-menus/#findComment-905324 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.