jrod356a Posted February 24, 2009 Share Posted February 24, 2009 Hi All, I am totally new to developing in PHP and I am having problems figuring out how to do the following: I am currently trying to create a data entry web form for entering car parts info into a mysql database. I have added some dropdown form fields to allow the user to select a vehicle manufacturer and a vehicle model. I would like to add conditional processing that would populate the vehicle model dropdown field (form field is named "MODEL") with the appropriate models for the vehicle manufacturer (form field is named "MAKE")that the user selected. For example, if the user selects "Acura" as the manufacturer then the vehicle model dropdown should only display a list of all the Acura models, if "Honda" is selected then only Honda models are displayed in the models dropdown, etc. In my mysql database, I have a table named "MAKE_MODELS" and it contains a row entry for each vehicle model for each vehicle manufacturer. I.e: MAKE MODEL ----- ------- ACURA CL ACURA NSX HONDA ACCORD HONDA CIVIC and so on... I believe that the logic should be something like this: If the selected value of the MAKE field is "Acura" then select MAKE_MODELS.MODEL where MAKE_MODELS.MAKE = "Acura"; populate list values for MODEL field with returned results of select query I'm not sure if I would create else/elseif statements for each MAKE selection condition or if this should be handled with a "CASE" routine (not sure if PHP has a "CASE" statement?). Again, I am totally new to PHP development and as such, I may be way off point from how this should actually be handled so any help/guidance that anyone can provide me with will be greatly appreciated! Quote Link to comment Share on other sites More sharing options...
phpdragon Posted February 24, 2009 Share Posted February 24, 2009 This is done with AJAX, my example here is done with countries and states you can change it to suit your needs, the method I use is:- 1.Insert a javascript to perform the ajax call, this scripts has 3 elements you can configure, they are var strURL value, in this case (statecall.php?countrycode) this is the page it calls and loads the second select box with. The other is document.getElementById, in this case (statediv), this is the name of the div tag that will be updated in your page. here is the script for the head of your page <script language="javaScript" type="text/javascript"> function getXMLHTTP() { //fuction to return the xml http object var xmlhttp=false; try{ xmlhttp=new XMLHttpRequest(); } catch(e) { try{ xmlhttp= new ActiveXObject("Microsoft.XMLHTTP"); } catch(e){ try{ xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch(e1){ xmlhttp=false; } } } return xmlhttp; } function getState(codeId) { var strURL="statecall.php?countrycode="+codeId; var req = getXMLHTTP(); if (req) { req.onreadystatechange = function() { if (req.readyState == 4) { // only if "OK" if (req.status == 200) { document.getElementById('statediv').innerHTML=req.responseText; } else { alert("There was a problem while using XMLHTTP:\n" + req.statusText); } } } req.open("GET", strURL, true); req.send(null); } } </script> 2. Create your form on the same page, I use tables for layout so below are the two rows of my form for your example. notice the onchange value in countries and the div tag in the second row that includes the state select box. form code for select boxes <tr> <td align="left" valign="middle"><label for="country">State: </label> <select name="country" id="country" onChange="getState(this.value);"> <option value="--Please Select Country--">--Please Select Country--</option> <?php // print category combo box $sql = "SELECT name,ccode FROM countries ORDER BY name ASC"; $result = mysql_query($sql); while ($result_row = mysql_fetch_array($result)) { $cname = $result_row["name"]; $cid = $result_row["ccode"]; echo "<option value='$cid'"; if ($usercountry==$cid) { echo " selected='selected'"; } echo ">$cname</option>\n"; } ?> </select> <font face="Geneva, Arial, Helvetica, sans-serif" color="#FF0000"><b>R</b></font></td> </tr> <tr> <td align="left" valign="middle"><label for="state"> State: </label><div id="statediv"><?php include('statecall.php'); ?></div></td> </tr> 3. Create the select box to be included in my case statecall.php. statecall.php <select name="state" id="state"> <option value='--Please Select State/Province--'>--Please Select State/Province--</option> <?php include('data_connect.php'); // print state select box // convert country code to country name if (!$countrycode) { $countrycode = $usercountry; } $getcnty = "SELECT * FROM countries WHERE ccode='$countrycode'"; $resultcnty = mysql_query($getcnty); $selectcnty = mysql_fetch_array($resultcnty); $cntyname = $selectcnty["name"]; $querystate = "SELECT * FROM states WHERE code='$countrycode' ORDER BY nameorder"; $result = mysql_query($querystate); while($row_result = mysql_fetch_array($result)) { $statename = $row_result["name"]; if ($statename) { if ($statename!=$userstate) { echo "<option value='$statename'>$statename</option>\n"; } } } if (!empty($userstate)) { echo "<option value='$userstate' selected='selected'>$userstate</option>\n"; } if (!$statename) { if (!empty($cntyname)) { echo "<option value='$cntyname' selected='selected'>$cntyname</option>\n"; } } ?> </select> <font face="Geneva, Arial, Helvetica, sans-serif" color="#FF0000"><b>R</b></font> The javascript sends the value to the newly called in page which starts empty first time round, when a counrty is selected the corresponding states are called from the database and populate the state select box. I also have error checking on my form so the code for selected values I show in the country and state select boxes is to remember the users selected option. And thats it, Quote Link to comment Share on other sites More sharing options...
jrod356a Posted February 25, 2009 Author Share Posted February 25, 2009 phpdragon, Thank you so much for your quick reply! I will try this out and let you know things went. Again, thank you! Quote Link to comment Share on other sites More sharing options...
jrod356a Posted February 25, 2009 Author Share Posted February 25, 2009 Hey phpdragon, I've been reviewing the code sample you provided me and I'm seeing some things that I am having trouble understanding. As I mentioned in my original post, I am totally new to php and actually, while I have some development experience in other program languages, I don't consider myself an "Expert" developer in any language (I'd say I'm a "novice" to "intermediate" level developer) so please accept my apologies and I ask for your patience with me if I'm showing my ignorance of something that should be pretty basic knowledge! Anyway, here are my questions: 1. The AJAX code should be added in the head area of an ".html" page (not a ".php" file), right? 2. Can you please clarify for me what the values of "var strURL=" represent? I get that "statecall.php" is the php file that contains the select box to be included in your case statecall.php but what do the "?countrycode=" and "+codeId;" values represent? 3. Regarding the "statediv" part where you say "this is the name of the div tag that will be updated in your page.", is this where the select box will be included? 4. In the statecall.php code, I see that there is an include statement with a value of 'data_connect.php'. Is this supposed to be the php file that contains the database connection parameters? If so, should this file be stored in the same directory that the statecall.php file is stored in? I ask this because I noticed that there is no directory path info included with this value. I think that everything else is pretty much self-explanitory (hopefully!) and I'm pretty confident that I understand it. Thanks again for your help so far and thanks in advance for any input you can provide to clarify these questions and please accept my sincerest apologies for any inconveniences my ignorance may be imposing! :-D - jrod356a Quote Link to comment Share on other sites More sharing options...
phpdragon Posted February 25, 2009 Share Posted February 25, 2009 Hi jrod 1. The page needs to be a php page, php produces html to the client side, the php extension is so your php code works, your ajax will still work in the php page, you code it the same as a html page except you insert your php scripts where evere you need to perform the functions. All processing and session handling is usually done above all html output at the start of the page. like mypage.php <?php some php script ?> <!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=iso-8859-1" /> <title>Title of my page</title> <script my javascripts here> </head> <body> </head> <body> My page content here <?php some php generated code here ?> more standard html here </body> </html> 2.using a question mark after a page name means that you are passing variables to the page you are going to, in this case we are calling the page statecall and passing the countrycode variable to it to determin which states to list. (+codeId) is the value the user selects from the country select box. You will see in my example some php which makes a call to the database to determine these values for teh select box then writes the select box to the page. 3.Correct you place a div tag <div name='statediv'>select box to include here</div>, it os the contents of this div tag thats gets replaced on the page instead of the whole page reloading, basically it reloads the statecall.php page with the new variable passed to it determining which country states to list. You can call any variable or any div tag any name you like as long as you refer to them correctly. Looking through my example you will see how the work. 4. Yes data_connect.php is my database connection script, I use it on many pages so I keep iit on a seperate page and just include it, this way if I need to modify the database connections for any reason then all pages get updated without me hunting for them. I have all 3 of these files in my base web directory, you can put them in any directory you like as long as you refer to them that way. example of statecall.php being in a directory called includes include('includes/statecall.php'); if statecall.php is in the main web directory it would be include('statecall.php'); Hope that helps you out Quote Link to comment Share on other sites More sharing options...
jrod356a Posted March 15, 2009 Author Share Posted March 15, 2009 hi phpdragon, Wow! Thank you so much for clarifying things for me! - JROD 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.