SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Nov 2007
    Location
    Murrells Inlet, SC
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Selecting Initial Value When Auto-Populating List Box

    I am a beginner to PHP and have what is probably a simple bug in my code.

    I have created a form list box that auto populates from a MySQL Database. That part works great, however I would like to choose one of the values in the database be initially selected.

    In this example, I have created a database table called states with two fields, states (the state name) and selected. The selected field is blank for every row expect one. For that one row, I added the value "selected" to the field.

    Here is my code:

    <?php
    include 'config.php';
    include 'opendb.php';
    $queryState='SELECT states, selected FROM states';
    $resultState = mysql_query ($queryState);
    echo "<select name=State>";
    while($st=mysql_fetch_array($resultState))
    echo "<option value='$st[states]' $st[selected]>$st[states]</option>";
    }
    echo "</select>";
    include 'closedb.php';
    ?>

    Here is the bit (in asterisks) giving me trouble...
    <option value='$st[states]' ****$st[selected]****>

    Everything works great except that one piece. I would have guessed that including $st[selected] (which is blank in all but one case) would have worked just like the following HTML, but none of the options are preselected.
    <select name="State">
    <option value="Georgia">Georgia</option>
    <option value="Florida" selected></option>
    </select>

    Thanks in advance for all your help!

  2. #2
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    include 'config.php';
    include 
    'opendb.php';
    $queryState='SELECT states, selected FROM states';
    $resultState mysql_query ($queryState);
    $selected_state $_GET['selected'];
    echo 
    "<select name=State>";
    while(
    $st mysql_fetch_array($resultState)){
        
    $sele = ($selected_state == $st['states']) ? ' selected' '';
        echo 
    '<option value="' $st['states'] . '"' sele '>' $st['states'] . '</option>';
    }
    echo 
    "</select>";
    include 
    'closedb.php'
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  3. #3
    SitePoint Member
    Join Date
    Nov 2007
    Location
    Murrells Inlet, SC
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the help! I was looking at your code and I realized what I did wrong. All I had to do was change this line...

    echo "<option value='$st[states]' $st[selected]>$st[states]</option>";

    To read...

    echo "<option value=\"$st[states]\" $st[selected]>$st[states]</option>";

    And it worked fine. Apparently I needed double quotes around the value attribute, single wouldn't work.

    Now I have one more problem. Again, I am new to this so I am probably overlooking the obvious.

    I have created this form which calls another php file to validate the contents. If errors are found, it will return the user to the original form and pass the form variable back so they don't have to reenter the information.

    In the case of text types, this process is super easy. I simply did the following...

    <input type="text" name="City" size="30"
    value="<?php echo stripslashes($_POST[City]); ?>">

    However, like the state field discussed earlier, some of my form fields are pull down list boxes. How can I duplicate the above effect with a list box? If the user is sent back to the form because of an error of some sort, I do not want them to have to reselect the data from the pull down list.

    Thank you again for all your help!

  4. #4
    SitePoint Member
    Join Date
    Nov 2007
    Location
    Murrells Inlet, SC
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by gordong1968 View Post
    Now I have one more problem. Again, I am new to this so I am probably overlooking the obvious.

    I have created this form which calls another php file to validate the contents. If errors are found, it will return the user to the original form and pass the form variable back so they don't have to reenter the information.

    In the case of text types, this process is super easy. I simply did the following...

    <input type="text" name="City" size="30"
    value="<?php echo stripslashes($_POST[City]); ?>">

    However, like the state field discussed earlier, some of my form fields are pull down list boxes. How can I duplicate the above effect with a list box? If the user is sent back to the form because of an error of some sort, I do not want them to have to reselect the data from the pull down list.
    Turns out it was really easy to do this. I added the following code...

    if(trim($_POST[State])!='') {
    echo "<option value=\"$_POST[State]\" selected>$_POST[State]</option>";
    }

    The second selected option will override the first, so if the pull down list has already been populated when the form is recalled they will not have to reselect.

    The complete code (for any newbies like me that may be interested) is...

    <?php
    include 'config.php';
    include 'opendb.php';
    $queryState='SELECT states, selected FROM states';
    $resultState = mysql_query ($queryState);

    echo "<select name=State>";

    while($st=mysql_fetch_array($resultState)){
    echo "<option value=\"$st[states]\" $st[selected]>$st[states]</option>";
    }

    if(trim($_POST[State])!='') {
    echo "<option value=\"$_POST[State]\" selected>$_POST[State]</option>";
    }

    echo "</select>";

    include 'closedb.php';
    ?>

  5. #5
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't think that your code is perfect one that is suitable for recommending to others. I think this will add two options for one selected one. Please review through out the code below and see the differences. Hope you will find and understand..
    PHP Code:
    include 'config.php';
    include 
    'opendb.php';
    $queryState='SELECT states, selected FROM states';
    $resultState mysql_query ($queryState);
    $selected_state $_POST['State'];
    echo 
    '<select name="State">';
    while(
    $st mysql_fetch_array($resultState)){
        
    $sele = ($selected_state == $st['states']) ? ' selected' '';
        echo 
    '<option value="' $st['states'] . '"' sele '>' $st[states] . '</option>' "\n";
    }
    echo 
    "</select>";
    include 
    'closedb.php'
    Try to see string quoting (double and single quotes) too.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5


Bookmarks

Posting Permissions

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