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;
Related
I have a list box and have 2 questions.
1st how can i still have the text to say Select a country?
2nd on my update page it displays the proper country but will not allow it to be change for it will not list any of the other countries in edit page
<?php
$sql = "SELECT countries.country_id, countries.country_name, users.user_country FROM countries, users
WHERE users.user_country = countries.country_id";
$result = query($sql);
?>
<select class="form-control input-lg box" id="country" name="country">
<?php
$i = 0;
while (($row = mysqli_fetch_assoc($result)) != false) {
?>
<option value="<?=$row["country_id"];?>"><?=$row["country_name"];?></option>
<?php
$i ++;
}
?>
Well, to have a default option not provided by your database call, you just need an HTML statement such as
<option value="">Select a country</option>
right below your <select> statement.
And we can't see your table definitions, but your SQL code seems to be picking the specific country that the user is associated with, so that there is only one country in the list. You probably need two queries: One that obtains the user's current country, and a second that obtains the entire list of countries.
After the first query, save the user's current countryid in a variable - say, $usercountryID.
Then, in your loop through the countries, compare each countryID to the user's country, like this:
while (($row = mysqli_fetch_assoc($result)) != false) {
if ($usercountryID == $row["country_id"]) $selected = " selected";
else $selected = "";
echo "<option value='{$row['country_id']}$selected>{$row['country_name']}</option>\n";
}
$selected could be defined many ways, of course, including in a conditional assignment statement. I omitted the $i counter, which isn't needed here, but, of course, you might need it later.
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>";
}
?>
Hi I have a piece of php code which takes php variables from a database and then populates A drop down box. This works fine but once the page updates it doesn't display the selected value and instead just displays the original list again.
I have played around with some different ideas of using isset to displays the selected value, but then it just displays that value and nothing else.
I have attached the original code where it works but always only shows the original list of values from the database.
If any body could provide a solution or even a nudge in the right direction then it would be much appreciated.
Thanks
if (isset($select) && $select != "location") {
$select = $_POST['location'];
}
?>
<select name="location">
<?php
// Get records from database (table "name_list").
$list = mysql_query("select DISTINCT region_name from masterip_details WHERE country_code='GB' AND TRIM(IFNULL(region_name,'')) <> '' order by region_name asc");
// Show records by while loop.
while ($row_list = mysql_fetch_assoc($list)) {
$location = $_POST['location']; ?>
<option value="<?php echo $row_list['region_name']; ?>"
<?php if $row_list['region_name'] == $select) {
echo "selected";
} ?>><?php echo $row_list['region_name']; ?></option>
<?php } ?>
You are sure you send that form with a post request?
Your code is unreadable
Better do something like this:
<?php
//database request don't belong in the presentation layer
$list=mysql_query("select DISTINCT region_name from masterip_details WHERE country_code='GB' AND TRIM(IFNULL(region_name,'')) <> '' order by region_name asc");
$location = "";
$isSelect = "";
if(isset($_POST['location'])){
$location = $_POST['location'];
}
while($row_list=mysql_fetch_assoc($list)){
if($row_list['region_name'] == $location ){
$isSelect = "selected";
}
echo '<option value="'.$row_list['region_name'].'" '.$isSelect.' >'. $row_list['region_name'].'</option>';
}
?>
You are not checking the select value properly.
Change this:
if (isset($select) && $select != "location") {
$select = $_POST['location'];
}
TO
if (isset($_POST['location']) && $_POST['location'] != "location") {
$select = $_POST['location'];
}
While creating the <option> tag, you may use the following:
<?php if ($row_list['region_name'] == $select) { echo "selected='true'"; } ?>
You may also like to check if the condition $row_list['region_name'] == $select is returning true. You may try to echo their result separately as:
echo ($row_list['region_name'] == $select)
or something similar that would show if you get true or not.
The problem lies in the first line - actually the system will never execute 2nd line to assign $select because $select is never set:
if(isset($_POST['select']) && $select!="location") { $select=$_POST['location']; } ?>
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":"";
I have 2 drop-down lists with different names, and I'm trying to query in just one field.
I'm using a jQuery function wherein if item 1 is selected, the drop-down list 1 will be displayed, and if the item 2 is selected, the drop-down list 2 will be displayed.
This is how I populated my drop-down list from the mysql database and tables:
<div id="minquep">
<label>Branch</label>
<SELECT name="user_min">
<OPTION VALUE="0">Choose a branch
<?=$minq_options?>
</SELECT>
</div>
<div id="albury">
<label>Branch</label>
<SELECT name="user_branch">
<OPTION VALUE="0">Choose a branch
<?=$al_options?>
</SELECT>
And this is how I insert queries into mysql by filling out the form with drop-down lists in it:
if (isset($_REQUEST['Submit'])) {
$sql = "INSERT INTO $db_table(branch) values ('".mysql_real_escape_string(stripslashes($_REQUEST['user_branch'])).",".mysql_real_escape_string(stripslashes($_REQUEST['user_min']))."')";
if($_REQUEST['user_branch']= ""){
($_REQUEST['user_branch']) = NULL;
}
if($result = mysql_query($sql ,$db)) {
echo '<script type="text/javascript">alert("The user has been added successfully!\n");return true;</script>';
echo "<meta http-equiv=\"refresh\" content=\"0;URL=add_user.php\">";
}
else {
echo "ERROR: ".mysql_error();
}
}
The testing scenario is that, I choose the value under <select name="user_min">.
So I assume that sql will just bypass the result for user_branch because it is null. But it does prints '0' instead, after the insert query. For example, if the inserted ($_REQUEST['user_min']) value is "Brisbane" and the ($_REQUEST['user_branch']) value is null (because I didn't selected any value under the user_branch drop-down list), the branch field should just become "Brisbane", knowing that user_branch is NULL. But it does print "BRISBANE" with 0, like 0, Brisbane in my mysql table.
How can I fix this?
I already tried putting an if condition, it did not work.
if($_REQUEST['user_branch']= ""){
($_REQUEST['user_branch']) = NULL;
}
I've also tried changing the user_min into same name user_branch, but it does not get the selected value, instead of Brisbane it just prints '0'
A few things -
you are setting user_branch = NULL after the $sql.
You are using quotes around value. It's fine non-Null values -you need to check if there is NULL value then don't use quotes in your $sql.
You are using single "=" in your IF statement. This is failing your If statement. Change it to if($var == '')
Update
Some suggestions based on your existing code - however there are other best practices to achieve what you are trying to achieve....
$user_branch = $_REQUEST['user_branch'];
$user_min = $_REQUEST['user_min'];
//you should validate above values first
if$user_branch == "" || $user_min == "") {
$db_value = "NULL";
} else
{
$db_value = "'".mysql_real_escape_string(stripslashes($user_branch.','.$user_min))."'";
}
$sql = "INSERT INTO $db_table(branch) values (".$db_value.")";
If you don't want 0, then change:
<option value="0">Chose a Branch</option>
To:
<option value="">Chose a Branch</option>
Otherwise it will get passed through the form as 0
Also, your PHP code won't work as you want it to:
if (isset($_REQUEST['Submit'])) {
// do this before the query
if($_REQUEST['user_branch']== ""){ // note the extra = in there, so you aren't assigning the variable, you are compairing
$_REQUEST['user_branch'] = NULL; // no need for brackets round the variable
}
$sql = "INSERT INTO $db_table(branch) values (".mysql_real_escape_string(stripslashes($_REQUEST['user_branch'])).",".mysql_real_escape_string(stripslashes($_REQUEST['user_min'])).")"; // you had single quotes around everything inside the VALUES() function
if($result = mysql_query($sql ,$db)) {
echo '<script type="text/javascript">alert("The user has been added successfully!\n");return true;</script>';
echo "<meta http-equiv=\"refresh\" content=\"0;URL=add_user.php\">";
}
else {
echo "ERROR: ".mysql_error();
}
}
$sql = "INSERT INTO reg(Name ,Email) values ('".mysql_real_escape_string(stripslashes($_REQUEST['Name']))."','".mysql_real_escape_string(stripslashes($_REQUEST['Email']))."')";
This is my code. Here I found error of Undefined index.