how to pass value from dropdown list to another in 1 form?

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

User avatar
iso-latte-d
New php-forum User
New php-forum User
Posts: 14
Joined: Tue Jul 22, 2003 5:38 pm
Location: Melbourne, Australia

how to pass value from dropdown list to another in 1 form?

Postby iso-latte-d » Thu Jul 24, 2003 5:22 am

another newbie question...
let say i have a form...with 2 drop down lists.
contents of list2 depends on the choice in list1.

Code: Select all

<tr>
  <td>select a manufacturer</td>
  <td><select name="selectcar" size="1">
            <?php
                  //database connection and query to display car
                  //eg. list=mazda,nissan,honda
                         choice=mazda
             ?>
        </select></td>
</tr>

<tr>
  <td>car model </td>
  <td><select name="selectmodel" size="1">
             <?php
                  //query = "select carmodel from car
                                 where manufacturer =
                                 'some value from selectcar'";
                     then display car model
                  //eg. list=rx-7,rx-8
              ?>
         </select></td>
</tr>

if both lists are in the same form, how do i pass value from list1 to list2?

thanks

User avatar
swirlee
Moderator
Moderator
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back
Contact:

Postby swirlee » Thu Jul 24, 2003 5:45 am

I can't be sure, but what I'm guessing you're after is this: The user selects an item from the "make" menu, say "mazda", and the "model" is immediately populated with all the different models with "mazda" as the manufacturer.

You can't do this with PHP alone. Once a PHP page is done being sent to the browser, you cannot "add" information to the page. Instead, you have to use JavaScript that will tell the browser "when the user selects X from menu1, fill menu2 with values that match X". The following article links to two very good tutorials on the subject:

http://www.faqts.com/knowledge_base/view.phtml/aid/6710/fid/51

If you don't want to use JavaScript, you're going to have to do this in two pages -- the user will have to select an item from the first menu, which will submit the form to a PHP script, which will then generate the second menu on a new page.

User avatar
iso-latte-d
New php-forum User
New php-forum User
Posts: 14
Joined: Tue Jul 22, 2003 5:38 pm
Location: Melbourne, Australia

Postby iso-latte-d » Thu Jul 24, 2003 7:51 am

hi swirlee,

yep, that's what i'm after.
i guess, it's now time for me to learn javascript as well.
apparently most of php scripts (server-side) will need some element of javascript (client-side) to complement each other.

btw, thanks for the info...

User avatar
swirlee
Moderator
Moderator
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back
Contact:

Postby swirlee » Thu Jul 24, 2003 10:32 am

iso-latte-d wrote:apparently most of php scripts (server-side) will need some element of javascript (client-side) to complement each other.


I'd tend to disagree. I see very few, very good PHP apps (or web apps in general) for which JavaScript is an essential part. Your example here is perhaps one of few examples of good use of JavaScript with PHP. But in the vast majority of solid web apps, JavaScript is only required for trivialities, nuance, or, as in the case of things like client-side form validation (which, I remind everyone, is useless to the administrator without server-side validation to back it up), as a convenience to the user.

I will forever go to great lengths to avoid using JavaScript as an intrinsic part of a web app, and I think you'll find that this is the case with many (most?) professional web developers (for the record, I don't consider myself a professional just yet), though certainly its occasional necessity is recognized.


Return to “PHP coding => General”

Who is online

Users browsing this forum: No registered users and 1 guest

cron