The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Drop down box affecting another one
sower
post Oct 8 2006, 09:30 AM
Post #1





Group: Members
Posts: 1
Joined: 8-October 06
Member No.: 367



Is it possible to create a drop down box, which would effect another drop down box?
Like when you choose an option from the first box, the next box would load new options to itself?
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Darin McGrew
post Oct 8 2006, 10:43 AM
Post #2


WDG Member
********

Group: Root Admin
Posts: 8,365
Joined: 4-August 06
From: Mountain View, CA
Member No.: 3



This kind of thing can be done with JavaScript. To make it work for those of us with JavaScript disabled/unavailable, you need to submit a form with the first select list, and have the server-side (e.g., CGI) program that processes that form send another form that includes the second select list.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
onlinespider
post Jan 8 2012, 05:38 AM
Post #3





Group: Members
Posts: 7
Joined: 8-January 12
Member No.: 16,209



I am having the same problem with my drop down related lists. Can anyone indicate how this can be done using php?



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>
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Brian Chandler
post Jan 8 2012, 06:07 AM
Post #4


Jocular coder
********

Group: Members
Posts: 2,460
Joined: 31-August 06
Member No.: 43



QUOTE
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.


You have a php program that runs on your server, accesses the database, and generates a page. The whole page. It then sends this page to a browser, where the user might select a country. But this is all happening after the page has been created.

You either need to use javascript to update the second drop-down list, or submit the page for the country, and send back the more detailed version.

With good design this can often be avoided: e.g. the link to your page says

Get details on dog-kennels (or whatever) for country [........] [GO]

(where [.....] is a drop-down list, and this is a form)

Then the user selects the country before calling the page, and then the country is available to your php program.



User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
onlinespider
post Jan 9 2012, 07:48 AM
Post #5





Group: Members
Posts: 7
Joined: 8-January 12
Member No.: 16,209



QUOTE(Brian Chandler @ Jan 8 2012, 06:07 AM) *

QUOTE
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.


You have a php program that runs on your server, accesses the database, and generates a page. The whole page. It then sends this page to a browser, where the user might select a country. But this is all happening after the page has been created.

You either need to use javascript to update the second drop-down list, or submit the page for the country, and send back the more detailed version.

With good design this can often be avoided: e.g. the link to your page says

Get details on dog-kennels (or whatever) for country [........] [GO]

(where [.....] is a drop-down list, and this is a form)

Then the user selects the country before calling the page, and then the country is available to your php program.



I am trying to keep away from javascript since it is client-side. I am not clear how to do it using PHP only?

Thank You
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
onlinespider
post Jan 9 2012, 07:49 AM
Post #6





Group: Members
Posts: 7
Joined: 8-January 12
Member No.: 16,209



is there anything which can be added to my code to make this happen?
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Darin McGrew
post Jan 9 2012, 11:22 AM
Post #7


WDG Member
********

Group: Root Admin
Posts: 8,365
Joined: 4-August 06
From: Mountain View, CA
Member No.: 3



The only way to do it without JavaScript is to have the user submit the first form, and to have the server-side (PHP) program that processes the form data return a second form with additional options.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Brian Chandler
post Jan 10 2012, 03:44 AM
Post #8


Jocular coder
********

Group: Members
Posts: 2,460
Joined: 31-August 06
Member No.: 43



QUOTE
I am trying to keep away from javascript since it is client-side. I am not clear how to do it using PHP only?


I'm trying to help you understand why you can't just stick some magic code in a php program and cause something someone else does later to change it.

As I said, often the neat way is to put the top level selection in the initial access. Instead of a link "Find a hospital", make a link which says

"Find a hospital in [@@@@@] [Go]"

where [@@@@@] is a drop-down list of countries, and [Go] is a submit button (this of course has to be a form.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
onlinespider
post Jan 10 2012, 12:37 PM
Post #9





Group: Members
Posts: 7
Joined: 8-January 12
Member No.: 16,209



QUOTE(Brian Chandler @ Jan 10 2012, 03:44 AM) *

QUOTE
I am trying to keep away from javascript since it is client-side. I am not clear how to do it using PHP only?


I'm trying to help you understand why you can't just stick some magic code in a php program and cause something someone else does later to change it.

As I said, often the neat way is to put the top level selection in the initial access. Instead of a link "Find a hospital", make a link which says

"Find a hospital in [@@@@@] [Go]"

where [@@@@@] is a drop-down list of countries, and [Go] is a submit button (this of course has to be a form.



Dear Brian,

I think i am now understanding. it has to be divided over 2 pages, one sending variables to the next page and then loading the second drop down menu options.

Thank You for your help.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Brian Chandler
post Jan 10 2012, 10:59 PM
Post #10


Jocular coder
********

Group: Members
Posts: 2,460
Joined: 31-August 06
Member No.: 43



QUOTE
I think i am now understanding. it has to be divided over 2 pages, one sending variables to the next page and then loading the second drop down menu options.


Right! The point to note about your original attempt is that **in the php program**, even when you get to the bottom of the section including <select name="country">, all you have done is written these instructions to output. It's easy to look at them and say "Ah, now we've selected the country...", but this is a misunderstanding. (And confusions like this are very common, so it's a big step forward to sort it out in your own head...)

User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
onlinespider
post Jan 13 2012, 01:15 AM
Post #11





Group: Members
Posts: 7
Joined: 8-January 12
Member No.: 16,209



QUOTE(Brian Chandler @ Jan 10 2012, 10:59 PM) *

QUOTE
I think i am now understanding. it has to be divided over 2 pages, one sending variables to the next page and then loading the second drop down menu options.


Right! The point to note about your original attempt is that **in the php program**, even when you get to the bottom of the section including <select name="country">, all you have done is written these instructions to output. It's easy to look at them and say "Ah, now we've selected the country...", but this is a misunderstanding. (And confusions like this are very common, so it's a big step forward to sort it out in your own head...)



Thanks a lot for your help. I will try to sort it out myself and post the code on success.
Very kind of you.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
onlinespider
post Jan 13 2012, 08:30 AM
Post #12





Group: Members
Posts: 7
Joined: 8-January 12
Member No.: 16,209



QUOTE(onlinespider @ Jan 13 2012, 01:15 AM) *

QUOTE(Brian Chandler @ Jan 10 2012, 10:59 PM) *

QUOTE
I think i am now understanding. it has to be divided over 2 pages, one sending variables to the next page and then loading the second drop down menu options.


Right! The point to note about your original attempt is that **in the php program**, even when you get to the bottom of the section including <select name="country">, all you have done is written these instructions to output. It's easy to look at them and say "Ah, now we've selected the country...", but this is a misunderstanding. (And confusions like this are very common, so it's a big step forward to sort it out in your own head...)



Thanks a lot for your help. I will try to sort it out myself and post the code on success.
Very kind of you.



i have found a perfect solution on

http://www.plus2net.com/php_tutorial/php_drop_down_list.php
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
onlinespider
post Jan 13 2012, 08:31 AM
Post #13





Group: Members
Posts: 7
Joined: 8-January 12
Member No.: 16,209



QUOTE(onlinespider @ Jan 13 2012, 08:30 AM) *

QUOTE(onlinespider @ Jan 13 2012, 01:15 AM) *

QUOTE(Brian Chandler @ Jan 10 2012, 10:59 PM) *

QUOTE
I think i am now understanding. it has to be divided over 2 pages, one sending variables to the next page and then loading the second drop down menu options.


Right! The point to note about your original attempt is that **in the php program**, even when you get to the bottom of the section including <select name="country">, all you have done is written these instructions to output. It's easy to look at them and say "Ah, now we've selected the country...", but this is a misunderstanding. (And confusions like this are very common, so it's a big step forward to sort it out in your own head...)



Thanks a lot for your help. I will try to sort it out myself and post the code on success.
Very kind of you.



i have found a perfect solution on

http://www.plus2net.com/php_tutorial/php_drop_down_list.php



Thank you for those who helped in some way
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 25th April 2024 - 06:37 AM