Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    New Coder
    Join Date
    Oct 2005
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Displaying data from a database in a drop-down menu

    Howdy folks
    Using PHP and a database i want to be able to fill in a form.

    The database contains the fields:
    ID, Attend, Location, Fromto, Leadership

    I also have a page with 4 form fields. I would like to be able to have a drop-down box on the page that fills in the form automatticaly depding on the chose you chose from the drop-down. I would like the drop-down to display the data in the Attend field of the database... Any help would be appreciated!

    Fet
    Edit/Delete Message

  • #2
    Senior Coder
    Join Date
    Nov 2002
    Location
    North-East, UK
    Posts
    1,265
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Here is an example

    PHP Code:
    <?php require_once('conn.php'); ?>
    <?php
    mysql_select_db
    ($database_PHP$PHP);
    $query_seminars "SELECT id, attend FROM seminar";
    $seminars mysql_query($query_seminars$PHP) or die(mysql_error());
    $row_seminars mysql_fetch_assoc($seminars);
    $totalRows_seminars mysql_num_rows($seminars);

    $colname_chosen_seminar "-1";
    if (isset(
    $_POST['attend'])) {
      
    $colname_chosen_seminar = (get_magic_quotes_gpc()) ? $_POST['attend'] : addslashes($_POST['attend']);
    }
    mysql_select_db($database_CHF_PHP$CHF_PHP);
    $query_chosen_seminar sprintf("SELECT * FROM seminar WHERE id = %s"$colname_chosen_seminar);
    $chosen_seminar mysql_query($query_chosen_seminar$CHF_PHP) or die(mysql_error());
    $row_chosen_seminar mysql_fetch_assoc($chosen_seminar);
    $totalRows_chosen_seminar mysql_num_rows($chosen_seminar);
    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Untitled Document</title>
    <script type="text/javascript">
    function chkFrm(el){
        if(el.selectedIndex == 0){
            alert("Please choose an option");
            return false
        }
        else{
        el.form.submit();
        }
    }
    </script>
    </head>

    <body>
    <form name="form1" method="post" action="">
      <p>
        <select name="attend" onchange="chkFrm(this)">
          <option value="Choose" <?php if (!(strcmp("Choose"$_POST['attend']))) {echo "SELECTED";} ?>>Choose Seminar</option>
          <?php
    do {  
    ?>
          <option value="<?php echo $row_seminars['id']?>"<?php if (!(strcmp($row_seminars['id'], $_POST['attend']))) {echo "SELECTED";} ?>><?php echo $row_seminars['attend']?></option>
          <?php
    } while ($row_seminars mysql_fetch_assoc($seminars));
      
    $rows mysql_num_rows($seminars);
      if(
    $rows 0) {
          
    mysql_data_seek($seminars0);
          
    $row_seminars mysql_fetch_assoc($seminars);
      }
    ?>
        </select>
    </p>
      <p>Location 
        <input name="textfield" type="text" value="<?php echo $row_chosen_seminar['location']; ?>">
        <br>
        From 
        <input name="textfield" type="text" value="<?php echo $row_chosen_seminar['from']; ?>">
        <br>
        Leader
        <input name="textfield" type="text" value="<?php echo $row_chosen_seminar['leadership']; ?>">
      </p>
    </form>
    </body>
    </html>
    <?php
    mysql_free_result
    ($seminars);

    mysql_free_result($chosen_seminar);
    ?>
    http://www.computer-helpforum.com/ph...ulate_form.php
    Last edited by degsy; 02-13-2006 at 03:16 PM.

  • #3
    New Coder
    Join Date
    Jan 2006
    Posts
    53
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Edit: Never mind, degsy's solved it I think

  • #4
    New Coder
    Join Date
    Oct 2005
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
    <?php 
    $db 
    mysql_pconnect('xx','xx','xx') or die ('I cannot connect to the database because: ' mysql_error());
    mysql_select_db ("xx"$db);
    $query_seminars "SELECT ID, Attend FROM Permission_To_Camp"
    $seminars mysql_query($query_seminars$PHP) or die(mysql_error()); 
    $row_seminars mysql_fetch_assoc($seminars); 
    $totalRows_seminars mysql_num_rows($seminars); 

    $colname_chosen_seminar "-1"
    if (isset(
    $_POST['Attend'])) { 
      
    $colname_chosen_seminar = (get_magic_quotes_gpc()) ? $_POST['Attend'] : addslashes($_POST['Attend']); 

    mysql_select_db($database_CHF_PHP$CHF_PHP); 
    $query_chosen_seminar sprintf("SELECT * FROM Permission_To_Camp WHERE id = %s"$colname_chosen_seminar); 
    $chosen_seminar mysql_query($query_chosen_seminar$CHF_PHP) or die(mysql_error()); 
    $row_chosen_seminar mysql_fetch_assoc($chosen_seminar); 
    $totalRows_chosen_seminar mysql_num_rows($chosen_seminar); 
    ?> 
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
    <html> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 
    <title>Untitled Document</title> 
    <script type="text/javascript"> 
    function chkFrm(el){ 
        if(el.selectedIndex == 0){ 
            alert("Please choose an option"); 
            return false 
        } 
        else{ 
        el.form.submit(); 
        } 

    </script> 
    </head> 

    <body> 
    <form name="form1" method="post" action=""> 
      <p> 
        <select name="Attend" onchange="chkFrm(this)"> 
          <option value="Choose" <?php if (!(strcmp("Choose"$_POST['Attend']))) {echo "SELECTED";} ?>>Choose Seminar</option> 
          <?php 
    do {   
    ?> 
          <option value="<?php echo $row_seminars['ID']?>"<?php if (!(strcmp($row_seminars['ID'], $_POST['Attend']))) {echo "SELECTED";} ?>><?php echo $row_seminars['Attend']?></option> 
          <?php 
    } while ($row_seminars mysql_fetch_assoc($seminars)); 
      
    $rows mysql_num_rows($seminars); 
      if(
    $rows 0) { 
          
    mysql_data_seek($seminars0); 
          
    $row_seminars mysql_fetch_assoc($seminars); 
      } 
    ?> 
        </select> 
    </p> 
      <p>Location  
        <input name="textfield" type="text" value="<?php echo $row_chosen_seminar['Location']; ?>"> 
        <br> 
        From  
        <input name="textfield" type="text" value="<?php echo $row_chosen_seminar['Fromto']; ?>"> 
        <br> 
        Leader 
        <input name="textfield" type="text" value="<?php echo $row_chosen_seminar['Leadership']; ?>"> 
      </p> 
    </form> 
    </body> 
    </html>
    Get an error on line 4... Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/scott/public_html/test.php on line 4

  • #5
    New Coder
    Join Date
    Jan 2006
    Posts
    53
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Try removing the $PHP from line four, so it would be:

    PHP Code:
    $seminars mysql_query($query_seminars) or die(mysql_error()); 

  • #6
    New Coder
    Join Date
    Oct 2005
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hum..

    $query_seminars = "SELECT ID, Attend FROM 'Permission_To_Camp' ";

    gets

    You have an error in your SQL syntax near ''Permission_To_Camp' ' at line 1

    Previous error fixed though!

  • #7
    Senior Coder
    Join Date
    Nov 2002
    Location
    North-East, UK
    Posts
    1,265
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It should be fine. You may have an error before that in your connection code.

    Post the code you are using now.

  • #8
    New to the CF scene
    Join Date
    Apr 2006
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    conn.php

    I continue to get errors in syntax on line 1. I can not seem to get the conn.php file correct.

    Could you post a sample?

  • #9
    Senior Coder
    Join Date
    Nov 2002
    Location
    North-East, UK
    Posts
    1,265
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
    <?php
    $hostname_CHF_PHP 
    "host";
    $database_CHF_PHP "dn_name";
    $username_CHF_PHP "user_name";
    $password_CHF_PHP "pass";
    $CHF_PHP mysql_pconnect($hostname_CHF_PHP$username_CHF_PHP$password_CHF_PHP) or trigger_error(mysql_error(),E_USER_ERROR); 
    ?>

  • #10
    New to the CF scene
    Join Date
    Apr 2006
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Two questions that are proabably related.

    If I add any other fields to this form, they are zeroed out when I use this

    also

    I need to have several groups of these text boxes on the form. How can I dissassociate them so that when I change one of the drop down boxes, it only updates the corresponding text and not every group

    I posted my code below
    Last edited by rhindo; 04-19-2006 at 12:48 AM.

  • #11
    New to the CF scene
    Join Date
    Apr 2006
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
    <?php require_once('conn.php'); ?> 

    <?php
    mysql_select_db
    ($database_PHP$PHP);
    $query_labors "SELECT id, code FROM labor";
    $labors mysql_query($query_labors$PHP) or die(mysql_error());
    $row_labors mysql_fetch_assoc($labors);
    $totalRows_labors mysql_num_rows($labors);

    $colname_chosen_labor "-1";
    if (isset(
    $_POST['code'])) {
    $colname_chosen_labor = (get_magic_quotes_gpc()) ? $_POST['code'] : addslashes($_POST['code']);
    }
    mysql_select_db($database_CHF_PHP$CHF_PHP);
    $query_chosen_labor sprintf("SELECT * FROM labor WHERE id = %s"$colname_chosen_labor);
    $chosen_labor mysql_query($query_chosen_labor$PHP) or die(mysql_error());
    $row_chosen_labor mysql_fetch_assoc($chosen_labor);
    $totalRows_chosen_labor mysql_num_rows($chosen_labor);
    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Untitled Document</title>
    <script type="text/javascript">
    function chkFrm(el){
    if(el.selectedIndex == 0){
    alert("Please choose an option");
    return false
    }
    else{
    el.form.submit();
    }
    }
    </script>
    </head>

    <body>
    <form name="form1" method="post" action="">
    <p>
    <select name="code" onchange="chkFrm(this)">
    <option value="Choose" <?php if (!(strcmp("Choose"$_POST['code']))) {echo "SELECTED";} ?>>Choose labor</option>
    <?php
    do { 
    ?>
    <option value="<?php echo $row_labors['id']?>"<?php if (!(strcmp($row_labors['id'], $_POST['code']))) {echo "SELECTED";} ?>><?php echo $row_labors['code']?></option>
    <?php
    } while ($row_labors mysql_fetch_assoc($labors));
    $rows mysql_num_rows($labors);
    if(
    $rows 0) {
    mysql_data_seek($labors0);
    $row_labors mysql_fetch_assoc($labors);
    }
    ?>
    </select>

    Description
    <input name="text1" type="text" value="<?php echo $row_chosen_labor['desc']; ?>">
    Amount
    <input name="text2" type="text" value="<?php echo $row_chosen_labor['amount']; ?>">
    Quantity
    <input name="txtQty" type="text" value="">
    </p>

    <p>
    <select name="code" onchange="chkFrm(this)">
    <option value="Choose" <?php if (!(strcmp("Choose"$_POST['code']))) {echo "SELECTED";} ?>>Choose labor</option>
    <?php
    do { 
    ?>
    <option value="<?php echo $row_labors['id']?>"<?php if (!(strcmp($row_labors['id'], $_POST['code']))) {echo "SELECTED";} ?>><?php echo $row_labors['code']?></option>
    <?php
    } while ($row_labors mysql_fetch_assoc($labors));
    $rows mysql_num_rows($labors);
    if(
    $rows 0) {
    mysql_data_seek($labors0);
    $row_labors mysql_fetch_assoc($labors);
    }
    ?>
    </select>

    Description
    <input name="text1" type="text" value="<?php echo $row_chosen_labor['desc']; ?>">
    Amount
    <input name="text2" type="text" value="<?php echo $row_chosen_labor['amount']; ?>">
    Quantity
    <input name="txtQty" type="text" value="">
    </p>
    </form>
    </body>
    </html>
    <?php
    mysql_free_result
    ($labors);

    mysql_free_result($chosen_labor);
    ?>
    here is my code if that helps...

  • #12
    Senior Coder
    Join Date
    Nov 2002
    Location
    North-East, UK
    Posts
    1,265
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The script is not setup to do that. For that you would need a seperate script for each box.


  •  

    Posting Permissions

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