0

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.

2
Contributors
3
Replies
5
Views
7 Years
Discussion Span
Last Post by emhmk1
0

Could you not include a

$UserId = $row['id'];

then use that as the value?

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

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

0

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 ;)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.