php select-option post back retain value - php

I have a form with input type-select
however when page submit and post back with error message
The select value is clean, I try to use radio button (checked unchecked) to retain the value,
yet it is not working.
//HTML///////////////////////////////////////////////////////////////////
<select name="age">
<option value="">Age</option>
<option value="14-17" <?PHP echo $age_14; ?>>14 - 17</option>
<option value="18-29" <?PHP echo $age_18; ?>>18 - 29</option>
</select>
//PHP////////////////////////////////////////////////////////////////////
$age_14="unchecked";
$age_18="unchecked";
$age=$_POST['age'];
if($age=='14-17'){$age_14="checked";}
else if($age=='18-29'){$age_18="checked";}

With select tag you have to use selected="selected" not checked.
Try with:
$age_14 = "";
$age_18 = "";
$age = $_POST['age'];
if ($age == '14-17') {
$age_14 = 'selected="selected"';
} elseif ($age == '18-29') {
$age_18 = 'selected="selected"';
}

With select boxes, the attribute is selected, not checked. You also do not need the unchecked attribute.

There is no checked attribute. You need to use selected:
$age=$_POST['age'];
$age_14 = $age=='14-17'?"selected":"";
$age_18 = $age=='18-29'?"selected":"";

Related

Keep selected option in php

I have below code which runs query correctly but does not keep selected option. I could get similar answers but not worked with this code. Here select options are in combination of user given and rest are from mysql column.
I am new to php, so any help will be appreciated.
<select name="tester">
<option value=""> -----------ALL----------- </option>
<?php
$dd_res=mysqli_query($conn, "Select DISTINCT tester from workflow1");
while($r=mysqli_fetch_row($dd_res))
{
echo "<option value='$r[0]'> $r[0] </option>";
}
?>
</select>
<input type="submit" name="find" value="find"/>
You can use the $_POST superglobal array to check the value of submitted form fields. In this case $_POST['tester'] contains the value of the select field after submitting the form.
echo "<option value='$r[0]'".($_POST['tester']==$r[0]?' selected':'')."> $r[0] </option>";
I am thinking you are asking to select the value passed into the form? If I am correct in that, you would do something like:
$selVal = $_REQUEST['tester'];
$val = $r[0];
echo '<option value="'.$val.'"';
if ($val == $selVal) {
echo ' selected="selected"';
}
echo '>'.$val.'</option>';
The $_REQUEST gets any POST, GET or COOKIE value and should be validated to make sure no hack attempts or anything. Once you have the value, you just add the selected attribute if it matches the value in the list.

Altering PHP Variable Based Off Selection

I have a html select that holds values for all four of our offices, or the text All. If the user selects All, I want to echo "All Offices", but if the user selects a specific office, I want to echo that number. My problem is that when I run the syntax below, All remains All instead of All Offices.
Did I set this up the incorrect way?
Display Data For Which Office:
<select name="office">
<option value="All">All...</option>
<option value="one">One Quarter</option>
<option value="two">Two Quarter</option>
<option value="three">Three Quarter</option>
<option value="four">Four Quarter</option>
</select>
<?php
if ($officename != 'All') {
$officename = $_POST['officename'];
} else {
$officename = "All Offices";
}
echo $officename;
?>
You never initialized the $officename variable, so it should be null. As a result, won't $officename != 'All' always be true, so $officename = $_POST['officename']; will always be executed?
I think what you want instead is something like:
$officename = $_POST['officename'];
if ($officename == 'All') {
$officename = "All Offices";
}
echo $officename;
I assume you're wanting "All" to come from the value of the <select>, which is named "office". Therefore you need to assign that value to a variable before you can use it for comparison. Note the select is named "office" not "officename", so it's "office" you need to look for in $_POST.
$officeValue = $_POST['office'];
if ($officeValue == 'All') {
$officename = "All Offices";
}
else {
$officename = $_POST["officename"]; //I assume this is some other field in the form which you haven't shown, but which contains a human-readable office name.
}
echo $officename;

$_POST only receives default value from select control

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

How to use assign html select value to a php variable

<select name="parentgroup" type="text" id="select2" onchange="this.form.submit();">
<?php
echo "<option value=\"0\">All Users</option>";
$result = mysql_query("select * from login WHERE stato=1");
while ($myresults = mysql_fetch_array($result))
{
$username = $myresults['user_name'];
echo "<option value=\"".$username."\" ";
echo $username== $parentgroupid ? " selected" : "";
echo ">".$username."</option>";
}
?>
</select>
Hi...am supposed to use the first element in <select> which is <option value=\"0\">All Users</option> to fetch values from a mysql database.
I want to now how you can assign this to a variable and use it just like $parentgroupid
When you submit the form (which presumably exists since you are referencing it from your JavaScript): Get the value from $_GET['name_of_form_control'] in the document referenced by the action attribute of the form.
When you submit the form, depending on the method specified in the form ("GET" or "POST"), the value will be in either the $_GET or $_POST arrays on the page to which you are submitting. You can retrieve the value by name (the name of the control):
$parentgroupid = $_GET['parentgroup'];
or
$parentgroupid = $_POST['parentgroup'];

how to set value for <select> tag

Using Php I have wrote the drop down list box code.
for Edit employee details I have used the same code and I tried to set the value on list box but the value is not set. how to do this.
Employee Name: <select name=emp_name *$value='$name'*>
<option> -- EMPLOYEE NAME -- </option>
<?
while ($q->fetchInto($row)){
print "<option>$row[0]</option>";
}
?>
</select>
You have to set the selected option to selected while building the options like so:
<select name='emp_name'>
<?
//Define the array of possible options.
//This can come from the database fetch such as:
$options = $q->fetchMutipleVals();
//Or defined manually such as:
$options = array('value1'=>'label1', 'value2'=>'label2'); //etc.
foreach ($options as $val => $label){
$selected = ($name == $val)? "selected='selected'":''; //sets html flag
echo "<option value='$val' $selected>$label</option>\n";
}
?>
</select>
Test for the right value as you iterate and mark the <option> you want to have as the selected value by using the selected='selected' attribute:
Employee Name: <select name="emp_name">
<option> -- EMPLOYEE NAME -- </option>
<?php
while ($q->fetchInto($row)){
if($row[0] == $name){
$selected_text = " selected='selected'";
}else{
$selected_text = "";
}
print "<option{$selected_text}>{$row[0]}</option>";
}
?>
</select>
You need the option tag you wanted selected to have a selected attribute:
<select>
<option> -- EMPLOYEE NAME -- </option>
<option>Sally</option>
<option selected="selected">Fred</option>
<option>Liz</option>
</select>
try
Employee Name: < select name=emp_name $value='$name' > < option > -- EMPLOYEE NAME -- < / option >
< ? while ($q->fetchInto($row))
{ print "<option>$row[0]</option>"; } < / select >
? >

Categories