How to select item in dropdown after posting a form?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bdbeames
    New Member
    • Jun 2007
    • 27

    How to select item in dropdown after posting a form?

    Ok, It has been one of those days. Here is my bump in the road.
    I have an add user form for the administrator. The administrator enters name, password and then selects access level from a dropdown. When the data is posted I take the administrator to a page where all users can be viewed. I then allow the administrator to select a user in the list that they would like to edit. The administrator is taken back to the add user form, but the form is filled in with the data of the user they wish to edit.

    Problem..
    My access level selection in a dropdown list. When the administrator wants to edit a user and the data is filled in the form, I can not figure out how to select the correct option from the dropdown. Any help will be nice.

    [php]
    function useradd() {
    if ($_GET[option] == "edit") {
    $nuser = getUser($_SESSI ON[nuser]);
    }
    print <<<PAGE
    <div>
    <form name="userForm" action="users.p hp" method="post">
    <fieldset>
    <legend>Add User</legend>
    <p>
    <label for="name">*Use r Name</label><input type="text" id="name" name="name" value="$nuser[name]" />
    </p>
    <p>
    <label for="password"> *Password</label><input type="password" id="password" name="password" value="" />
    </p>
    <p>
    <label for="access">Ac cess Level</label>
    //This is where I need help selecting read only or administrator
    <select id="access" name="access">
    <option value="2">Read Only Access</option>
    <option value="1">Admin istrator</option>
    </select>
    </p>
    <p>
    <input type="submit" name="aSubmit" value="Submit" onclick="if (userCheck() == false) return false;"/>
    <input type="reset" />
    <input type="button" onclick="locati on.href='users. php'" value="Back" />
    </p>
    </fieldset>
    </form>
    </div>
    PAGE;
    }
    [/php]

    The $nuser array will contain name, password, and accesslevel.

    Thanks
  • jodan
    New Member
    • Nov 2007
    • 1

    #2
    Originally posted by bdbeames
    //This is where I need help selecting read only or administrator
    <select id="access" name="access">
    <option value="2">Read Only Access</option>
    <option value="1">Admin istrator</option>
    </select>
    Two options:

    1.
    Code:
    //change your select to this:
    
                   <select id="access" name="access">
                         <option value="2"<?php if($nuser['accesslevel']=="2") echo " selected";?>>Read Only Access</option>
                         <option value="1"<?php if($nuser['accesslevel']=="1") echo " selected";?>>Administrator</option>
                   </select>
    2.
    [PHP]<?php

    $options_array = array('1'=>'Adm inistrator', '2'=>'Read Only Access');

    echo '<select id="access" name="access">' ;
    foreach($option s_array as $value=>$option ){
    echo '<option value="'.$value .'"';
    if($nuser['accesslevel'] == $value){
    echo " selected";
    }
    echo '>'.$option."</option>\n";
    }
    echo '</select>';

    ?>[/PHP]

    Hope this helps. Cheers.

    Comment

    • bdbeames
      New Member
      • Jun 2007
      • 27

      #3
      Thanks for the help.

      I like you second option.

      Comment

      Working...