Jump to content

Select Box populating another select box


onlinespider

Recommended Posts

Dear All,

 

I wish to have 2 drop down boxes, Country Select Box and Locality Select Box. The locality select box will be affected by the value chosen in the country select box.

 

All is working fine except that the locality select box is not being populated.

I know that the problem is in the sql statement

 

WHERE country_id='$co'

 

because i am having an error that $co is an undefined variable.

 

All the rest works fine because i have replaced the $co variable directly with a number (say 98) for a particular country id and it worked fine. In what way can i define this variable $co so that it is accepted by my sql statement?

 

Thank you for your help in advance.

 

MySQL Tables indicated below:

 

CREATE TABLE countries(

country_id INT(3) UNSIGNED NOT NULL AUTO_INCREMENT,

country_name VARCHAR(30) NOT NULL,

PRIMARY KEY(country_id),

UNIQUE KEY(country_name),

INDEX(country_id),

INDEX(country_name))

ENGINE=MyISAM;

 

CREATE TABLE localities(

locality_id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,

country_id INT(3) UNSIGNED NOT NULL,

locality_name VARCHAR(50),

PRIMARY KEY (locality_id),

INDEX (country_id),

INDEX (locality_name))

ENGINE=MyISAM;

 

Extract PHP script included below:

 

// connect to database

require_once(MYSQL);

 

if(isset($_POST['submitted']))

 

{

 

// trim the incoming data

 

/* this line runs every element in $_POST through the trim() function, and assigns the returned result to the new $trimmed array */

$trimmed=array_map('trim',$_POST);

 

// clean the data

 

$co=mysqli_real_escape_string($dbc,$trimmed['country']);

 

$lc=mysqli_real_escape_string($dbc,$trimmed['locality']);

 

}

 

?>

 

<form action="form.php" method="post">

 

<p>Country

<select name="country">

<option>Select Country</option>

 

<?php

 

$q="SELECT country_id, country_name

FROM countries";

 

$r=mysqli_query($dbc,$q) or

trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));

 

while($row=mysqli_fetch_array($r))

 

{

 

$country_id=$row[0];

$country_name=$row[1];

echo '<option value="' . $country_id . '"';

if(isset($trimmed['country']) && ($trimmed['country']==$country_id))

echo 'selected="selected"';

echo '>' . $country_name . '</option>\n';

 

}

 

?>

 

</select>

</p>

 

<p>Locality

<select name="locality">

<option>Select Locality</option>

 

<?php

 

$ql="SELECT locality_id, country_id, locality_name

FROM localities

WHERE country_id='$co'

ORDER BY locality_name";

 

$rl=mysqli_query($dbc,$ql) or

trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));

 

while($row=mysqli_fetch_array($rl))

 

{

 

$locality_id=$row[0];

$country_id=$row[1];

$locality_name=$row[2];

echo '<option value="' . $locality_id . '"';

if(isset($trimmed['locality']) && ($trimmed['locality']==$locality_id))

echo 'selected="selected"';

echo '>' . $locality_name . '</option>\n';

 

}

 

// close database connection

mysqli_close($dbc);

 

?>

 

</select>

</p>

 

<p><input type="submit" name="submit" value="Submit" /></p>

 

<input type="hidden" name="submitted" value="TRUE" />

 

</form>

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.