Showing the previously selected value in a multi-select option - php

Thanks for reading.
I am using the following multiselect option for a users 'interests'. They can pick one or many. The interests are then stored in the database as a string, which is created from the array that the code below supplies. The thing is, when the user returns to modify his/her interests I want the box to display whats already in the database. The value for this is $user->interests (which is a string). I can't see how I can perform this. You can see I added $user->interest below to the value of the . Obviously this doesn't work. Any ideas...?
<select name="interests[]" value="<?php echo $user->interests;?>" style="width:390px;" data-placeholder="Select your specialist interests" class="chosen-select" multiple tabindex="6">
<option value=""></option>
<option value="Acute critical care">Acute critical care</option>
<option value="Allergic diseases">Allergic diseases</option>
<option value="Asthma">Asthma</option>
<option value="Bronchiectasis">Bronchiectasis</option>
<option value="Cardiology">Cardiology</option>
</select>

The select element doesn't have a value attribute.
You need to add a selected attribute to each of the <option>s that should be selected by default.
You should get the list of values in an array and then check to see if each option is in that array as you output them.

Try this variant.
<select name="interests[]" style="width:390px;" data-placeholder="Select your specialist interests" class="chosen-select" multiple tabindex="6">
<option value=""></option>
<option value="Acute critical care"
<?php if(strpos( $user->interests,"Acute critical care")){echo 'selected="selected"'} ?> >
Acute critical care
</option>
//Similarly for other options.
//....
</select>

Related

Select input of form doesn't render on php email

I have a form that includes some select inputs like this
<select class="motive" name="motive">
<option value="" disabled selected>Select a Number</option>
<option value="">One</option>
<option value="">Two</option>
<option value="">Three</option>
</select>
On my php form side I have this piece of code
$motive = $_POST['motive'];
In my received email I'm not getting the motive displayed, is showing as a blank space. Can someone tell me why?
thanks!
For each of your <option> tags, you must have a value attribute set.
<select class="motive" name="motive">
<option value="" disabled selected>Select a Number</option>
<option value="1">One</option>
<option value="2">Two</option>
<option value="2">Three</option>
</select>
If user has option One selected and submits the form, then in PHP you
$selected_motive = $_POST['motive']; // equals "1"
It's probably because <select> inputs are not allowed in emails. Most email clients will just remove them.

HTML select Tag Title using Codeigniter 3

I'm using Codeigniter 3.
The countries select box is an optional field on a form:
<select name="countries">
<option>Choose a Country</option>
<option value="en">England</option>
<option value="fr">France</option>
<option value="de">Germany</option>
</select>
I have the following line for the countries field validation:
$this->form_validation->set_rules('year', 'Year', 'trim|integer');
When I submit the form with the 1st option (Choose a Country), even without a value defined the validator sees something like this:
echo '<pre>';
print_r($this->input->post('year'));
echo '</pre>';
>> Choose a Country
.. when I am expecting something like false or null.
How can I make CI ignore the value when there isn't one selected?
Thanks in advance!
First of all, you paste the wrong line, I think it should be countries instead of year.
You can make it by these ways:
Give the default selection a value, that can be caught. And handle when the value equals to none
<select name="countries">
<option value="none">Choose a Country</option>
<option value="en">England</option>
<option value="fr">France</option>
<option value="de">Germany</option>
</select>
Change your validation rule that only allows options in the list. If the value returns isn't in the list, it would make error.
$this->form_validation->set_rules('countries', 'Countries', 'required|trim|in_list[en|fr|de]');

Form with disabled select option selected as default not sending anything to validate

I have a form like so:
<select name="employment">
<option disabled="" selected="">-- What is your employment status --</option>
<option value="employed">Employed</option>
<option value="self_employed">Self Employed</option>
</select>
In PHP I am running through each of the $_POST variables and then checking if they have a value. If not I then add that field to the array for an error message.
The issue is that if I leave the default 'disabled' message selected nothing is passed through a post value so theres nothing for me to validate.
If I print_r my $_POST variable then it contains no 'employment' field unless I select an option.
How can I solve this?
<select name="employment">
<option disabled selected>-- What is your employment status --</option>
<option value="employed">Employed</option>
<option value="self_employed">Self Employed</option>
</select>
disabled attribute
The disabled attribute is a boolean attribute.
When present, it specifies that an option should be disabled
A disabled option is unusable and un-clickable.
Syntax:
<select disabled>
Not
<select disabled="">
In case of XHTML, syntax differs like
<select disabled="disabled">
hidden attribute
So, If you want to validate it. Use hidden in option.
<select name="employment">
<option hidden>-- What is your employment status --</option>
<option value="employed">Employed</option>
<option value="self_employed">Self Employed</option>
</select>
When, nothing got selected, then it will output as -- What is your employment status --
<?php
echo $Employment=$_POST['employment'];
?>
Output: -- What is your employment status --
So, Now you can easily use your validation in dropdown
For more info, click disabled attribute - W3 Schools
Try this:
<select name="employment">
<option disabled="" selected="" value ="">-- What is your employment status --</option>
<option value="employed">Employed</option>
<option value="self_employed">Self Employed</option>
</select>
PHP:
if(empty($_POST['employment'])){
return false;
}else {
// I got value;
}

Collect and echo submission via multiple <select> tags in submission form?

My basic goal is to "capture" three values from three menus (Year, month, day), and print them on the next page pointed to by the submission form. I've been trying all day to find a way to do such - No luck whatsoever, I can only manage to print one of the three values.
An abbreviated version of my select menus:
<form action="processform.php" method="POST">
<select name="birthdate_year[ ]">
<option value="2003">2003</option>
<option value="2004">2004</option>
<option value="2005">2005</option>
</select>
<select name="birthdate_month[ ]">
<option value="01">January</option>
<option value="02">Feburary</option>
<option value="03">March</option>
</select>
<select name="birthdate_day[ ]">
<option value="01">01</option>
<option value="02">02</option>
<option value="03">03</option>
</select>
<input type="submit" value="Submit">
</form>
...And the following shows how I've managed to only get one value to become an array:
foreach( $_POST['birthdate_year'] as $year ) /*Neither &&/|| work, and I cannot "add" to it or add another "foreach" */
{
print $year;
}
I've been scouring the internet for a way to do such - The only working method is "foreach", which can only seemingly convert one value to an array (It won't accept "AND" statements, or for that matter, another "foreach").
All I want is to have each entered value become it's own array - All of whom need to be accessed multiple times to complete the program's function (Compute if whether the user is above/below a cutoff age & storing their birthdate for later examination when they become old enough)
Programs for storing their age and determining their age "group" have already been written - Now I just need each value to become it's own array.
Solution found
For some reason it wouldn't accept multiple "foreach" statements if they weren't descending in some weird way - So now that they're doing so, it works.
What you want is:
2003
2004
2005
<select name="birthdate[month]">
<option value="01">January</option>
<option value="02">Feburary</option>
<option value="03">March</option>
</select>
<select name="birthdate[day]">
<option value="01">01</option>
<option value="02">02</option>
<option value="03">03</option>
</select>
<input type="submit" value="Submit">
</form>
Notice that the indexes have no quotes. As you do it, you have to check the following variables:
$_POST['birthdate_month'][0]
$_POST['birthdate_day'][0]
$_POST['birthdate_year'][0]
which is not so nice.

Working with "multiple" select

In my profile advanced search i have an mutiple select type..
<form method="POST">
<select size="6" name="Cities" multiple="multiple" id="Cities">
<option value="0">All</option>
<option value="1">City1</option>
<option value="2">city2</option>
<option value="3">city3</option>
<option value="4">city4</option>
<option value="5">city5</option>
</select>
</form>
How do I use it, I mean how should i call what they have chosed...As you can choose one or more options (by holding in control, or just holding in mouseclick and point to other options).
Give it the name Cities[] instead of Cities, and you'll have an array in $_POST['Cities'] on submit.
google helps
http://www.onlinetools.org/tricks/using_multiple_select.php

Categories