Jump to content

[SOLVED] Array to Drop Down Form Box ---help


savagenoob

Recommended Posts

I have a table in mysql for offices and I want to display all the offices in the table on a drop down box in a form. Its for a login system and they have to specify which office they are in.

 

Here is my wrong code:

$office -> $myrow['office'];
function createDropdown($arr, $frm) {

echo '<select name="'.$frm.'" id="'.$frm.'"><option value="">Select one…</option>';

foreach ($arr as $key => $value) {

echo '<option value="'.$value.'">'.$value.'</option>';

}

echo '</select>';

} 
?>
<form action="process.php" method="POST">
<table align="left" border="0" cellspacing="0" cellpadding="3">
<tr><td>Username:</td><td><input type="text" name="user" maxlength="30" value="<? echo $form->value("user"); ?>"></td><td><? echo $form->error("user"); ?></td></tr>
<tr><td>Password:</td><td><input type="password" name="pass" maxlength="30" value="<? echo $form->value("pass"); ?>"></td><td><? echo $form->error("pass"); ?></td></tr>
<tr><td>Email:</td><td><input type="text" name="email" maxlength="50" value="<? echo $form->value("email"); ?>"></td><td><? echo $form->error("email"); ?></td></tr>
<label for="Office">Office:</label>
<?php createDropdown($office, 'office'); ?>
<tr><td colspan="2" align="right">
<input type="hidden" name="subjoin" value="1">
<input type="submit" value="Register"></td></tr>
<tr><td colspan="2" align="left"><a href="main.php">Back to Main</a></td></tr>
</table>
</form>

 

Link to comment
https://forums.phpfreaks.com/topic/139913-solved-array-to-drop-down-form-box-help/
Share on other sites

If $arr is a valid array containing the key / value pairs for the offices, then your code should work. You have the right idea, you just need to ensure that the array your looping through is the array that contains the data you wish.

 

Looking at the function again, your calling it right, and passing data to it, so is $office an array with all the information you wish to use in the form?

 

Nate

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.