Results 1 to 7 of 7
  1. #1
    Join Date
    Apr 2005
    Posts
    46

    Unhappy Unanswered: Populating Form from mySQL DB

    I want to edit a DB that exists. I am importing the DB into a form. It is easy getting the txt into text fields, and I have been able to use a function to populate check-boxes and radio buttons, but i am now trying to choose the correct option from a drop down menu based on the data from the DB.

    Here is my function for check-boxes:

    function is_checked($string3, $string4)
    {
    if ($string3 == $string4)
    {
    return 'checked="checked"';
    } else
    {
    return;
    }
    }



    Here is how the function selects hte correct check-box:

    <input name="Name" type="radio" value="y" checked="checked" <?php print is_checked('y', $Name); ?>/>
    Yes <br />
    <input name="Name" type="radio" value="n" <?php print is_checked('n', $Name); ?>/>
    No




    Here is my function for drop-downs:
    function is_selected($string1, $string2)
    {
    if (tolower($string1) == tolower($string2))
    {
    return 'selected="selected"';
    } else
    {
    return;
    }
    }


    WHERE do I put the call to populate the below drop-down:

    <select name="Name2" id="Name2">
    <option value="A" >A</option>
    <option value="R">R</option>
    <option value="D">D</option>
    </select>

  2. #2
    Join Date
    Apr 2006
    Location
    Denver, Co. USA
    Posts
    242
    Here is a link to the proper syntax for the "option" "selected" form element - http://htmlcodetutorial.com/forms/_OPTION_SELECTED.html

  3. #3
    Join Date
    Apr 2005
    Posts
    46
    Thank you for the post, although the problem isn't with hte HTML syntax, its being able to have the selection be dynamically based on what is being pulled from the database.

    Meaning, if they have "A" stored in their database, then the drop-down should automatically select "A", and not "B", "C", or "D". If another row has "C" selected, then the drop-down should select "C". See what I am trying to do?

  4. #4
    Join Date
    Dec 2003
    Posts
    37
    Hi I'm in the middle of getting my own site live so excuse the crude on-commented code but it works and should be easy enough to follow.

    Code:
    <?
    $conn= mysql_connect ("localhost",YOUR USERNAME,"YOUR PASSWORD" )
    	or die ("Error connecting to database. Please try again or contact admin");
    	
    	$db =mysql_select_db ("YOUR DATABASE",$conn)
    	or die ("Error accessing database");
    
    $sql="SELECT * FROM YOUR TABLE"; 
    $result=mysql_query($sql,$conn) 
    or die("couldnt execute query");
    
    ?>
    
    
    <form action="resultlong.php">
    <method ="get">
            
    <?
    $options=""; 
    
    while ($row=mysql_fetch_array($result)) 
    { 
    
        $Mem_Id=$row["Mem_Id"]; 
        $Surname=$row["Surname"]; 
        $space=" ";
        $Forename=$row["Forename"];
        $options.="<OPTION VALUE=\"$Mem_Id\">". $Forename. $space . $Surname. "</option>";
    
    }
    
    ?>
    
    <SELECT NAME="Mem"> 
    <OPTION VALUE=0>Select
    
    <? echo $options ?> 
    </SELECT> 
    <?
            <?
    $conn= mysql_connect ("localhost",jd77_golf328,"golf1289a" )
    	or die ("Error connecting to database. Please try again or contact admin");
    	
    	$db =mysql_select_db ("jd77_golf",$conn)
    	or die ("Error accessing database");
    
    $sql="SELECT * FROM Member"; 
    $result=mysql_query($sql,$conn) 
    or die("couldnt execute query");
    
    ?>
    
    
    <form action="resultlong.php">
    <method ="get">
            
    <?
    $options=""; 
    
    while ($row=mysql_fetch_array($result)) 
    { 
    
        $Mem_Id=$row["Mem_Id"]; 
        $Surname=$row["Surname"]; 
        $space=" ";
        $Forename=$row["Forename"];
        $options.="<OPTION VALUE=\"$Mem_Id\">". $Forename. $space . $Surname. "</option>";
    
    }
    
    ?>
    
    <SELECT NAME="Mem"> 
    <OPTION VALUE=0>Select
    
    <? echo $options ?> 
    </SELECT> 
    <?
              $form.= "<input type=\"hidden\" name=\"Course\" value=\"$Course\">";
              $form.= "<input type=\"submit\" value=\"Post\">";
              $form.= "</form>";
              echo($form);
    
    ?>
    JD

  5. #5
    Join Date
    Apr 2005
    Posts
    46
    So, JD, this code will create a drop down with a predetermined set of values, and have the value from the database selected as the default?

  6. #6
    Join Date
    Apr 2006
    Location
    Denver, Co. USA
    Posts
    242
    His code forms a drop down using a list of values from a database.

    The following code forms a drop down using a predetermined set of values and selects a default value that could come from a database -
    PHP Code:
    <?php
    function is_selected($string1$string2) {
        if (
    strtolower($string1) == strtolower($string2)){
            return 
    "selected=\"selected\"";
        } else {
            return 
    "";
        }
    }

    $option_list = array("A""R""D"); // get or create the list of predetermined option values/names
    $selected_option "R"// get which option is selected (from database or wherever you like)
    ?>

    <form action="" method="POST">

    <select name="Name2" id="Name2">
    <?php
    foreach($option_list as $value){
        echo 
    "<option value=\"$value\" ".is_selected($value,$selected_option).">$value</option>";
    }
    ?>
    </select> 

    <input type="submit" name="submit" value="submit"/> 
    </form>
    Note - Unless you have your own tolower(...) function, the proper PHP function name is strtolower(...)

  7. #7
    Join Date
    Apr 2006
    Location
    Denver, Co. USA
    Posts
    242
    For your first function, the usage example you posted contains errors. If the $Name variable is "y" it will evaluate to -
    Code:
    <input name="Name" type="radio" value="y" checked="checked" checked="checked" />
    Yes <br />
    <input name="Name" type="radio" value="n" />
    No
    Which will probably work but contains redundant elements.

    If the $Name variable is "n" it will evaluate to -
    Code:
    <input name="Name" type="radio" value="y" checked="checked" />
    Yes <br />
    <input name="Name" type="radio" value="n" checked="checked" />
    No
    Which does not make sense.

Posting Permissions

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