$_POST only receives default value from select control - php

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>";
}
?>

Related

How can save drop down selected values. dropdown located in table column as shown in the image

I want to save each row by getting the ID of selected value in the dropdown like this:
This is supposed to be a comment, but i have a low reputation here
Hi Khan i do not really understand what you mean.
A dropdown is basically a select option. The attribute "value" is what gets posted as the selected value.
To save the value, simply have this:
$selected_value = mysqli_real_escape_string($connection, $_POST['name_of_dropdown_from_html_form']);
To get the value on an update form, do this:
<select id="id_of_field" name="name_of_field" class="form-control">
<?php
#i assume your select dropdown list is from a database.
#SO in that table, we have two columns, id_of_table and list_type.
#For example a list of different types of cars,
#id(1) and list_type(BMW).
#SO now we check if the id of the car is in the current dropdown list
$list = $class->get_list();
foreach ($list as $key => $value) {
if ($value['id_of_table'] == $value['list_type']) {
$selected = "selected='selected'";
echo "<option value=\"{$value['id_of_table']}\" {$selected}>{$value['list_type']}</option>";
} else {
echo "<option value=\"{$value['id_of_table']}\">{$value['list_type']}</option>";
}
}
?>
</select>
try and integrate this with your project and let me know what you get

Sticky select box with while loop

I can't seem to get this right. I am trying to make a select box that gets it values from the database sticky please see my code below :
<select name="cob_bank">
<?php while($row = mysqli_fetch_array($query,MYSQLI_ASSOC)){
if($row['Id']==$myId) {
echo '<option value="'.$row['Id'].'"selected="selected">'.$row['Name'].'</option>';
} else {
echo '<option value="'.$row['Id'].'">'.$row['Name'].'</option>';
}
}?>
</select>
after the select box is filled in and the submit button is pressed where I save my Post variable
$myId = $_POST['cob_name'];
The above code does not work I please assist
the no space could be causing the problem as a space before selected
if($row['Id']==$myId) {
echo '<option value="'.$row['Id'].'" selected="selected">'.$row['Name'].'</option>';
}
The select name should be set as:
<select name='cob_name'>

Get the selected value of a <select> tag in php

Is there a way to find out what is the value of the <option> in a <select> tag?
I have a form that contains a tag, with several options. Then, I want to insert the selected value on the database.
Is this doable with PHP? Or do i need JS/Jquery?
EDIT: this is what i have tried:
This creates the variable and stores the value of the option.
$query=mysql_query("Select cargo from utilizadores where nome<>'admin'");
echo '<select name="icargo[]">';
while ($row = mysql_fetch_array($query, MYSQL_ASSOC))
{
foreach ($row as $cargo){
echo '<option value="$cargo" name="icargo[]">'.$cargo.'</option>';
}
}
echo '</select>';
I send the variable this way, and then i try to receive it with a foreach:
$countcargo = 0;
foreach ($_POST["icargo"] as $cargoo){
$cargoarray[$countcargo] = $cargo;
$countcargo++;
}

Drop down menu with form data

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>";
?>

set default drop down value on php generated form

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.

Categories