Reputation: 4961
how do i make a drop down with integer values 1-12, so that when one is chosen, that value is saved into the variable
I just want a select field in html
that is populated with 1-12
so
<select name="dropdown" option value= ? ? ?></select>
Upvotes: 2
Views: 16780
Reputation: 36629
Since you're using PHP, you can use a loop to generate the <option>
elements:
<form method="POST">
<select name="dropdown">
<?php
for ($x=1; $x<=12; $x++) {
echo ' <option value="' . $x . '">' . $x . '</option>' . PHP_EOL;
}
?>
</select>
<input type="submit" value="Submit">
</form>
And to read the variable:
<?php
if (isset($_POST['dropdown']) {
// cast to integer to avoid malicious values
$dropdown = (int)$_POST['dropdown'];
}
?>
Upvotes: 3
Reputation: 4068
Another option using php
<?php
echo "<select name=\"numbers\">";
for($i = 1; $i <= 12 ; $i++){
echo "<option value=\"$i\">$i</option>";
}
echo "</select>"
?>
Upvotes: 0
Reputation: 219027
In HTML:
<select name="myDropDown">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
</select>
In PHP:
// Note: Perform input sanitizing and type checking as needed here...
$myVariable = $_POST["myDropDown"];
Naturally, there's more scaffolding around all of this which is omitted here for brevity. If there's more you need to know about any of that, please update the question to be more specific. As it stands now, it's difficult to determine your experience level with the technologies in question.
Upvotes: 2