scotty22 Posted July 4, 2011 Share Posted July 4, 2011 Hi, many thanks to you all for all your helpful posts and answers - I have now got stuck myself so would appreciate your help! I am trying to populate a textbox based upon the selection of a dropdown box. The dropdown box contents are retrieved via PHP from my mysql database. The dropdown box lists all the 'counties' in the UK. Once selected I would like a text box to display the matching 'region' that 'county' is in - without a submit. However, if this is not possible (or overly complex) - it is not essential to display the associated 'region' at all as long as onsubmit the 'region' is fired to a further database that is storing all the form submissions - this is with a view that that database can be searched by 'region'. The MYSQL 'county_regions' table contains just three fields - 'CountyID', 'County' and 'Region'. I have looked at javascript and due to the amount of 'counties' this seems to be the wrong way to go so would love a php solution. Here is the selection of code at the moment. Would be grateful for your suggestions. <?php $link = mysql_connect('host', 'username', 'password') or die('Could not connect: ' . mysql_error()); mysql_select_db('databasename') or die('Could not select database'); $query = "select `CountyID`,`County` from `county_regions`"; $result = mysql_query($query) or die('Query failed: ' . mysql_error()); ?> <label for="County">County</label> <select name="County"> <?php while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){ echo '<option value="'.$row['County'].'"'. (isset($_SESSION['County']) && $_SESSION['County'] == $row['County'] ? 'selected="selected"' : '').'>'. $row['County'].'</option>';} ?> </select> <font size="1" face="arial" color="red">Required field</font><br /> <br/> <label for="Region">Region</label> <input type="text" size="24"id="Region" name="Region" /><br /> Thanks in advance for your help. Quote Link to comment https://forums.phpfreaks.com/topic/241048-populate-text-box-from-mysql-based-upon-a-dropdownbox-selection/ Share on other sites More sharing options...
monkeytooth Posted July 4, 2011 Share Posted July 4, 2011 You sir are on the wrong forum then what you want is 5 forums down and 3rd door on the right.. What you want can be achieved with PHP backing it, but without submitting the page via a form and reloading the page based on that form submission you will need to use AJAX which is mostly JavaScript sending data to a PHP file, which the php file pulls the stuff from the DB which then sends data back through the PHP to the JavaScript. Generally speaking a lot easier then it sounds here. I suggest you pick up jQuery (really flexible and fairly extensible JavaScript library that makes what I just said rather easy). You will want to make use likely of jQuery's post() function. Its worth mentioning your PHP stays relatively the same so your not really needing to learn anything special there. Quote Link to comment https://forums.phpfreaks.com/topic/241048-populate-text-box-from-mysql-based-upon-a-dropdownbox-selection/#findComment-1238142 Share on other sites More sharing options...
scotty22 Posted July 4, 2011 Author Share Posted July 4, 2011 Many thanks. will do some research on AJAX, jQuery and post in other forum if I can't figure it out. Thanks for the pointer. Quote Link to comment https://forums.phpfreaks.com/topic/241048-populate-text-box-from-mysql-based-upon-a-dropdownbox-selection/#findComment-1238151 Share on other sites More sharing options...
monkeytooth Posted July 4, 2011 Share Posted July 4, 2011 http://www.jquery.com Quote Link to comment https://forums.phpfreaks.com/topic/241048-populate-text-box-from-mysql-based-upon-a-dropdownbox-selection/#findComment-1238163 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.