Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a drop down list that gets its values from SQL. When a user selects a value I want to store it in a variable. Currently I when I select a value from my drop down list, it doesn't display that value.

It seems that it pr-selects a value and I don't know why?

Here is my code:

XML
<html>
<HEAD>
<link rel="stylesheet" type="text/css" href="mytech.css">
</HEAD>
<body>

<?php
session_start();
if(isset($_SESSION['DeskFusername']))
{
$config = parse_ini_file("noc.ini", true);
  $serverName = $config['server']['SQLserver'];
  $connectionInfo = $config['connectionInfo'];
  $connectionInfo["LoginTimeout"] = 5;
  $conn = sqlsrv_connect($serverName, $connectionInfo);

$tsql = "SELECT userId, EmailAdd
         FROM [Dashboard].[dbo].[EA_t_Master_Users]";




?>
<table id="UserRoles" width="500" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="UserRoles" method="post" action="1.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3"><strong>Create User Login</strong></td>
</tr>

<td>User Name</td>
<td>:</td>
<td>
<select name="UserName" id="UserName">
<?PHP
echo "<option value=\"\" disabled=\"disabled\" selected=\"selected\">Please select a Customer</option>";
$stmt = sqlsrv_query( $conn, $tsql);
if( $stmt === false )
{
     echo "Error in executing query.<br/>";
     die( print_r( sqlsrv_errors(), true));
}


while ($row=sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {

    if($row['EmailAdd'] == $_POST['UserName'])
    {
        $isSel = " selected";
    }
    else
    {
        $isSel = "";
    }

    echo "<option value=\"".$row['EmailAdd']."\">".$row['EmailAdd']."</option>";

                $UserName = $row['EmailAdd'];

}

//
?>
</select>
</td>
</tr>

<tr>

<td>
<?php

    echo $UserName;

?>
</td>
</tr>


</table>
</td>
</form>
</tr>
</table>
<?PHP
}
else
{
header("location:loginTry1.php");
}
?>

</body>
</html>
Posted

1 solution

You have created a variable called
$isSel

and assign it the value of "selected" if the post value matches the "emailAdd" from the rowset, but it is NOT used anywhere after its creation and assignment.
It should be:
PHP
if($row['EmailAdd'] == $_POST['UserName'])
{
    $isSel = " selected";
}
else
{
    $isSel = "";
}
// include the $isSel here
echo "<option value='".$row['EmailAdd']."'".$isSel.">".$row['EmailAdd']."</option>";
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900