tontox5 Posted June 10, 2008 Share Posted June 10, 2008 Hello lads, I'm new here and was hoping I could get some help with a hurdle I ran into. I'm making a motorcycle classified site and have all of my makes and models stored in a mysql database. What I want to do is have the Models load after Make is selected without having to submit a form. Do I need to use javascript? Here is my code: <form name="form1" method="post" action="test-manufacturers.php"><?php mysql_connect('localhost','***','***'); mysql_select_db('database'); if (isset($_REQUEST['make'])) $make=$_REQUEST['make']; $result= mysql_list_fields ('database','manufacturers'); $max=mysql_num_fields($result); //start of the drop down menu echo "<select name='make'>"; $farray=Array(); $i=0; for ($x=0; $x < $max; $x++) { if ($make==mysql_field_name($result, $x)) { $farray[$i++]="<option value='". mysql_field_name($result, $x)."' selected>". mysql_field_name($result, $x). "</option >"; } else { $farray[$i++]="<option value='". mysql_field_name($result, $x)."'>". mysql_field_name($result, $x). "</option>"; } } sort($farray); foreach($farray as $f){ echo $f; } echo "</select><br/><br/>"; // end of the drop down menu if (isset($make)) { echo "<select name='model'>"; $sql="SELECT * FROM manufacturers WHERE $make !=''"; $result= mysql_query($sql); $farray=Array(); while ($row = mysql_fetch_array($result)) { $farray[$i++]="<option value='". $row[$make]."'>". $row[$make]. "</option>"; } sort ($farray); foreach($farray as $f){ echo $f; } echo "<option value='Other'>Other</option>"; echo "</select><br/><br/>"; } ?> <input type="submit" name="submit" id="submit" value="List"> </form> Quote Link to comment Share on other sites More sharing options...
revraz Posted June 10, 2008 Share Posted June 10, 2008 http://www.phpfreaks.com/forums/index.php/topic,155984.0.html Quote Link to comment Share on other sites More sharing options...
tontox5 Posted June 11, 2008 Author Share Posted June 11, 2008 No offense to the writer, the concept looks good and there are 50,000 views, but the example is very hard to understand. The writer says it all works as one file, but then says "you can replace with" and then finally says "try this first" for just the bottom snippet of code. I copied and pasted the code, and I am getting the following error Parse error: syntax error, unexpected T_IF in /var/www/vhosts/***.com/httpdocs/menu.php on line 26 It is being caused by the following code, line 26 being if (!mysql_query("SELECT * FROM table WHERE ID=$P")) $dbh = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL"); $selected = mysql_select_db("first_test",$dbh) if (!mysql_query("SELECT * FROM table WHERE ID=$P")) { echo "Database is down"; } while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) { $NewData .= "<option value='".$row['ID']."'>".$row['Name']."</option>\n"; } mysql_close($dbh); Any ideas? Quote Link to comment Share on other sites More sharing options...
Barand Posted June 11, 2008 Share Posted June 11, 2008 if you have a structure like this, with numeric PKs and FKs then the baaSelect link in my sig should help. Otherwiise there's an ajax sample too. [pre] make model --------- ------------- id (PK) ---+ id (PK) make | model +---< makeID (FK) Quote Link to comment Share on other sites More sharing options...
revraz Posted June 11, 2008 Share Posted June 11, 2008 That error means you are simply missing a semi colon $selected = mysql_select_db("first_test",$dbh); <------ Quote Link to comment Share on other sites More sharing options...
tontox5 Posted June 11, 2008 Author Share Posted June 11, 2008 Thank you Barand, your baaSelect example was exactly what I needed. It's much simpler than the first link. Now that it works, I just need to analyze it until I understand it fully or my brain explodes, whichever comes first. Thanks for your help, solved. Quote Link to comment Share on other sites More sharing options...
Barand Posted June 11, 2008 Share Posted June 11, 2008 The basic principle: it creates a javascript array of the models for every make. When you select a make, the second dropdown is poulated with it's models using its array. To make life easier, it also writes all the js scripts that are required for you. 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.