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?
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 put a first option in my select element just to tell the user what they are selecting, I think it's advisable to put a null first option, but I want to put an error message that the user didnt select anything if he is just selecting the first option which is the null option here's my code
<select class = "strand" name = "strand">
<option >=====Strand=====</option>
<option value ="GAS">GAS</option>
<option value ="ICT">ICT</option>
<option value ="STEM">STEM</option>
<option value ="HUMSS">HUMMSS</option>
<option value ="ABM">ABM</option>
</select>
In my controller:
public function putbillpage()
{
$proname = productmodel::select('itemname')->get();
return view('putbillpage', compact('proname'));
}
In My View:(It lists the product name)
<td>
<select name="proname[]" class="form-control proname">
<option value="" selected="true" disabled="true">Select Product</option>
#foreach($proname as $v)
<option value="{{$v->itemname}}" >{{$v->itemname}}</option>
#endforeach
</select>
</td>
Whenever i click the plus on the right end a new row is added below.when is press cross row is removed.
Now my question is if i select a option in first row.then in the second row that option must not be displayed..
in the above image in first row i have selected cricket bat and now in the second row only soaps must be displayed.And when i remove first row the cricket bat also must be displayed in the options.
Any Help or suggestions are most Welcome.Thanks.
You should set a Flag
for example Soap is already selected in the next row choice you should remove the Soap option or alert the user that this option is already selected
Just set a variable on your js or set a cookie for selected options
Let me know if this helps you Thank you
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!
Let's say, that i have users stored in a database, identified by an id (an integer primary key). All the users have a multiple choice attribute, for example privileges on a site (Admin, Member, Administrator etc...). I would like to create a php admin page, where i can select for each user his/her privileges, and all users at one page (or at least more users at one page):
Tom [_privileges_v_]
Ed [_privileges_v_]
Lisa [_privileges_v_]
...
How would it efficient to do?
if i print the dropdowns with the name attribute set as the id of the record, then how could i access them through php?
<select name="12323">
<option value="admin">admin</option>
<option value="user">user</option>
...
</select>
<select name="4323">
<option value="admin">admin</option>
<option value="user">user</option>
...
</select>
Then after i post this form, my $_POST array will contain a $_POST["12323"] and a $_POST["4323"] member.
How could i make something like this, so i could iterate through the $_POST values?
Thanks for any kind of help!
You can output a select box like this
<select name="user_4323">
<option value="admin">admin</option>
<option value="user">user</option>
...
</select>
<input name="otherinp" value=""/>
in case your form has another input.... then in php code you can do
foreach($_POST AS $name=>$value) {
if (strpos($name, "user_")===0){
$userid = str_replace("user_","",$name);
//Do st with userid
}
}
//Work with orther input $_POST['']
Either fetch all the users again and loop through their IDs, then check if there is a post value linked to it, or loop through all the post values.
foreach($_POST AS $name=>$value) {
echo "Selection box with name {$name} has value {$value}";
}