Jump to content

Populate text box from mysql based upon a dropdownbox selection.


scotty22

Recommended Posts

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.

 

Monkeytooth suggested on the PHP forum on here . . . ."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. "

 

.....but I'm struggling a bit - never used AJAX and only used a small bit of Javascript - the jQuery I have downloaded but struggling to find a solution.

 

Thanks in advance for any help

Link to comment
Share on other sites

Don't think you need ajax for this. When you query for the counties you can also get the associated region. pass all of it into the js section as an array. When the county is selected do a look up in js to come up with the region and write it to the output.

 

$area[0][0] = county

and $area[0][1] = corresponding region

 

$area[1][0] = different county and the next selection in drop down box.

$area[1][1] = corresponding region to be written.

 

Hope this makes sense.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.