SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    midnight coder
    Join Date
    Dec 2000
    Location
    The flat edge of the world
    Posts
    838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have a form, and have text field like:

    <input type="text" name="email" value="<?php echo $email ?>">

    So in that text box, the varible $email will show up, how do I do the same to drop down menus?

    I have a varible call $status, and the available options are New, Pending, and Old. Say the $status varible is New, how do I do it so when people goes to the form, New is seleted in the drop down menu? And the other way round so when people choose say Pending, the $status varible changes to Pending.

    My unsuccessful code is:

    <select name="status">
    <option value="<?php echo $status ?>">New</option>
    <option value="<?php echo $status ?>">Pending</option>
    </select>

  2. #2
    AdSpeed.com Son Nguyen's Avatar
    Join Date
    Aug 2000
    Location
    Silicon Valley
    Posts
    2,241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could just simply assign the value or if you want to make some default choice base on $status, use matching (compare and write the right <option> tag)
    Maybe I don't quite understand what you're trying to do.
    - Son Nguyen
    AdSpeed.com - Ad Serving and Ad Management Made Easy

  3. #3
    Non-Member
    Join Date
    Apr 2000
    Location
    Waco, Texas.
    Posts
    188
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <select name="status">
    <?
    $status = "Old";
    $statusarray = array("New","Pending","Old");
    foreach($statusarray as $value)
    {
      echo "<option value=\"$value\"";
      if($value == $status)
      {
           echo "selected";
      }
      echo ">$value</option>";
    }
    ?>
    </select>
    I am sure there is a more efficient way, but that works fine.


    <Edited by robp on 12-09-2000 at 10:00 PM>

  4. #4
    AdSpeed.com Son Nguyen's Avatar
    Join Date
    Aug 2000
    Location
    Silicon Valley
    Posts
    2,241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Exactly the same thing I did couple times before!
    Couldn't delete it, so keep it there!

    [edit] oh man! he just edited the post and I posted for that reply. So now I couldn't delete this post!! haha, this is funny when the forums are so active!
    - Son Nguyen
    AdSpeed.com - Ad Serving and Ad Management Made Easy

  5. #5
    SitePoint Addict
    Join Date
    Dec 2000
    Location
    BOSTON MA
    Posts
    335
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i don't know if your using a DB or not, but i had a similar challenge 2 days ago. i was writing an "edit personal info" type script for mysql that a user would access by logging in. this script would then pull the values from the DB and insert them into the form. this is what my brain came up with after going on very little sleep for 2 days. i hope it works for you.

    :::::::::::::::::::::::::::::::::::
    <html>
    <head>
    <title>YOUR TITLE HERE</title>
    </head>
    <body>


    <?php

    $DBhost = "PUT YOUR SERVER NAME HERE. USUALLY LOCALHOST";
    $DBuser = "YOUR DB USERNAME";
    $DBpass = "YOUR DB PASSWORD";
    $DBName = "YOUR DB NAME";
    $table = "YOUR DB TABLE NAME";
    $db = mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable to connect to database");


    @mysql_select_db("$DBName",$db) or die("Unable to select database $DBName");

    $sqlquery = "SELECT * FROM $table";

    $result = mysql_query($sqlquery);
    $myrow = mysql_fetch_array($result);




    $choice = $myrow["status"];


    if($choice == "New") {
    $status_select_new = "selected";
    $status_select_old = "";
    $status_select_pending = "";

    } elseif ($choice == "Old") {

    $status_select_new = "";
    $status_select_old = "selected";
    $status_select_pending = "";

    } elseif ($choice == "Pending") {

    $status_select_new = "";
    $status_select_old = "";
    $status_select_pending = "selected";}


    ?>


    <FORM METHOD=POST ACTION="SCRIPTNAME">




    <select name="status">

    <option value="New" <?php echo $status ?>">New</option>
    <option value="Old" <?php echo $status ?>">Old</option>
    <option value="Pending" <?php echo $status ?>">Pending</option>

    </select>




    </form>

    </body>
    </html>

    :::::::::::::::::::::::::::::::::::::::

    :: chris
    <Edited by atomicmunky on 12-09-2000 at 10:16 PM>
    . . . chris

  6. #6
    midnight coder
    Join Date
    Dec 2000
    Location
    The flat edge of the world
    Posts
    838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks robp, your script worked fine, just had to put a space before selected in the echo "selected";

    Didn't really understand the function at first, but found a tutorial at DevShed which explaned it pretty well.

    It's reasonably efficent, since you only have to enter the list items once.


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
  •