HTML option selects first default value instead of choosen value - php

Whenever I Select any other options in my drop down I am still getting the first value which is '?' instead of any of the other ones I picked.
<select name="role" class="form-control">
<option value="?" label ="Please Select a Role" id="role">Select a Role</option>
<?PHP foreach($roles as $role){ ?>
<option value= "<?=$role['id']?>" label=" <?=$role['role']?>"><?=$role['role']?>
</option>
<?PHP } ?>
</select>
What am I missing?

I made a mistake
The reason I was not getting getting any other values was because I was getting the ID of the first option so it would retrieve the same value each time
I solved it by moving the value of Id to the first select tag

My best guess is that you are pulling the value off whatever DOM element has an id of role. In this case, you've defined an option that always has the id role.

Related

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
" >
" >
" >
*

How to display option values in ng-bind?

I want to display selected name from dropdown. i can see their names
like abc, xyz in dropdown but when i choose particular to display on
form. its candidate_id is printing on Form .
i want to print that name
only not id of name. i put candidate_id in value because i want to
store id in db after submitting form.
<select ng-model="empInfo.candidate_name" class="span2">
<option ng-repeat="x in names4" value="{{x.candidate_id}}">
{{x.candidate_name}}
</option>
</select>
If I understand it correctly, for display purpose you want the name and id for DB purposes.
Change in HTML markup :
<select ng-model="empInfo"
ng-options="emp.candidate_name for emp in names4"></select>
<div>id is : {{empInfo.candidate_id}}</div>
<div>name is : {{empInfo.candidate_name}}</div>
https://plnkr.co/edit/cOKY3GJIOsWWEqurmlEr?p=preview
Change the value attribute of option tag to x.candidate_name.
Change your valueto :
<select ng-model="empInfo.candidate_name" class="span2">
<option ng-repeat="x in names4" value="{{x.candidate_name}}">{{x.candidate_name}}</option>
</select>
<select ng-model="empInfo.candidate_name" class="span2">
<option ng-repeat="x in names4" value="{{x}}">{{x.candidate_name}}</option>
</select>{{x}} {{x.candidate_id}}{{x.candidate_name}}
While send just take id

using a variable in $_POST in php?

i have a page in which i am using a dropdown menu. i have passed a variable in the name field of the select tag whose value comes from an array.
$query1="Select SSAP from project where projname='$project'";
$result=mysql_query($query1);
while($row=mysql_fetch_assoc($result))
{
$ssap = $row['SSAP'];
$query2="Select * from student where SSAP='$ssap'";
$res=mysql_query($query2);
$row1=mysql_fetch_assoc($res);
$name=$row1['name'];
echo $name; ?> <select name="<?php echo $name;?>">
<option value="A"> Exceptional </option>
<option value="B"> Highly Effective </option>
<option value="C"> Effective </option>
<option value="D"> Good </option>
<option value="E"> Not Satisfactory </option>
</select> <br> <?php
}
and i need to retrive the value of each select tag created in another variable on the action page.
$grade=$_POST[$name];
echo $grade;
the first code snippet works fine but i am unable to fetch the value in the second snippet.
When the form containing the <select> with the dynamic name is submitted, it is a different request, so $name hasn't been set.
When your form is submitted, the <select> names are getting evaluated like <select name="Raj">, <select name="Kiran">, ..........<select name="Sameer"> etc depending on the values of $name=$row1['name']; from your query on 'student' table. Also you should pay consideration to the fact that your query $query2="Select * from student where SSAP='$ssap'"; might return more than one rows, it seems you are assuming only one row will be returned.
Also note $_POST is an associative array that accept parameters as
KEY=>VALUE pairs from submission of HTML. In $_POST key is name of
element in input form value is value of that element in input form
So you can not use a variable as the KEY in your SUPER GLOBAL $POST like this $_POST[$name]; when $name is undefined on your action script. I would suggest you use an named array in your multiple <Select>. Do a var_dump($_POST) on your action script. That may reveal most of the stuffs for you.

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]');

How can I retain a option from a select field?

I have a <select>:
<select id="countries">
<option value="1">USA</option>
<option value="2">Spain</option>
</select>
The user selects an option, then he press a send button to make a query, thru PHP. The result of the query appears in the same page, so the page reloads.
How do i retain the selected option? when the page reloads??
I mean, if they select spain, how can I see spain again when the page reloads?
You need to first give your select dropdown a name.
For example:
<select id="countries" name="countries">
Then you will have access to the value in your PHP when the form is submitted. The value can be retrieved like this in PHP (after submit):
$countries = $_POST['countries'];
Then you can do something like #JohnConde did by setting the selected attribute with PHP.
Here's a very basic way to do it:
<option value="1"<?php if (1 === (int) $_POST['countries']) echo ' selected="selected"'; ?>>USA</option>
<option value="2"<?php if (2 === (int) $_POST['countries']) echo ' selected="selected"'; ?>>Spain</option>

Categories