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']; } ?>
Related
Thanks for dropping by and I'm sorry if this question seems to be easy as I'm kinda new to php. Im trying to do a school project and my problem now is that I cant seem to get the values of the dropdown. I have been trying to search in every forum and some similar questions in here but they seem to only show how i can populate the dropdown with database values.
The code for populating the dropdown menu is working, but recording the selected value is not working for me.
Here is my php code for the dropdown:
<div class="panel">
<label class="panelname">Category:</label>
<select name="category" class="signup_field">
<option>Select One</option>
<? // retrieve all the categories and add to the pull down menu
$query = 'SELECT category_id, name FROM category ORDER BY name ASC';
$request = mysqli_query($dbc, $query);
while ($row = mysqli_fetch_array($request, MYSQLI_NUM)) {
$cat_id = $row[0];
echo "<option value=\"row[0]\"";
//check for stickyness:
if (isset($_POST['category']) && ($_POST['category'] == $row[0])) {
echo ' selected="selected"';
//$_POST[]
} echo ">$row[1]</option>\n";
}
?>
</select>
<? if (array_key_exists('category', $add_product_errors)) echo '<label class="error">' . $add_product_errors['category'] . '</label>'; ?>
</div>
and here is my part of my code for validation: -
// Check for a form submission:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// Check for a category:
if (!isset($_POST['category']) || !filter_var($_POST['category'], FILTER_VALIDATE_INT, array('min_range' => 1))) {
$add_product_errors['category'] = 'Please select a category!';
}
..... // other codes for validation (Eg: if (empty($add_product_errors)) { proceed to query} )
I don't know what seems to be the problem but there are no values recorded in the $_POST['category'] for me to save into MySQL and I keep getting the Please select a category error.
Thank you all for your help. :)
your problem seems to be echo "<option value=\"row[0]\"";. You want to replace it with
echo "<option value=\"$row[0]\"";
echo "<option value=\"row[0]\""; just puts a String row[0] there
Thanks, I seem to have solve the problem ..
I have seemed to have deleted the ($)row in the row[0] for it to be read properly.
just put $ on the row[0]:
echo "<option value=\"row[0]\"";
I used to solve this problem like ... In your case.
while ($row = mysqli_fetch_array($request, MYSQLI_NUM)) {
$cat_id = $row[0];
$str = null;
if (isset($_POST['category']) && ($_POST['category'] == $row[0])) {
$str = 'selected="selected"';
}
echo '<option value="'.$row[0].'" '.$str.'>'.$row[1].'</option>';
}
I have a problem with getting a value out of a select which gets it's options out of the database. I've tried looking for a solution but I can't seem te find an answer because the options aren't hard coded.
This is the option select:
<select id="selectmission" name='missionselect' onchange='showoptions()'>
<?php while($mission = $allmissions->fetch_assoc())
{ echo "<option value='".$mission['missionid']."'>".$mission['missionname']."</option>";?>
<option value="other">Other</option>
</select>
This is the php:
if(isset($_POST['btnSubmit']))
{
try
{
$t->Description = $_POST['description'];
if($_POST['missionselect'] = 'other')
{
$m->Missionname = $_POST['missionname'];
$m->CreateNewMission();
}
else
{
$t->Missionid = $_POST['missionselect'];
}
I have tried to let it echo the value it identifies by using this code:
$select = $_POST['missionselect'];
echo $select;
It showed that it always detects the option 'other', the only hardcoded option.
I hope somebody can see what I've missed!
Thanks guys,
Jana
Perhaps this change:
if($_POST['missionselect'] = 'other')
if($_POST['missionselect'] == 'other')
I have dropdown list that gets values form array based on a mySQL SELECT query. Everything is working fine except that I would like to add the option to select ALL values in the list. Here is my code...
$dataArray = array();
$result = mysql_query("SELECT id, user_name FROM apsc_customers");
while($row = mysql_fetch_assoc($result)) {
$dataArray[$row['id']] = $row['user_name'];
}
AND
if($this->customer_id == ""){
$this->arrFilteringFields[_CUSTOMER] = array("table"=>DB_PREFIX."customers", "field"=>"id", "type"=>"dropdownlist", "source"=>$dataArray, "sign"=>"like%", "width"=>"");
}
Looking forward to any replies.
Thx
Where is your dropdown list? I can't see it in your code. Do you mean html element select created by PHP and based on data from MySQL table? Then you need to use javascript code to select all options in such dropdown list. What is $this in your code? Provide more information, more code and more clarificatios.
Do you mean:
<select>
<?php
while($row = mysql_fetch_assoc($result)) {
?>
<option value="<?php echo $row['id']; ?>"><?php echo $row['user_name']; ?></option>
<?php } ?>
</select>
how do i get dropdown selected for each user.
user table
------------
id job
1 1
2 2
job table
----------
id name
1 Doctor
2 Sales
$q = $db->query("SELECT * FROM affiliate LEFT JOIN user ON user.job = affiliate.id_affiliate");
while($r = $q->fetch_array()) :
if($r['id_user'] == $_SESSION['id_user'] && $r['job'] == $r['id_affiliate']) {
echo '<option selected value="'.$r['id_affiliate'].'">'.$r['org'].'</option>';
} else {
echo '<option value="'.$r['id_affiliate'].'">'.$r['org'].'</option>';
}
endwhile;
selected="selected" or just selected should normally work. If not there is a problem with your if statement. One simple way is to echo out the content of the if statement like this:
note!! the echo should normally be done outside the select open tag, just paste the following outside the select open tag but just after your query.
while($r = $q->fetch_array()) :
echo $r['id_user'] .'=='. $_SESSION['id_user'] .'&&'. $r['job'] .'== '.$r['id_affiliate'].'<br />';
endwhile;
you can now check if the values actually match. if not then there is your problem.
How about modifying the following ...
if( ($r['id_user'] == $_SESSION['id_user']) && ($r['job'] == $r['id_affiliate']) )
Not sure if it matters, but I have selected at the end of my option.
<option value='cat' selected>
this is my code which populates a drop down menu, all working perfectly, but when editing a database record, i want the first value in the drop down to be what is currently in the database, how would i do this?
<li class="odd"><label class="field-title">Background <em>*</em>:</label> <label><select class="txtbox-middle" name="background" />
<?php
$bgResult = mysql_query("SELECT * FROM `backgrounds`");
while($bgRow = mysql_fetch_array($bgResult)){
echo '<option value="'.$bgRow['name'].'">'.$bgRow['name'].'</option>';
}
?>
</select></li>
You would set the selected="selected" attribute on the relevant <option>. Presumably there would be some sort of check in your while loop, checking against the variable that contains the current value.
You can do like:
$counter = 1;
while($bgRow = mysql_fetch_array($bgResult)){
if ($counter === 1)
{
echo '<option value="'.$bgRow['name'].'" selected="selected">'.$bgRow['name'].'</option>';
}
else
{
echo '<option value="'.$bgRow['name'].'">'.$bgRow['name'].'</option>';
}
$counter++;
}
As can be seen I have added selected="selected" for you so it will work automatically for you :)
Correct me if I'm wrong, I believe that you have another database table which holds the selected background (e.g. users table with background field), you need to do a query to get the background from the other table and add selected="selected" attribute to the option tag of the background, please check the code below (hope it helps):
<?php
$result = mysql_query("SELECT `background` FROM `users` LIMIT 1");
$myBg = mysql_fetch_array($result, MYSQL_ASSOC);
$bgResult = mysql_query("SELECT * FROM `backgrounds`");
while($bgRow = mysql_fetch_array($bgResult)){
if($myBg['background'] == $bgRow['name'])
echo '<option value="'.$bgRow['name'].'" selected="selected">'.$bgRow['name'].'</option>';
else
echo '<option value="'.$bgRow['name'].'">'.$bgRow['name'].'</option>';
}
?>