I need to make a drop down menu that defaults to the account type passed in via a form.
I know that I can put 'selected' as an option attribute to make that option the default selection.
But I don't always know what the type will be, so how would I go about making it default to match the type.
Here is my Code :
<?php
$type=$_POST['Type'];
echo "<select name='AccType'>
<option>User Type</option>
<option value='admin'>Administrator</option>
<option value='ban'>Banned</option>
<option value='mod'>Moderator</option>
<option value='new'>New User</option>
<option value='spec'>Special</option>
</select>";
?>
So say for example, an account in the moderator group is being edited, so the 'mod' type is passed in by the form.
I want the page to display 'Moderator' in the menu by default so that the user doesn't have to select it to make sure the account stays as Moderator in case they submit without checking.
Is this possible with just PHP and HTML ?
You could do something like this: make an array of all the option values and texts:
$options = array( 'admin' => "Administrator", 'ban' => "Banned", 'mod' => "Moderator", 'new' => "New User", 'spec' => "Special" );
Each key corresponds to a option value, and each value in the array corresponds to the text displayed to the user.
Then, you would just display the option that is being submitted in the $_POST variable first.
<?php
$type = $_POST['type'];
echo "<select name='AccType'>";
echo "<option disabled>User Type</option>"; // note disabled attribute.
if(array_key_exists($type)) {
echo "<option value='$type' selected>$options[$type]</option>";
}
// echo the rest of the fields.
for($options as $value => $text) {
if(strcmp($value, $type) !== 0) {
echo "<option value='$value'>$text</option>";
}
}
echo "</select>";
?>
Try this-
<?php
$type=$_POST['Type'];
$data=array(
'admin'=>'Administrator',
'ban'=>'Banned',
'mod'=>'Moderator',
'new'=>'New User',
'spec'=>'Special',
);
echo "<select name='AccType'>";
echo "<option value='.$type.' selected='selected' >.$data[$type].</option>";
foreach($data as $key=>$val)
{
if($key!=$type)
{
echo "<option value='.$key.'>.$val.</option>";
}
}
echo "</select>";
?>
Related
I use the following code to select all value from a DB table and put it in dropdown. Here is the code :
<select name="dis_name">
<?php
$qu="Select DISTINCT name from root";
$res=mysqli_query($con,$qu);
while($r=mysqli_fetch_row($res))
{
echo "<option value='$r[0]'> $r[0] </option>";
}
?>
Based on previous code a value is selected and submitted. I want to show previously selected value in dropdown on another page so i use following code:
<select name="dis_name">
<option value="RAM"<?php echo $r20 == "RAM" ? " selected" : ""; ?>>RAM</option>
<option value="ROHAN"<?php echo $r20 == "ROHAN" ? " selected" : ""; ?>>ROHAN</option>
</select>
This code work fine till no new value added in DB table. But whenever i add new value i have to update edit code manually. Is there any way, so that dropdown previous selection updated automatically in edit page.
Add the condition in your loop, eg:
<?php
while($r=mysqli_fetch_row($res))
{
echo "<option value='$r[0]'";
if ($r[0] == $r20) echo " selected";
echo "> $r[0] </option>";
}
?>
If I get what you want (assuming that $r20 is the previously selected value:
<?php
$qu="Select DISTINCT name from root";
$res=mysqli_query($con,$qu);
while($r=mysqli_fetch_row($res))
{
echo "<option value='$r[0]'";
if($r[0]==$r20){echo 'selected="selected"';}
echo "> $r[0] </option>";
}
?>
this way the select will display as selected the option that match the $r20 value dinamically.
BTW, you should use id in your code. This way you could also have more than one user with the same name (but different ID). With DISTINCT, as you are doing, if you have more than one user with the same name you will get only one.
This is sort of an extension of the problem solved here: Set default value for HTML select control in PHP however I would like to fill in Multiple values that match, with the values to fill in stored in an additional array:
This is my code so far:
<select name="genres[]" id="genres_edit" multiple>
<?php
$genrelist = array(
'Action',
'Adventure',
'Comedy',
'Cooking',
'War',
'Western');
for($i = 0;$i < count($genrelist);$i++) {
echo "<option value=\"$genrelist[$i]\"";
for ($g = 0; $g < count($genre);$g++) {
if ($genrelist[$i] == $genre[$g]) {
echo "selected=\"selected\"";
}
echo ">$genrelist[$i]</option>";
}
}
?>
</select>
$genrelist is the array of all possible genres that will be used to fill up the select control, and the array of actual genres is stored in $genre.
Basically I want it to highlight the values in the selectbox that match any of the values in the $genre array.
i.e. if the genres stored in $genres are: Adventure, Cooking, Western, then those 3 values will be highlighted in the select box, out of the 6 available genres in the box.
Here's how I'd do it ...
$genres = array(
'Action',
'Western'
);
$genrelist = array(
'Action',
'Adventure',
'Comedy',
'Cooking',
'War',
'Western');
foreach ($genrelist as $k=>$v) {
$sel = (array_search($v,$genres) !== false) ? ' selected' : '';
echo '<option value="'. $k .'"'. $sel .'>'. $v .'</option>';
}
Here's the sandbox ... http://sandbox.onlinephpfunctions.com/code/e4f2ca28e0fd43513b694f5669329cc1db328598
Assuming your form in being set with method="get" then the $_GET superglobal should have an array in it called genres (as defined by the fact that your multiple select box is called genres[]).
So when looping through your output you should be able to check if the current genre (from the $genrelist array) exists in the $_GET['genres'] array ... like so:
<?php
$genrelist = array(
'Action',
'Adventure',
'Comedy',
'Cooking',
'War',
'Western');
?>
<select name="genres[]" id="genres_edit" multiple="multiple">
<?php foreach($genrelist as $genre): ?>
<?php $sThisSelected = in_array($genre, $_GET['genres']) ? " selected=\"selected\"" : ""; ?>
<option value=\"<?= $genre; ?>\"<?= $sThisSelected; ?>><?= $genre; ?></option>
<?php endforeach; ?>
</select>
There's no sanity checking or sanitisation in this but that's the theory anyway.
If you want multiple selection you must specify your select tag with multiple option
<select multiple="1">
<option selected>option1</option>
<option selected>option1</option>
<option selected>option1</option>
</select>
The drawback is that the select menu is no more a dropdown, if that matter for you, let me know and I will try to tune in the solution.
I have a select control that will load next to each user and the value defaults to which floor that user is located on based on the result from the MySQL Database. Whomever is editing the list can change which floor that user is located on and submit the change to push to the database. However when I receive the $_POST['selFloor'] value it is always whichever the default selected value is. No matter if the user changes it or not.
<?php
$floors = array('1st'=>"First",
'2nd'=>"Second",
'3rd'=>"Third",
'4th'=>"Fourth",
'5th'=>"Fifth",
'6th Control'=>"Sixth");
$query = "SELECT * FROM employees ORDER BY name asc";
$result = $db->query($query);
$i = 0;
while ($row = $result->fetch_array())
{
$i++;
echo '<select name="field['.$i.'][floor]"';
foreach($floors as $key=>$val) {
echo ($key == $row['floor']) ? "<option selected=\"selected\" value=\"$key\">$val</option>":"<option value=\"$key\">$val</option>";
}
echo '</select>';
} ?>
A sample of the select control. If the $row['floor'] returns ['1st'] it will make that option the selected value, but once the user changes it to '2nd' and hits submit, $_POST only see's the select value for whichever option has the selected argument.
foreach ($_REQUEST as $key => $val) {
if (is_array($val)) {
foreach($val as $subkey => $sub) {
echo $sub['floor'] // Outputs first option that got selected set
}
}
}
HTML Output of Select:
<select name="field[1][floor]">
<option value="1st">First</option>
<option value="2nd">Second</option>
<option selected="selected" value="3rd">Third</option>
<option value="4th">Fourth</option>
<option value="5th">Fifth</option>
<option value="6th">Sixth</option>
</select>
Thanks.
I don't see any errors in your HTML, except for the fact, that you are checking $_POST['selFloor'], while the name of select is field[1][floor]. Try to change it to 'selFloor':
echo '<select name="selFloor">';
...
And I don't see closing angle bracket (>) for select.
Because each one of your <option> menus has a selected attribute, the browser doesn't know which one is selected even though it is firing the change event.
Try placing the selected attribute on the first <option> only.
<?php
foreach($floors as $key=>$val) {
// if first, place selected attribute
echo ($key == $row['floor'])
? "<option value=\"$key\">$val</option>" // selected att was removed
:"<option value=\"$key\">$val</option>";
}
?>
I generated a drop down menu with the code below. How can I set the field to the GET variable after submit is clicked?
<select>
<?php
$sql="SELECT c FROM problems WHERE b='$id'";
$result=mysql_query($sql);
$options="";
while ($row=mysql_fetch_array($result)) {
$id=$row["c"];
$c=$row["c"];
$options.="<option value=\"$id\">".$c;
}
?>
<option value=''>C <?=$options?> </option>
</select>
<select>
<?php
$sql="SELECT c FROM problems WHERE b='$id'";
$result=mysql_query($sql);
$options="";
while ($row=mysql_fetch_array($result)) {
$id=$row["c"];
$c=$row["c"];
if($_GET['var'] == $id)
echo '<option selected="selected" value=\"$id\">' . $c . '</option>';
else
echo '<option value=\"$id\">' . $c . '</option>';
}
?>
</select>
Basic idea is compare value GET data with database data and using if else condition add selected="selected" if condition matched. I am directly printing string as they will not be getting use later on.
I'm a bit confused about what you want to know. To have an option selected by default, use the selected="selected" attribute on that option.
If you want to know how to get the submitted value in PHP, you need to assign the name attribute to the <select> tag. In general, however, you've got the idea right.
here is my mysql and php code layout:
I have 3 tables
tableA stores unique "person" information
tableB stores unique "places" information
tableC stores not unique information about a person and places they have "beenTo".
here is how i layed out my form:
-one big form to insert into "person" tableA; "beenTo" tableC
in the form, a person mulitple selects "places" which get inserted into "beenTo"
my question is, when i am editing a "person" how do i display what the user has already selected to appear on my multiple select options drop down menu?
my drop down menu at the moment query "places" table and displays it in a multiple select drop down menu. its easier when a person have beenTo one place, the problem arrises when there is more than one "beenTo" places?
Foreach option, check if they have beenTo it. Then add the selected="selected" attribute to the tag if true.
Example:
<select multiple="multiple">
<option selected="selected">Rome</option>
<option>France</option>
<option selected="selected">Underpants</option>
</select>
And in PHP this might look like:
$beenTo = array("Rome","Underpants");
$places = array("Rome","France","Underpants");
?> <select multiple="multiple"> <?php
foreach($places as $place) {
echo "<option";
$found = false;
foreach($beenTo as $placeBeenTo) {
echo "value='$place'";
if ($placeBeenTo == $place) {
$found == true;
echo " selected=\"selected\" ";
break;
}
}
if (!$found) echo ">";
echo $place . "</option>";
}
?> </select> <?php
There's probably a much more efficient way to do this.
For clarification, you will want to have a name attribute for your select tag which allows for multiple selected options to function correctly.
<form method="post" action="">
<select name="places[]" multiple="multiple">
<?php
$_POST += array('places' => array());
$places = array('fr' => 'France', 'cn' => 'China', 'jp' => 'Japan');
$beenTo = array_flip($_POST['places']);
foreach ($places as $place => $place_label) {
$selected = isset($beenTo[$place]) ? ' selected="selected"' : '';
echo '<option value="' . $place . '"' . $selected . '>' . $place_label . '</option>';
}
?>
</select>
<input type="submit" value="Save Changes" />
</form>
<option id = 'example' <? if ($_POST['example']) echo 'selected' ?>>
But you'll be using a $_SERVER['cookies'] or other cache to store the past visits, not the $_POST array.. edit with extreme predjudice