Jump to content

Populate second dropdown list based upon selection of first - neraly solved!


Recommended Posts

Hi all,

 

I have been struggling on a bit of code for a while now. I need to populate a second drop down list (Region) based upon the selection of the first (County).

I have found a piece of code that works on its own and have adapted to suit my needs -  see below. However, when I drop it into my main php page the javascript is not working. It's because of the formObject but I just don't know enough to resolve this! Furthermore, I need the textboxes already completed in the form to retain their value once the javascript kicks in.

 

start of working example code

 

<?php

 

$link = mysql_connect('myhost', 'myusername', 'mypassword') or die('Could not connect: ' . mysql_error());

mysql_select_db('mydatabase') or die('Could not select database');

 

if(isset($_GET["County"]) && is_numeric($_GET["County"]))

{

$County = $_GET["County"];

}

 

if(isset($_GET["Region"]) && is_numeric($_GET["Region"]))

{

$Region = $_GET["Region"];

}

 

?>

 

<script language="JavaScript">

 

function autoSubmit()

{

var formObject = document.forms['theForm'];

formObject.submit();

}

 

</script>

 

<form name="theForm" method="get">

 

<!-- County SELECTION BASED ON city VALUE -->

 

<?php

 

?>

 

<select name="County" onChange="autoSubmit();">

<option value=''</option>

 

<?php

 

//POPULATE DROP DOWN MENU WITH COUNTRIES FROM A GIVEN city

 

$sql = "SELECT * FROM county_regions";

$counties = mysql_query($sql,$link);

 

while($row = mysql_fetch_array($counties))

{

echo ("<option value=\"$row[CountyID]\" " . ($County == $row["CountyID"]? " selected" : "") . ">$row[County]</option>");

}

 

?>

 

</select>

 

<?php

 

?>

 

<br><br>

 

<?php

 

if($County!= null && is_numeric($County))

{

 

?>

 

<select name="Region" onChange="autoSubmit();">

 

 

<?php

 

//POPULATE DROP DOWN MENU WITH RegionS FROM A GIVEN city, County

 

$sql = "SELECT * FROM county_regions WHERE CountyID = $County ";

$Regions = mysql_query($sql,$link);

 

while($row = mysql_fetch_array($Regions))

{

echo ("<option value=\"$row[CountyID]\" " . ($Region == $row["CountyID"]? " selected" : "") . ">$row[Region]</option>");

}

 

?>

 

</select>

 

<?php

 

}

 

?>

 

end of code

 

Here is my page - edited quite a bit to save on space!

 

Start of my code where the Javascript is not working

 

<head>

 

<script language="JavaScript">

 

function autoSubmit()

{

var formObject = document.forms['subform'];

formObject.submit(); :shrug:

}

 

</script>

 

</head>

 

<form enctype="multipart/form-data" method="post" action="add_attraction01.php" FORM NAME="FormName">    :shrug:

<input type="hidden" name="MAX_FILE_SIZE" value="32768" />   

<label for="Business_name">Business Name</label>   

<input type="text" size="60" STYLE="color: #FFFFFF; font-family: Arial, Helvetica, sans-serif; font-weight: bold; font-size: 12px; background-color: #72A4D2;"   

<id="Business_name" name="Business_name" maxlength=60/><font size="1" face="arial" color="red">Required field</font><br />   

<label for="StreetAddress">Address</label>   

<input type="text" size="60" rows="2" id="StreetAddress" name="StreetAddress" maxlength=120/><font size="1" face="arial" color="red">Required field</font><br />   

<label for="Town">Town</label>    <input type="text" size="25" id="Town" name="Town" maxlength=25/><font size="1" face="arial" color="red">Required field</font><br />

 

<?php

 

$link = mysql_connect('myhost', 'myusername', 'mypassword') or die('Could not connect: ' . mysql_error());

mysql_select_db('mydatabase') or die('Could not select database');

 

if(isset($_GET["County"]) && is_numeric($_GET["County"]))

{

$County = $_GET["County"];

}

 

if(isset($_GET["Region"]) && is_numeric($_GET["Region"]))

{

$Region = $_GET["Region"];

}

 

?>

 

<form name = "subform" method="get"> :shrug:

 

<select name="County" onChange="autoSubmit();"> :shrug:

<option value=''</option>

 

<?php

 

$sql = "SELECT * FROM county_regions";

$counties = mysql_query($sql,$link);

 

while($row = mysql_fetch_array($counties))

{

echo ("<option value=\"$row[CountyID]\" " . ($County == $row["CountyID"]? " selected" : "") . ">$row[County]</option>");

}

 

?>

 

</select>

 

<?php

 

?>

 

<br><br>

 

<?php

 

if($County!= null && is_numeric($County))

{

 

?>

 

<select name="Region" onChange="autoSubmit();">

 

 

<?php

 

$sql = "SELECT * FROM county_regions WHERE CountyID = $County ";

$Regions = mysql_query($sql,$link);

 

while($row = mysql_fetch_array($Regions))

{

echo ("<option value=\"$row[CountyID]\" " . ($Region == $row["CountyID"]? " selected" : "") . ">$row[Region]</option>");

}

 

?>

 

</select>

 

<?php

 

}

 

?>

<input type="text" size="20"id="Tel_No" name="Tel_No" maxlength=20 onkeypress="return isNumberKey(event)"/><font size="1" face="arial" color="red">Required field</font><br />   

<br/>   

<input type="submit" value="Submit your attraction" name="submit" onclick="return BothFieldsIdenticalCaseSensitive();"/> 

</form>

</body>

</html>

 

end of code

 

It's probably obvious to you guys!!

 

Thanks in advance for your help.

when specifying a <script> tag, you must specify the type attribute....the language attribute has been deprecated in favor of the type attribute, however you can keep the language attribute if you wish

 

<script type="text/javascript" language="JavaScript">

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.