PHP Drop Down Boxes always Isset - php

HI i have two drop down boxes which are used to filter data from a mysql table, the problem is the boxes always show as if they are set when i use isset to determin if any of them have any values selected.
Here is the code for the drop boxes.
<?php
if (isset($_POST['referrer']) && $_POST['referrer'] != "referrer") {
$select = $_POST['referrer'];
}
?>
<select name="referrer">
<option value="">Referrer</option>
<?php
// Get records from database (table "name_list").
$list=mysql_query("select DISTINCT referrer from masterip_details WHERE country_code='GB' AND TRIM(IFNULL(referrer,'')) <> '' order by referrer DESC");
// Show records by while loop.
while($row_list=mysql_fetch_assoc($list)){
$referrer = $_POST['referrer']; ?>
<option value="<?php echo $row_list['referrer']; ?>" <?php if($row_list['referrer']==$select){ echo "selected"; } ?>><?php echo $row_list['referrer']; ?></option>
<?php
}
?>
</select>
<?php
if (isset($_POST['returning']) && $_POST['returning'] != "returning") {
$select2 = $_POST['returning'];
}
?>
<select name="returning">
<option value="">Referrer</option>
<?php
// Get records from database (table "name_list").
$list2=mysql_query("select * from repeater_drop_down order by id DESC");
// Show records by while loop.
while($row_list2=mysql_fetch_assoc($list2)){
$returning = $_POST['returning']; ?>
<option value="<?php echo $row_list2['value']; ?>" <?php if($row_list2['value']==$select2){ echo "selected"; } ?>><?php echo $row_list2['title']; ?></option>
<?php
}
?>
</select>
Now here are the isset elements of the code.
$returning = $_POST['returning'];
$referrer = $_POST['referrer'];
if (isset($returning, $referrer)){
//code //
elseif (isset($returning)){
///code //
elseif (isset($referrer)){
//code //
else {
//code
}
I haven't included the queries as they work fine but whether or not i select both drop down boxes the code automatically goes to the first isset for both variable even if only one of the boxes has a option selected.
Any advice or suggestions would be appreciated.

You should check the value that was submitted with the form, not only whether or not the select POST value is set.
The value of the referrer will be '' (an empty string) if it is submitted without changing the select. Check for that instead.

Related

Retain selected option after form submitted

I have the following code that get some options from database using php and mysql
<select class="form-control" id="Type" name="Type">
<option></option>
<?php
$TypeQuery = "SELECT DISTINCT Type FROM Details";
$TypeQueryExecute = mysqli_query($conn, $TypeQuery);
while($TypeQueryRow = mysqli_fetch_array($TypeQueryExecute)){
$Type = $TypeQueryRow['Type'];
echo "<option value='{$Type}'>{$Type}</option>";
}
?>
</select>
I want to retain the dropdown list selection after form submission. I am using php $_POST[''] method for form submission. I tried the following way. But it is not working.
<select class="form-control" id="Type" name="Type">
<option></option>
<?php
$TypeQuery = "SELECT DISTINCT Type FROM oaDetails";
$TypeQueryExecute = mysqli_query($conn, $TypeQuery);
while($TypeQueryRow = mysqli_fetch_array($TypeQueryExecute)){
$Type = $TypeQueryRow['Type'];
?>
<option <?php if ($_POST['Type']==$Type) echo 'selected="selected"'; ?> >
<?php echo $Type; ?>
</option>
<?php
}
?>
</select>
Does anyone know what am I doing wrong here?
Edit 1
I tried the following way. Now it is not showing any options. Instead some errors
<option value=<?php echo $_POST['Type']; ?> <?php if ($_POST['Type'] == $Type) echo 'selected="selected"'; ?> ><?php echo $_POST['Type']; ?></option>
Edit 2
I tried following way and this time it is retaining the values. But only thing is if I select an option with spacing in between and click submit, the value is not retaining and just go to default blank. You can see what I mean by option with spacing as below
Options with no space as below retaining and working well as per my need
You are missing some quotes in your output HTML, that's why spaces in the value throw you out. It should be
<option value="<?php echo $_POST['Type']; ?>" <?php if ($_POST['Type'] == $Type) echo 'selected="selected"'; ?> ><?php echo $_POST['Type']; ?></option>

listbox value goes 0 in database if not selected on edit mode in php

I have three tables
City: city_id, city_name
State: State_id, State_name
News: City_id, State_id, Headline, Story, Author etc.
I am Displaying data in textbox and listbox on edit button click Of Selected ID.
Data is displaying in listbox properly of selected id but when i click save button without change of listbox value it goes 0 in database.
If I don't change the listbox value and click save then it inserting 0 from listbox.
<?php
$data = 0;
if(isset($_GET['edit']))
{
$id = clean($_GET['edit']);
mysql_set_charset('utf8');
$sql="SELECT city_name,state_name,category_name,headline,author,story,source,photo,date from news left join
city on news.city_id=city.city_id left join state on news.state_id=state.state_id left join category on news.cat_id=category.category_id where id = '$id'";
$result=mysql_query($sql);
$data=mysql_fetch_array($result);
}
?>
<?php
$cat = $Admin->select($Admin->cat_table,'','','');
?>
<select name="cat_id" class="select" required="">
<option value="<?php if(isset($_GET['edit'])){ echo $data['category_name']; }?>"><?php if(isset($_GET['edit'])){echo $data['category_name'];}?></option>
<?php
foreach($cat as $load_category)
{
?>
<option value="<?php echo $load_category["category_id"]; ?>"><?php echo $load_category["category_name"]; ?></option>
<?php }?>
</select>
<?php
$errors = array();
$Admin = new admins;
if(isset($_POST['save']))
{
$table = $Admin->news_table;
if(isset ($_GET['edit']))
{
$id = clean($_GET['edit']);
$cond = "id = '$id'";
if($Admin->save_advance($table,$_POST,'',$cond))
{
$_SESSION['message'] = "News Updated Successfully";
header("Location:add_news.php");
exit;
}
}
}
?>
What am I doing wrong?
Tell me if I'm wrong but this line
<option value="<?php if(isset($_GET['edit'])){ echo $data['category_name']; }?>">
<?php if(isset($_GET['edit'])){echo $data['category_name'];}?>
</option>
seems strange to me, it sets the category name as value for first option in the select. Basically, if you don't change the listbox, you are trying to update a row where the category_id is a category_name.
This should be:
<option value="0">
<?php if(isset($_GET['edit'])){echo $data['category_name'];}?>
</option>
But if you have to update something you can't do that without a value. So add some kind of validation that forbids to click the save button without setting a value in the select, or just do something like this (adds a default category to the listbox)
<select name="cat_id" class="select" required>
<?php
$counter = 0;
foreach($cat as $load_category){ ?>
<option <?php echo ($counter == 0) ? 'selected' : ''; ?> value="<?php echo $load_category["category_id"]; ?>"><?php echo $load_category["category_name"]; ?></option>
<?php
//increments counter
$counter++; }?>
</select>

combo box posting selection not value not other page submit

I have a Select/Combo Box that I use to select the number of records to display on a page. In order to cover the 'All' option I pass the ID and the Number Selected in the value field.
<?php //get info for results per page combo box //
$stmt = $db->prepare("SELECT resultspp.ID, resultspp.NumberResults FROM resultspp");
$stmt->execute();
$rows2 = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows2 as $row) {
if ($pageID !=$row['ID']) { ?>
<option value="<?php echo $row['ID']; ?>|<?php echo $row['NumberResults']; ?>"><?php echo $row['NumberResults']?></option>
<?php } else { ?>
<option value"<?php echo $row['ID'];?>|<?php echo $row['NumberResults']; ?>" selected><?php echo $row['NumberResults']?></option>
<?php }
}
?>
This all works fine and the correct values are posted, checked using print_r($_POST). However when another buttom is submitted on the page it posts the option displayed to the user, not the value in the value tag. I have checked this by change the selected option to a constant.
The values are unpacked like so after the post.
<?php
if($_POST['resultsPP']){
$pageresult = explode('|', $_POST['resultsPP']);
$pageID = $pageresult[0];
if ($pageresult[1] == 'All'){
$number_select = $totalRecords;
} else {
$number_select = $pageresult[1];
}
}
?>
Thanks in advance
You have missed "=" sign in your code. Below is the updated code. This causing the posting of option instead of posting of value.
.........
else { ?>
<option value="<?php echo $row['ID'];?>|<?php echo $row['NumberResults']; ?>" selected><?php echo $row['NumberResults']?></option>
<?php }
.......

PHP Form select correctly populates MySQL values, does not with simple PHP echo

I have a PHP select that dynamically populates based on a MySQL recordset and uses a in_array value to identify and select the value(s) found in the database like this:
<select name="StaffSetSelection[]" size="5" multiple="MULTIPLE" id="StaffSetSelection">
<option <?php if ($totalRows_StaffSetID == 0) { echo "selected"; } ?> value="">Choose a Staff Set</option>
<?php
do {
?>
<option <?php if (in_array($row_StaffSetChoices['EmpNumber'], $StaffSetIDs)) {
echo "selected";
} ?> value="<?php echo $row_StaffSetChoices['EmpNumber'] ?>"><?php echo $row_StaffSetChoices['EmpFirstName'] ?></option><?php
} while ($row_StaffSetChoices = mysql_fetch_assoc($StaffSetChoices));
$rows = mysql_num_rows($StaffSetChoices);
if ($rows > 0) {
mysql_data_seek($StaffSetChoices, 0);
$row_StaffSetChoices = mysql_fetch_assoc($StaffSetChoices);
}
?>
</select>
Instead of displaying the selected values in a form option select I'd like to just display them as text.
I've tried to use the in_array again in a simple php echo but it does not display the data and there are no errors in my apache log.
This is what I've tried:
<?php if(in_array($row_StaffSetChoices['EmpNumber'], $StaffSetIDs)) echo $row_StaffSetChoices['EmpFirstName']?>
I think you forgot the brackets,
Try this way:
<?php
if(in_array($row_StaffSetChoices['EmpNumber'], $StaffSetIDs)) {
echo $row_StaffSetChoices['EmpFirstName'];
}
?>

set multiple select list options to selected from query

I have a field named 'majors' which contains a string of integers seperated by commas. This field is populated from a multiple select list on insert query using the following variable:
$majors_string = implode(",", $majors);
Now on the edit page, I want to have those values set the options in the same list. Part of the problem is that the list is generated dynamically using:
<select name="majors[]" size="9" multiple="multiple" id="majors">
<?php
$query = mysql_query("SELECT * from major ORDER BY title ASC");
for($i=0;$i<mysql_num_rows($query);$i++) {
$row=mysql_fetch_assoc($query);
?>
<option value="<?php echo $row['major_pk']; ?>"><?php echo $row['title']; ?></option>
<?php
}
?>
</select>
On the initial query on the edit page, I have:
$query_topic = "SELECT * FROM topic WHERE topic_pk = '$topic_pk'";
$result_topic = mysql_query($query_topic, $connection) or die(mysql_error());
$row_topic = mysql_fetch_assoc($result_topic);
$retrieved_majors = explode(",", $row_topic['majors']);
But I'm unsure as to set the options in the multiple list to the values in array $retrieved_majors.
Thanks for any help!
I think you're asking how to set the default for which options are selected, right?
<option value="<?php echo $row['major_pk']; ?>" <?php if(array_search($row['major_pk'], $retrieved_majors) !== false) {echo 'selected';} ?> ><?php echo $row['title']; ?></option>

Categories