Results 1 to 6 of 6

Thread: Displaying Data from a Drop Down List

  1. #1
    Member stu1903's Avatar
    Join Date
    Jan 2009
    Posts
    51

    Default Displaying Data from a Drop Down List 5 May 2011 @ 11.14

    I'm struggling with displaying details from a selected option in a drop down list.

    I have a drop down list which is populated from a MYSQL query. I want the user to select an option and the values associated pulled from the database and displayed.

    The dynamically populated drop down list is for "Brand" and when a user selects the brand from the drop down list I want the "supplier" of that brand to be displayed.

    The code below is for dynamically populating the drop down list. I'm just not sure on how to produce the next step.

    Code:
     
    <form id="form1" name="form1" method="get" action="<?php echo $PHP_SELF; ?> ">
    <strong>Brand:</strong>
    <select name="select">
    <option value="">--- Please Select Brand ---</option>
    <?php
    // Get records from database (table "name_list").
    $list=mysql_query("select * from who_supplies_us_what order by brand asc");
    // Show records by while loop.
    while($row_list=mysql_fetch_assoc($list)){
    ?>
    <option value="<?php echo $row_list['brand']; ?>" <?php if($row_list['brand']==$select){ echo "selected"; } ?>><?php echo $row_list['brand']; ?></option>
    <?php
    // End while loop.
    }
    ?>
    </select> 
    <input type="submit" name="Submit" value="Select" />
    </form>
    Many thanks
      Reply With Quote

  2. #2
    Member janvt's Avatar
    Join Date
    Jan 2011
    Posts
    182

    Default 5 May 2011 @ 11.38

    well, when the user submits the form, the selected brand will be stored in $_POST['select']. You can then use this value to select the supplier?

    Pseudocode:
    Code:
    <?php
    
    if(isset($_POST['select']))
    {
      $brand=mysql_real_escape_string($_POST['select']);
      $result=mysql_query("SELECT supplier FROM suppliers WHERE brand='".$brand."';
      $row=mysql_fetch_assoc($result);
      print $row['supplier'];
    }
    
    ?>
      Reply With Quote

  3. #3
    Member stu1903's Avatar
    Join Date
    Jan 2009
    Posts
    51

    Default 5 May 2011 @ 11.47

    quote
    well, when the user submits the form, the selected brand will be stored in $_POST['select']. You can then use this value to select the supplier?

    Pseudocode:
    Code:
    <?php
    
    if(isset($_POST['select']))
    {
    $brand=mysql_real_escape_string($_POST['select']);
    $result=mysql_query("SELECT supplier FROM suppliers WHERE brand='".$brand."';
    $row=mysql_fetch_assoc($result);
    print $row['supplier'];
    }
    
    ?>
    Originally Posted by janvt View Post
    Many thanks for the quick response I'll give this a try.
      Reply With Quote

  4. #4
    Member stu1903's Avatar
    Join Date
    Jan 2009
    Posts
    51

    Default 5 May 2011 @ 12.28

    quote
    well, when the user submits the form, the selected brand will be stored in $_POST['select']. You can then use this value to select the supplier?

    Pseudocode:
    Code:
    <?php
     
    if(isset($_POST['select']))
    {
      $brand=mysql_real_escape_string($_POST['select']);
      $result=mysql_query("SELECT supplier FROM suppliers WHERE brand='".$brand."';
      $row=mysql_fetch_assoc($result);
      print $row['supplier'];
    }
     
    ?>
    Originally Posted by janvt View Post
    Many thanks worked a treat!

    Only thing is for some of my brands I have several suppliers for and it only returns one supplier. How to I display all suppliers?

    Once again many thanks for your quick response much appreciated.
      Reply With Quote

  5. #5
    Member stu1903's Avatar
    Join Date
    Jan 2009
    Posts
    51

    Default ****** solved ****** 5 May 2011 @ 14.06

    Got it sorted Now. Many thanks for your help.

    For anyone interested the I have posted the completed code below.

    Code:
    <form id="form1" name="form1" method="POST" action="<?php echo $PHP_SELF; ?> ">
    <strong>Brand:</strong>
    <select name="select">
    <option value="">--- Please Select Brand ---</option>
    <?php
    // Get records from database (table "name_list").
    $list=mysql_query("select * from who_supplies_us_what order by brand asc");
    // Show records by while loop.
    while($row_list=mysql_fetch_assoc($list)){
    ?>
    <option value="<?php echo $row_list['brand']; ?>" <?php if($row_list['brand']==$select){ echo "selected"; } ?>><?php echo $row_list['brand']; ?></option>
    <?php
    // End while loop.
    }
    ?>
    </select> 
    <input type="submit" name="Submit" value="Select" />
    </form>
    <br />
    <p>
    <?php
    if(isset($_POST['select']))
    {
      $brand=mysql_real_escape_string($_POST['select']);
      $result=mysql_query("SELECT supplier FROM who_supplies_us_what WHERE brand='".$brand."'");
      while ($row=mysql_fetch_assoc($result)) {
      echo '<strong>Supplier: </strong>'; echo $row['supplier']; echo '<br />';
    }
    }
    ?>
      Reply With Quote

  6. #6
    Super Moderator CloudedVision's Avatar
    Join Date
    Jan 2009
    Posts
    831
    Blog Entries
    4

    Default 5 May 2011 @ 19.23

    PHP Code:

    if(isset($_POST['select']))
    {
      
    $brand=mysql_real_escape_string($_POST['select']);
      
    $result=mysql_query("SELECT supplier FROM suppliers WHERE brand='".$brand."'";
     while( 
    $row=mysql_fetch_assoc($result)) {
      print 
    $row['supplier'];
      }

    Hope that helps.
      Reply With Quote

Similar Threads

  1. Drop Down Menu
    By silversurfer5150 in forum Javascript Libraries
    Replies: 1
    Last Post: 15 Aug 2010, @ 07.28
  2. displaying db records in groups?
    By ljackson in forum PHP, ASP & Java
    Replies: 8
    Last Post: 19 Jan 2010, @ 12.12
  3. Drop shadow container
    By trixiemay in forum HTML & CSS
    Replies: 2
    Last Post: 18 Jan 2010, @ 16.12
  4. populating a list box with data from an array?
    By ljackson in forum PHP, ASP & Java
    Replies: 12
    Last Post: 22 Dec 2009, @ 15.23
  5. Google tells users to drop IE6
    By WelshStew in forum Coffee House
    Replies: 5
    Last Post: 26 Jan 2009, @ 20.11

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •