Jump to content

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.

You sir are on the wrong forum then what you want is 5 forums down and 3rd door on the right..  :D

 

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.

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.