No biggie to set default value in a select box:
<option selected> MyOptionValue </option>
And for defaulting with a value from a database table ( here I read years from a table, and if the user record field edu_end_year gets a match I display that value as selected, if no match the default text is used ) :
<option value="default"> <?php echo $default_text_year ?> </option>
<?php while($option = $years->fetch_object()){ ?>
<option <?php echo $option->year == $record->edu_end_year ? 'selected' : ''?> value='<?php echo $option->year; ?>'> <?php echo $option->year ?></option> <?php } ?>
But I am stuck when it comes to combining the two in a code-efficient way. My question is this:
How do I set table record entry a default value only when there is no hit in the entries read from the user record? The value must come from the same table.
An example of implementation:
A user can select a favorite country from a list.
When the user logs in the next time the favorised country should be shown as selected.
If no favorite country was selected, the user's home country should be shown as selected.
I am stuck. Any and all help appreciated! Thanks!
Related
I'm a beginner with PHP and have some decent issues ^^
Would like to pick the data from the field "disabled" from the database and update the selectfield with the value.
The selectfield has 2 possible options "yes" and "no" (value 0 or 1).
Now i would finally like to post the selected or unchanged value back to the database.
Here all the values including disabled which are loaded into $row by MYSQL SELECT query
$mydb->query('SELECT a.* ...
$row = $mydb->fetchRow();
Here the working query to write into the database / table (tested on other fields).
disabled is the field that should be updated/overwritten:
$sql = "REPLACE INTO `api_tokens` (`id`, `disabled`) VALUES (
".$id.",
".(empty($_POST['disabled']) ? 0 : 1).")";
Here the form which should have 2 selectable options, one of them shoudl be the value loaded from the database and the other value should be the opposite.
Unclear value/ares is marked with ????
<div class="row">
<label for="disabled">Deactivate</label>
<select name="disabled" id="disabled">
<option selected="<?=$row['disabled']?>"></option>
<option value="????">No</option>
<option value="????">Yes</option>
</select>
Try this. The key is to check the value for each condition. If the saved choice was 'no' (0) then set the select attribute. Do the same for 'yes' (1).
<div class="row">
<label for="disabled">Deactivate</label>
<select name="disabled" id="disabled">
<option value="0"<?php echo ($row['disabled'] == 0?' selected':''); ?>>No</option>
<option value="1"<?php echo ($row['disabled'] == 1?' selected':''); ?>>Yes</option>
</select>
UPDATE
I see from your comment that you'd like additional clarification. Here's what happens:
Each <option> element will specify a value attribute as one possible choice of the <select> element it belongs to. When a choice is made, the <option> element's selected attribute is set. (Technically, there is the multiple attribute on the <select> element, but let's not get into that now.)
When your user submits the form, their choices (option values) are made persistent by storing them in a database. In this example, this will be either 1 for yes or 0 for no.
If you want to present the user with the last state their form was in, you have to read their choices back from the database. You now have to set the select attribute on the <option> element they chose before. On their screen, their chosen option is now highlighted in the <select> list.
You can find the choice they submitted in $row['disabled'], where the 'disabled' key is equal to the name attribute of the <select> element. To set the select attribute on the right (previously chosen) <option> element, you check $row['disabled'] against each <option> element's value. Are they equal? Then this was their last chosen option, so now highlight it and set the selected attribute on this specific <option> element.
I have a form with a select box where the user can select his gender. I succeed in getting the selected value and displaying this. But I want to use another term for the selected value.
For example, there are 2 values in the select box, named "man" and "vrouw".
<select name="geslacht" class="formulier-input">
<option value="" disabled selected hidden>Kies uw geslacht</option>
<option value="man">Man</option>
<option value="vrouw">Vrouw</option>
</select>
But I want to display Dhr. as the user selects for the value "man" and Mevr. as the user selects for the value "vrouw".
I guess I can do this in an if statement with:
if($geslacht === "the value"){ }
I don't know how I can finish this and I don't know what to put in the place of "the value".
How can I do this?
I need to store all the items selected from a dropdown box inside array. I have only one form now with the select dropdown list. So each time I select an item from the list and submit the form it overwrites the previous item.
How do I make it work like each time it submits the id will be stored so that I can display all the items selected?
//this is the select dropdown for addon item
<select name="addon">
<?php
mysql_select_db($database_bumi_conn, $bumi_conn);
$query="SELECT * FROM tbl_addons WHERE status=1";
$result=mysql_query($query)or die(mysql_error());
while($row=mysql_fetch_array($result))
{
$a_id=$row['addOns_id'];
$a=$row['addOns'];
?>
<option value="<?php echo $a_id?>"><?php echo $a;?></option>
<?php
}
?>
</select>
//And this is how I store the id
$addon_id=$_POST['addon'];
//edited with session
$_SESSION['option']=array();
$_SESSION['option'][$addon_id]=array('qty'=>$qty,'date_1'=>$date_1,'date_2'=>$date_2);
print_r($_SESSION['option']);
foreach($_SESSION['option'] as $option=>$value)
{
echo $option.'=>';
foreach($value as $val)
{
echo $val;
}
}
You could use SESSION' variables to store all the ID :
session_start();
// Rest of your code here
// $addon_id=$_POST['addon']; Becomes :
if (!in_array($_POST['addon'], $_SESSION['addons']))
$_SESSION['addons'][] = $_POST['addon'];
Edit: Not sure about your edit with sessions. You're resetting $_SESSION['option'] everytime, losing previous addon_id values
Ignoring the fact that you're using a deprecated, inherently un-secure and unmaintained extension, you need to use the multiple attribute on your <select> element and let PHP know that it will be receiving an array of values by using the [] suffix on the element name. To summarise...
<select name="addon[]" multiple>
<?php foreach($collection as $val => $label) : ?>
<option value="<?= htmlspecialchars($val) ?>"><?= htmlspecialchars($label) ?></option>
<?php endforeach ?>
</select>
The PHP variable $_POST['addon'] will then contain an array of selected values (when the form is posted of course).
I'm bug-proofing a form that allows data editing for book entries in a database. Everything is working except for the drop-down box. The drop-down box automatically populates itself with every unique entry in a specific field in the database table, and that part works perfectly. However, when people click to edit a book all the fields are populated with that books information, and I wanted the drop-down box to default to the correct value for that book. My solution was to check each value as it populates the drop-down box against the actual book's value for that field and if they match, make it the "selected" value.
It is not working. The box is still populating fine, but it is not defaulting. Here is the code for the drop-down box.
<span style="margin-left:10px;">
Publication Type:
<select name="publicationType" >
<option value=""></option>
<option value="">-------------------------</option>
<?php
$lPub = '';
if(array_key_exists('publicationType',$_REQUEST)) $lPub = $_REQUEST['publicationType'];
$lPubArr = $datasetManager->getPublicationType();
foreach($lPubArr as $pubStr){
if($pubStr == $bookArr['publicationType']){
echo '<option '.($lPub==$pubStr?'selected="selected"':'').'>'.$pubStr.'</option>'."\n";
}
else{
echo '<option '.($lPub==$pubStr?'':'').'>'.$pubStr.'</option>'."\n";
}
}
?>
</select>
</span>
I can provide what all the variables are if needed. I don't see what I'm doing wrong, but maybe someone will be able to catch an obvious mistake.
Thank you,
Kai
Not sure this will help but try this:
<?php
$lPub = '';
if( array_key_exists('publicationType',$_REQUEST) )
$lPub = $_REQUEST['publicationType'];
$lPubArr = $datasetManager->getPublicationType();
foreach($lPubArr as $pubStr){
echo '<option '.($lPub==$pubStr?'selected="selected"':'').'>'.$pubStr.'</option>'."\n";
}
I removed this condition:
f($pubStr == $bookArr['publicationType'])
since I didn't get what the $bookArr['publicationType'] is used for, perhaps you left it there by mistake
I basically need to create a value by default for a drop down menu on a PHP page that will use the value previously selected and stored in the database.
For example, lets just say I have the value '3' stored in a database column. I want to use this number as the default value for a drop down menu where the <option value = "3">Good</option>. Is there a simple solution to this problem?
Or do i literally need to loop through the values until it makes a value?
Thanks.
I usually do this
<?php
$sel = 'selected="selected"';
$current_whatever = 5;
?>
<option name="whatever">
<?php foreach($list as $listItem): ?>
<option value="<?=$listItem->id?>" <?=($listItem->id == $current_whatever)?$sel:''?>><?=$listItem->name?></option>
<?php endforeach; ?>
</option>
I'm using an inline if statement to check each one :) Looks reasonably tidy.
Assuming you're using database objects, you get the idea if you're not though :)
<?php foreach($options as $key=>$option){?>
<option value='<?=$key?>' <? echo $key==$selected?"SELECTED":"";?> ><?=$option?></option>
<? }?>