I have the code shown below that nicely fills a Combobox but I have no idea how to have it on a form so that the user can select an item and the resulting $_POST would be availabe to another page the same way it would be if the Combobox had been filled at programming time. Can anyone help?
If it can be done another way, making $_SESSION = the "selected username" for instance, would be acceptable.

connect2DB();
$result = mysql_query("SELECT * FROM Members ORDER BY name ") or die("Unable to query ");
mysql_close();
echo "<select name=username value=''>username Name</option>";
while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
echo "<option value=$nt[id]>$nt[name]</option>";
}
echo "</select>";
<div id="wb_Form2" style="position:absolute;width:329px;height:75px;">
<form name="LoginForm" method="post" action="verify.php" id="Form2">
<select name="username" size="1" id="Combobox1" style="position:absolute;left:96px;top:9px;width:163px;height:22px;z-index:1;">
<option>Select your name</option>
<option value="John Doe">John Doe</option>
But about 12 names loaded from Mysql database instead.
</select>
<input type="text" id="Editbox1" style="position:absolute;left:107px;top:44px;width:148px;height:18px;line-height:18px;z-index:2;" name="password" value="">
<input type="submit" id="Button2" name="" value="Login" style="position:absolute;left:267px;top:10px;width:51px;height:54px;z-index:3;">
<div id="wb_Text1" style="position:absolute;left:3px;top:4px;width:92px;height:32px;z-index:4;">
<span style="color:#000000;font-family:Arial;font-size:27px;"><strong>Login:</strong></span></div>
<input type="button" id="Button1" onclick="window.location.href='http://www.dubelou.com/ccmen/Forum.php'; ;return false;" name="" value="Forum" style="position:absolute;left:8px;top:42px;width:71px;height:25px;z-index:5;">
</form>
</div>

Recommended Answers

All 6 Replies

just put that php code in your form, it should work fine unless and until there is any db or syntax error

Member Avatar for diafol

You want the selection to show in the form after form submit?

connect2DB();
$output = "";
$result = mysql_query("SELECT * FROM Members ORDER BY name ") or die("Unable to query ");
while($nt=mysql_fetch_array($result)){
   $sel = (if(isset($_POST['username']) && $_POST['username'] == $nt['id']) ? ' selected="selected"' : '';
   $output .= "\n\t<option value=\"{$nt['id']}\"$sel>{$nt['name']}</option>";
}
mysql_close();

THEN

<div id="wb_Form2" style="position:absolute;width:329px;height:75px;">
<form name="LoginForm" method="post" action="verify.php" id="Form2">
<select name="username" size="1" id="Combobox1" style="position:absolute;left:96px;top:9px;width:163px;height:22px;z-index:1;">
<option disabled="disabled">Select your name</option>
<?php echo $output;?>
</select>
<input type="text" id="Editbox1" style="position:absolute;left:107px;top:44px;width:148px;height:18px;line-height:18px;z-index:2;" name="password" value="">
<input type="submit" id="Button2" name="" value="Login" style="position:absolute;left:267px;top:10px;width:51px;height:54px;z-index:3;">
<div id="wb_Text1" style="position:absolute;left:3px;top:4px;width:92px;height:32px;z-index:4;">
<span style="color:#000000;font-family:Arial;font-size:27px;"><strong>Login:</strong></span></div>
<input type="button" id="Button1" onclick="window.location.href='http://www.dubelou.com/ccmen/Forum.php'; ;return false;" name="" value="Forum" style="position:absolute;left:8px;top:42px;width:71px;height:25px;z-index:5;">
</form>
</div>

However, I would advise you to use a css file for the inline styling and event handlers for the inline js.

Thank you for the suggestion it looks promising butI can't get pass that

Parse error: syntax error, unexpected T_IF in /home/content/68/7140768/html/wrk/index.php on line 219

with the following code:

<?php>
connect2DB();
$output = "";
$result = mysql_query("SELECT * FROM Members ORDER BY name DESC") or die("Unable to query ");
while($nt=mysql_fetch_array($result))
{
   $sel = (if(isset($_POST['username']) && $_POST['username'] == $nt['id']) ? ' selected="selected"' : '');
   $output .= "\n\t<option value=\"{$nt['id']}\"$sel>{$nt['name']}</option>";
}
mysql_close();
?>

<div id="wb_Form2" style="position:absolute;width:329px;height:75px;">
<form name="LoginForm" method="post" action="verify.php" id="Form2">
<select name="username" size="1" id="Combobox1" style="position:absolute;left:96px;top:9px;width:163px;height:22px;z-index:1;">
<option disabled="disabled">Select your name</option>
<?php echo $output;?>
</select>
<input type="text" id="Editbox1" style="position:absolute;left:107px;top:44px;width:148px;height:18px;line-height:18px;z-index:2;" name="password" value="">
<input type="submit" id="Button2" name="" value="Login" style="position:absolute;left:267px;top:10px;width:51px;height:54px;z-index:3;">
<div id="wb_Text1" style="position:absolute;left:3px;top:4px;width:92px;height:32px;z-index:4;">
<span style="color:#000000;font-family:Arial;font-size:27px;"><strong>Login:</strong></span></div>
<input type="button" id="Button1" onclick="window.location.href='http://www.dubelou.com/ccmen/Forum.php'; ;return false;" name="" value="Forum" style="position:absolute;left:8px;top:42px;width:71px;height:25px;z-index:5;">
</form>
</div>
Member Avatar for diafol

Doh!

$sel = (isset($_POST['username']) && $_POST['username'] == $nt['id']) ? ' selected="selected"' : '';

I got caught between a ternary and a regular!

Doh!

$sel = (isset($_POST['username']) && $_POST['username'] == $nt['id']) ? ' selected="selected"' : '';

I got caught between a ternary and a regular!

That did it, thank you ardav. Lou

Member Avatar for diafol

OK, mark as solved.

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.