#1
  1. Wiking
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Sep 2000
    Location
    Sweden
    Posts
    3,608
    Rep Power
    26
    I've picked up some scripts from this forum regarding drop down lists (thanx!), and now I'm wondering if anyone can explain to me how to do the following:

    Table:
    +---+-----+
    |ID |NAME |
    +---+-----+
    |1 |name1|
    |2 |name2|
    |3 |name3|
    +---+-----+

    I fetch values from NAME to a drop down list without problems. Then I want to select the corresponding ID (depending on which NAME-value I've selected) into a hidden(?) field to use later on in a submit.php page which is sent to another table in the db.

    Can someone explain to me how to do this?

    Thanx
  2. #2
  3. .Net Developer
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2000
    Location
    London
    Posts
    987
    Rep Power
    15

    here a sample script for you .modify this and use as per your requirment..


    <?
    $con=mysql_connect('localhost','username','pwd');
    //database connectivity

    mysql_select_db('databasename',$con);
    //select your databae..

    $result=mysql_query("select * from tablename",$con);
    //issue the query..

    if(mysql_num_rows($result)>0){
    echo "<form method="post" action="submit.php3">n";

    echo "<select name="somename">";
    while($row=mysql_fetch_array($result)){
    //loop through and the print your records to drop down menu....

    echo "<option value="row[id]">row[name]</option>n";
    //print name in the drop down and id as value
    }
    echo "</select>";
    echo "</form>";
    }
    ?>

    Good luck!!

    ------------------
    SR -
    webshiju.com
    www.jobxyz.com-IT Career Portal
    ezipindia.com--WebStudio


    "The fear of the LORD is the beginning of knowledge..."
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2000
    Location
    Pune, Maharashtra, India
    Posts
    86
    Rep Power
    14
    Try something like this:

    <?
    $db=mysql_connect($host,$username) or Die("Unable to connect to the database, Please try later");
    mysql_select_db($database,$db) or die("Unable to Select Database, Please try later");

    ##Selecting from the table
    $ssl="Select * from $table";
    $result=mysql_query($ssl);
    echo("<SELECT NAME="name">");
    while($row=mysql_fetch_array($result))
    {
    echo ("<option value="$row['id']">$row['name']>");
    }
    echo ("</SELECT>");
    ?>


    ------------------
    Nikunj
    MYSQL/PHP/XML
    ** Expertise comes with experiece ** Nikunj
  6. #4
  7. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2000
    Location
    Colchester, England
    Posts
    131
    Rep Power
    14
    An example that calls a function to populate your dropdown:

    <?php
    include("functions.php");
    OpenDb("localhost","user","password","databasename") or die("Failed Opening Database");
    ?>
    <form method="post" action="<?php echo nameinsert ?>">
    <?php
    CreateDropDown("cboYourName","ID, name ", "tblname");
    ?>
    <input type="Submit" name="submit" value="Continue">
    </form>

    The second picks up the variable passed by the submit from the previous script. Save as nameinsert.php:

    <?php
    include("functions.php");
    OpenDb("localhost","user","","databaseName") or die("Failed Opening Database");

    $sql = "INSERT INTO tblTable (NameFieldName) VALUES ('$cboName')";

    $result = mysql_query($sql);

    ?>

    And the last are the functions.php:

    <?php
    /* You can put this in other file and just include it */
    function OpenDb($hostname,$uid,$pwd,$dbname)
    {
    $link = @mysql_pconnect($hostname,$uid,$pwd);
    if($link && mysql_select_db($dbname)){

    return($link);
    }
    else{
    return(FALSE);
    }
    }//end function



    ?>

    <?php
    function CreateDropDown($strLabel,$strFields, $strTable){
    settype($retval,"array");
    $strSql = "SELECT $strFields FROM $strTable";

    $result= mysql_query($strSql);

    if(!$result){
    print "Query Failed";
    }
    echo "<select name=$strLabel>";

    for($intRecordCount=0;$intRecordCount<mysql_numrows($result);$intRecordCount++)
    {

    $myrow = mysql_fetch_row($result);
    printf("<option value='$myrow[0]'>$myrow[1]");

    }

    echo "</select>";

    return $retval;
    }//end function

    ?>

    Andy J.


    [This message has been edited by eaamj01 (edited September 08, 2000).]

    [This message has been edited by eaamj01 (edited September 08, 2000).]
  8. #5
  9. Wiking
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Sep 2000
    Location
    Sweden
    Posts
    3,608
    Rep Power
    26
    Thanx guys!

    I'm just another newbie but with this help from you and some thinking of my own, I managed to get this working.

    / NoXcuz
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2000
    Posts
    669
    Rep Power
    15
    I am assuming that none of the above suggestion was what you were after since you stated that you wanted the ids in hidden fiedls not as the values of the drop down list.
    Your while statement should probably have read
    while ($row = mysql_fetch_array($result)) {
    $id = $row["id"];
    $name = $row["name"];
    //Put all records into an array with the id as a key and the name as the value
    $names[$id] = $name;
    }
    print '<select name="name">';
    //Loop through array and create drop down
    while (list($key, $val) = each ($names)) {
    printf('<option value="%s">%s', $val, $val);
    }
    print '</select';

    //Reset the array to the start
    reset($names);
    //Further down your page somwhere
    //Loop through array and create hidden inputs
    while (list($key, $val) = each ($names)) {
    printf('<input type="hidden" name="%s" value="%s">%s', $val, $key);
    }

Similar Threads

  1. dynamic drop down list using jsp in javascript
    By quilla_b in forum JavaScript Development
    Replies: 2
    Last Post: May 27th, 2004, 11:35 AM
  2. MySQL output to table using phpBB
    By CCCMTech in forum PHP Development
    Replies: 0
    Last Post: February 2nd, 2004, 02:43 PM
  3. refresh drop down
    By yashi in forum HTML Programming
    Replies: 1
    Last Post: September 11th, 2003, 11:12 AM
  4. How do I Generate Access 200 Db from script?
    By johnnyr77 in forum Database Management
    Replies: 0
    Last Post: September 1st, 2003, 05:45 PM
  5. Replies: 4
    Last Post: February 18th, 2002, 06:08 AM

IMN logo majestic logo threadwatch logo seochat tools logo