how to display all user selected "options" in a list - php

I need the user to select one, two, three or four items from a drop down list and then display them in the "ul". I have the number of the selected item displayed and I need the value to be displayed.
my code
<select name="courses" size="4" multiple>
<option>BMW</option>
<option>Mercedes</option>
<option>Audi</option>
<option>Volvo</option>
</select>
<?php
if(!isset($_POST["courses"])) {
echo 'error';
} else {
echo '<ul><li>' . implode('</li><li>', $_POST["courses"]) . '</li></ul>';
}...
do not judge strictly, I am new to php thanks in advance

Change name="courses" attribute to name="courses[]", then it will be passed to $_POST and treated as an array, not as single value string.

<select name="courses[]" size="4" multiple>
<option>BMW</option>
<option>Mercedes</option>
<option>Audi</option>
<option>Volvo</option>
Making the name attribute "courses" an array would solve the problem

Related

PHP variable in header function in one drop list with two values [duplicate]

I'd like to post two values in one drop down option and I'm not sure about the best way to approach it.
The drop down list pulls data in from an external service. This data is 'id' and 'name'.
When I select an option in the drop down and then press submit I want the 'id' and the 'name' to be posted.
My code looks like this:
<select name="data">
<option>Select a name</option>
<?php foreach($names as $name): ?>
<option value="<?php echo $name['id']);?>">
<?php echo $name['name']);?>
</option>
<?php endforeach; ?>
</select>
I've tried putting in a hidden input field, but that then doesn't render out the drop down (instead it just creates a list).
I am using both the id and name elsewhere, so I don't want to have to post just the id and then have to get the name again, hence I want to post both of them at the same time.
Any recommendations?
You cannot post two values unless both of them appear in the value attribute. You can place both in, separated by a comma or hyphen and explode() them apart in PHP:
// Place both values into the value attribute, separated by "-"
<option value="<?php echo $name['id'] . "-" . $name['name']);?>">
<?php echo $name['name']);?>
</option>
Receiving them in PHP
// split the contents of $_POST['data'] on a hyphen, returning at most two items
list($data_id, $data_name) = explode("-", $_POST['data'], 2);
echo "id: $data_id, name: $data_name";
You may add a hidden field with the name "id" and then bind an onchange event listener to the <select>. inside the onchange function, get the value of the <select> and assign it to the "id" field.
<form>
<select name="name" onchange="document.getElementById('id').value=this.value">
<!--
...
options
...
-->
</select>
<input type="hidden" name="id" id="id" />
<input type="submit" />
</form>
You could output something like:
<option value="<?php echo $name['id']."_".$name['name'];?>">
Then use preg_splitor explode to separate the two once the form data is processed.
The only way to do this is to include both pieces of information in the value for the single option. Just use code like the following, and then when you get the value back, split the string at the first underscore to separate out the values.
<select name="data">
<option>Select a name</option>
<?php foreach($names as $name): ?>
<option value="<?php echo $name['id'] . "_" . $name['name']);?>">
<?php echo $name['name']);?>
</option>
<?php endforeach; ?>
</select>
Also, you could just leave the ID alone in your form, and then look up the id again when the user submits the form.
You could make as many hidden inputs as there are options, each with the name of one option value. The input's values are the contents of the options. In the second script, you can look for the dropdown value, and then take the value of the hidden input with that name.
We can pass it with data attribute, and can access in js,
<option value="<?php echo $name['id']);?>" data-name="<?php echo $name['name']);?>">One</OPTION>
var name = $(this).find(':selected').data('name');

Select value from the dropdown in which the data are taken from database

I need to post the value from the drop down, in which the dropdown contains items which are retrieved from the database. To display the item of the table I use echo in the option. But then, I need to get that value of item selected to be updated in the database. As be seen below, I've tried the code which (surely) will not work. How is it possible to get the the value of selected item?
Your suggestion will be appreciated.
Thank you.
<select name="user" class="form-control" id="user">
<?php while ($hasil = mysqli_fetch_array($result2)){ ?>
<option name="user_name"><?php echo $hasil['user_name'] ?></option>
<option name="user_name" value="<?php echo $hasil['user_name'] ?>" hidden></option><?php }?>
</select>
<select name="user" class="form-control" id="user">
<?php while ($hasil = mysqli_fetch_array($result2)){ ?>
<option><?php echo $hasil['user_name'] ?></option>
<?php } ?>
</select>
This code solves your problem, Check at your side and let me know in case of any issue.
We need to give name only once in select tag and no need to add a extra option only for value.
You must fill your dropdown from base table after that you must check value or id from second table that stored in database. If id or value is equal you must set selected='selected' in your option element
Hope it is help you to resolve you problem
" >
" >
" >
*

HTML PHP: Why do two identical select elements produce different HTML displays?

Could someone kindly help out with this rather basic issue: In a simple HTML form (functionally all good) two select elements are included.
Code:
<select id="dept" name="dept" required>
<?php
foreach($stddept as $item0) {
echo "<option value='$item0'";
if ($_POST['dept'] == $item0) echo 'selected="selected"';
echo ">$item0</option>";
}
?>
</select>
<br><br>
<select id="lev" name="lev" required>
<?php
foreach($stdlev as $item1) {
echo "<option value='$item1'";
if ($_POST['lev'] == $item1) echo selected="selected"';
echo ">$item1</option>";
}
?>
</select>
<input type='submit' id='submituser' name='submituser' value='Submit'>`
These two drop-down boxes behave differently in that only the second one honours the 'required' attribute. The first one can be submitted blank, which is obviously not right.
They also appear differently (see image) in that the first displays the first option in the list (from MySQL DB) but the second displays a blank. I have checked everything I could to see what else could cause the difference - without any joy.
Does anyone know what I am missing here? Thank you.
You can do this using jquery/javascript by getting the id of the first selected value and then select the result for the second box based on that id.
I think the problem with the code ( as it is shown ) is that you are incorrectly echoing the option in the second dropdown - it is missing quotes. You can streamline the code somewhat like this:
<select id="dept" name="dept" required='required'>
<?php
foreach($stddept as $item0){
$selected=$_POST['dept'] == $item0 ? " selected='selected'" : "";
echo "<option value='$item0'{$selected}>";
}
?>
</select>
<br /><br />
<select id="lev" name="lev" required='required'>
<?php
foreach($stdlev as $item1){
$selected=$_POST['lev'] == $item1 ? " selected='selected'" : "";
echo "<option value='$item1'{$selected}>";
}
?>
</select>
<input type='submit' id='submituser' name='submituser' value='Submit'>

php-Not getting values while i am selecting values from drop down?

I am trying to get a values from the drop down displayed from table .
i have tried in diff ways but i don't know where i have gone wrong.
Can any one help me on this..
Below is my code.
<tr class="form-field" id="appid">
<div>
<th valign="top" scope="row" >
<label for="country"><?php _e('country', 'custom_table_example')?></label>
</th>
<td>
<select id="country" name="country" class="code" >;
<option value="">select country</option>
<?php
global $wpdb;
$coun_name = $wpdb->get_col("select country_name FROM countries") ;
//print_r($coun_name);
foreach($coun_name as $a)
{
echo '<option value="'. strtolower($a) .'" />' . "$a </option>";
}
?>
</td>
</div>
</tr>
The above code is displaying values into drop down.
now the problem is i need to get the selected values.
echo '<option value="'. strtolower($a) .'"<?php echo $item['country']==".$a."?'selected="selected"':'' ?> />' . "$a </option>";
$item is the variable where i am storing all data.
country =name attribute.
Have you set $item from $_POST? Are you using POST as your form action? Do something like this:
<form name='countryTest' method='POST' action='<?/*where your action is going to*/?>'>
<select name='country'>
<?foreach($coun_name as $c){
?><option value='<?echo$c;?>'<?if($_POST['country']==$c)echo' selected="selected"';?><?
}?>
</select>
</form>
OR! If you want to be really cool (yea ok not so cool but nerdy)! Make a function or class function to do all this for you!
class formHelper{
public function select_form($name,$options=array([0]=>'Please select'),$selected=array(),$multiple=false){//name of select, options, selected options, multiple select
if(!is_array($selected))$selected=array($selected);
$sel='<select name="'.(($multiple===true)?$name.'[]':$name).'"';
if($multiple===true)$sel.=' multiple';
$sel.='>';
foreach($options as $value=>$shown){
$sel.='<option value="'.$value.'" '.((in_array($value,$selected))?'selected="selected"':'').'>'.$shown.'</option>';
}
return$sel.='</select>';
}
}
Now to use it just do this
$coun_name=array(merge(array('Please select a country'),$coun_name));
formHelper::select_form('country',$coun_name,$_POST['country']);
EDIT
your error is you've set your value to lower but when you're comparing it's not lowered. See strtolower. What you want to do is compare both as lower as $item will be lower. I'd recommend using an integer when comparing like this:
array(
[1]=>'England',
[2]=>'Wales',
[3]=>'Scotland'
);
So that your values will be
<option value='1'>England</option>
<option value='2'>Wales</option>
<option value='3'>Scotland</option>
But ye your issue is $item['country']==$a. Needs to be $item['country']==strtolower($a). And remove the string quotes with the full stops. "england" does not equal ".England.". The reason it's "england" already is because you've already set the string to lower. Unless $item is not $_POST['county']'

Multiple Select Box in Wordpress Custom Meta Box Behaves and looks like text input

I am trying to add a select dropdown with multiple="multiple" in a custom meta box, and the select is appearing with fancy styling, is not clickable, and is not the size I am setting:
<select multiple="multiple" size="3" name="location">
<option value="">Please select</option>
<option value="0">All</option>
<?php
foreach(get_terms('town',array('get' => 'all')) as $term)
{
if (!empty($term->name))
{
$str .= "<option value='" . $term->term_id . "'";
$str .= (is_object_in_term($post->ID, "town", $term->name)) ? " selected>" : ">";
$str .= $term->name . "</option>";
}
} echo $str;?></select>
What I get is more like a text input box in appearance, although firebug shows the code is correct for a select box multiple complete with options. Any help gratefully received.
UPDATE :: ADding code of select for Bingjie's request in comments:
<select name="location" size="3" multiple="multiple">
<option value="">Please select</option>
<option value="0">All</option>
<option value="5">Akbuk</option><option value="4">Altinkum</option></select>
add style css.
<select multiple="multiple" size="3" name="location" style="height:200px;">
Hi thanks for you updates. I think the meta box is still custom meta data which is Key-value paired, so it is not allow to select multiple keys. when you retrieve post_meta, you call
get_post_meta($post_id, $key, $single);
so it is not allowed to use multiple keys.
I am not sure, but it is the only reasonable explanation since the select code appears correct.

Categories