coolphpdude Posted September 2, 2008 Share Posted September 2, 2008 hi, just wondering if this can be done in php... say i wanted to create a form with 2 drop down boxes where the data in the second box was determined by the users choice for the first box. For example on the autotrader website you choose your make of car, then you can access the second drop down box and choose a model of that make. So if you choose Ford for the first box the second box would contain all cars ford make (focus, fiesta, etc...) but if you selected vauxhall the second drop down box would contain vauxhalls model (astra, vectra, etc...). Im doing something similar to this for subjects in for a school system so i'd appreciate your help. Quote Link to comment Share on other sites More sharing options...
revraz Posted September 2, 2008 Share Posted September 2, 2008 If you use a submit button, you can do it in PHP If you want it dynamic, you can use ajax or javascript Quote Link to comment Share on other sites More sharing options...
discomatt Posted September 2, 2008 Share Posted September 2, 2008 Ideally, a PHP/Javascript combination. Java alone if less dynamic results are needed. Quote Link to comment Share on other sites More sharing options...
coolphpdude Posted September 2, 2008 Author Share Posted September 2, 2008 see thing is i was going to use a table of year groups and what subjects would be available for the year (in a heirachy if you get what i mean) so going back to the order trader example you could have massive amounts of data so i was hoping to use php and mysql. if someone would recommend which way would be the best to produce what i need i would appreciate it. Or even just give me some suggestions of what to search for on google to research that kind of drop down box. Quote Link to comment Share on other sites More sharing options...
coolphpdude Posted September 2, 2008 Author Share Posted September 2, 2008 ahh you posted that while i was replying. I think that is what i was trying to get at, i need a lot of dynamic results so a kind of heirachy table structure would be necessary but i dont know how to go about it or what to research to find a solution. Quote Link to comment Share on other sites More sharing options...
aebstract Posted September 2, 2008 Share Posted September 2, 2008 I haven't gotten in to ajax much, but that should be exactly what you want to use to get this job done. Can work with php/mysql to change forms and update your table/fields on the spot without loading. Which is pretty nice when pulling information from a database, where you are browsing through and grabbing information quickly. This page might help you get started: http://www.tizag.com/ajaxTutorial/ajaxform.php Quote Link to comment Share on other sites More sharing options...
coolphpdude Posted September 2, 2008 Author Share Posted September 2, 2008 Thanks i appreciate that!!! its just selecting data to be honest, doesnt need to modify the SQL tables. i'l take a look at this though. if anyone else wants to drop a few lines of their own thoughts on this feel free! Quote Link to comment Share on other sites More sharing options...
revraz Posted September 2, 2008 Share Posted September 2, 2008 Check the FAQ area and see if there are any code snippets you can use. Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 2, 2008 Share Posted September 2, 2008 because I was bored I mocked up a quick AJAX example for dynamic select lists. This does not include a lot of validation, but will work. I leave it to you to optimize: The user page: <html> <head> <script type="text/javascript"> var xmlHttp function updateModels(makeVal) { //Disable the dependant list until it is updated document.getElementById('model').disabled = true; xmlHttp=GetXmlHttpObject(); if (xmlHttp==null) { alert ("Your browser does not support AJAX!"); return; } var url="get_models.php"; url=url+"?make="+makeVal; url=url+"&sid="+Math.random(); xmlHttp.onreadystatechange=populateModels; xmlHttp.open("GET",url,true); xmlHttp.send(null); } function GetXmlHttpObject() { var xmlHttp=null; try { // Firefox, Opera 8.0+, Safari xmlHttp=new XMLHttpRequest(); } catch (e) { // Internet Explorer try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } } return xmlHttp; } function populateModels() { if (xmlHttp.readyState==4) { //Create obj ref to the select list var selectObj = document.getElementById('model'); //Clear values from select list selectObj.options.length = 0 //Get the response from the server-side page var modelListStr=xmlHttp.responseText; //Split the results into an array var modelListAry = modelListStr.split(','); //Repopulate the list for ($i=0; $i<modelListAry.length; $i++) { var modelVals = modelListAry[$i].split('-'); selectObj.options[selectObj.length] = new Option(modelVals[1], modelVals[0]); } //re-enable the select list selectObj.disabled = false; } } </script> </head> <body onload=""> <form name="test" method="POST" action="processingpage.php"> Make: <select name="make" id="make" onchange="updateModels(this.value);"> <option value="Dodge">Dodge</option> <option value="Ford">Ford</option> <option value="Honda">Honda</option> <option value="Toyota">Toyota</option> </select> <br> Model: <select name="model" id="model"> <option value="">Not Applicable</option> </select> <br> </form> </body> </html> The server-side page <?php $returnAry = array(); //Get the make from the query string $make = $_GET['make']; //Find the models for the selected make $query = "SELECT model FROM cars WHERE make='$make'"; $result = mysql_query($query); //Create the return data while($model = mysql_fetch_assoc($result)) { $returnAry[] = $model['id'] . '-' . $model['name']; } echo implode(',', $returnAry); //The return string will be inthis format // "id1-value1,id2-value2,id3-value3, etc" ?> 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.