I am wanting to create a drop down menu that displays one value in a database inside the form, but passes another value when the form is submitted.

For example I have a table named users with entries:

-----------------------------------
ID | UserName
-----------------------------------
1 BobG
-----------------------------------
2 JaneD

I want the drop down menu to list the names, but when JaneD is selected I want to pass the value "2" not "JaneD"

My current code is

// Connect to the database
    $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
 
  
  //Get data for UserName Dropdown
    $query="SELECT UserName FROM users";
$result1=mysqli_query($dbc, $query);

$options="";

while ($row=mysqli_fetch_array($result1)) {

    $UserName=$row["UserName"];
    $options.="<OPTION VALUE=\"$UserName\">".$UserName;
} 

<form enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <label for="UserName">Select User:</label><SELECT NAME="UserName" id="UserName">
		<OPTION VALUE=0 selected="selected">
		<?=$options?>
		</SELECT> </div>

<div class="field_submit"><input type="submit" value="submit" name="submit" /></div>
  </form>

Thanks for looking.

Recommended Answers

All 3 Replies

Could you not include a

$UserId = $row['id'];

then use that as the value?

$options.="<OPTION VALUE=\"$UserId\">".$UserName;

Yes that works fine ... that's what I was trying to do but did not fully understand where I was going wrong.

Yes that works fine ... that's what I was trying to do but did not fully understand where I was going wrong.

Glad i could help ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.