PHP get dropdownlist select option value - php

In my dropdownlist I have two different values for each option. How can I retrieve both? Let me illustrate what I mean.
<select name="my_ddl">
<option value="<?php $value_Id ?>"><?php $value_text ?></option>
<option value="<?php $value_Id ?>"><?php $value_text ?></option>
</select>
When the form is posted, I want to be able to get both the $value_id and $value_text of the selected option. How can I do this?
$_POST['my_ddl'] only gets one value not both.
In asp.net I could do this simply by referring to my_ddl.Value and my_ddl.Text.
Thank you!

Strictly, this is not possible.
What you could do is use a delimiter in your value attribute:
<select name="my_ddl">
<option value="<?php echo $value_Id ?>|<?php echo $value_text ?>"><?php echo $value_text ?>
</option>
</select>
And...
<?php
list($id, $text) = explode('|', $_POST['my_ddl']);
//...
?>

Another strange way of doing it is:
<select name="my_ddl">
<option value="<?php echo $value_Id ?>[<?php echo $value_text ?>]">
<?php echo $value_text ?>
</option>
</select>
Then when you process it you can do this or maybe even something more simple:
foreach ($_POST['my_dd1'] as $value_Id => $value_text) {
$value_Id = $value_Id;
$value_text = $value_text;
}
Because php treats the [] as meaning the string is an array and so you instantly have an associative array. I agree though that if you put it there in the first place you ought to be able to just look it up again in the code rather than rely on this.

If you're using PHP to populate the text for the <option> then you can probably just look the value up on the server. Perhaps you just need to use the $value_id to look up the text in a database table?
If not, you could include a hidden field on your form and use JavaScript to update that hidden field with the text every time a new value is selected.

You cannot get value_text from POST data. One solution is to populate the hidden field after choosing the option via JavaScript.

Related

Is there a way to retrieve a value that has already been set on a dropdown select option?

I have a create.php which creates a user, and edit.php for editing information of the student.
I'm a beginner in PHP so if this might be a question that has a very easy method, please do tell.
I've been searching for retrieving a value that has already been set on a dropdown, but the only things I've found are:
a.) They only print the selected value, not retrieve the already-set option.
sample code:
<p>Schedule</p>
<select value="<?= $data->schedule; ?>" name = "schedule">
<?php
$schedule = array ('Morning', 'Afternoon');
foreach ($schedule as $sched) { ?>
<option value="<?php echo $sched; ?>"><?php echo $sched; ?></option>
</select>
<?php } ?>
b.) I have no knowledge of javascript, I tried it, but as I have no knowledge of it, I can't seem to make it work.
sample codes I tried:
<p>Schedule</p>
<select id = "my_select" name = "schedule">
<option id = "o1" id="id1">Morning</option>
<option id = "o2" id="id2">Afternoon</option>
</select>
<script type="text/javascript">
$("#my_select").change(function() {
var id = $(this).find('option:selected').attr('id');
});
</script>
I also tried
document.getElementById('schedule').selectedOptions[0].text
but still does not output "afternoon"
This is my last resort as I have no other resource to find.
I added a picture of what I wanted to do since my question might be a little confusing.
Selects use selected attribute not the value to define the selected state.
So in your code check the value and then apply selected.
<p>Schedule</p>
<select name="schedule">
<?php foreach (['Morning', 'Afternoon'] as $sched): ?>
<option value="<?= $sched ?>"<?= ($data->schedule == $sched ? ' selected' : null) ?>><?= $sched ?></option>
<?php endforeach ?>
</select>

How to get id of selected option in dropdown?

i want to get the id of selected option in dropdown.
I have a dropdow that displays company names from database. Now I want get the id of that company. How do i do that?
<select required name="org-list" id="org-list" class="form-control">
<option value="">Select</option>
<?php foreach($org as $value) { ?>
<option id="org" value="<?php echo $value['org_name'];?>"><?php echo $value['org_name']; ?></option>
<?php } ?>
</select>
The Model
public function get_organisation()
{
$q = $this->db->select('*')
->from('company')
->get();
return $q->result_array();
}
And in my controller i want the id of selected option from database.
$data = $this->key_m->array_from_post(array('id','org-list','keys'));
$data['keys'] = $license;
var_dump($data);
You should put the organization id in option tag's value. like this
<option value="<?php echo $value['org_id'];?>"><?php echo $value['org_name']; ?></option>
No need to provide id attribute to each option tag. When you will submit the form value of "org-list" will be selected organization id.
This is the ajax way
$.post(url,{ id : "id value from drop down here" },function(data){
//This is the call back for success
})
For that one, you need jquery. The is the easiest way to use. Url is where you are posting back to server.
In your server side catch like this
$this->input->post('id')
Because you used "id" in the ajax request. So you have to retrieve it using the field name 'id' . Hope you get it.
There are a couple things which you "should" / "could" change. At first I noticed that your ID attribute is "org", Id's are supposed to be unique and yours is not. Though, back to the actual question. You should change your code as follow:
<select required name="org-list" id="org-list" class="form-control">
<option value="">Select</option>
<?php foreach($org as $value): ?>
<option id="<?php echo $value['org_id'];?>" value="<?php echo $value['org_id'];?>"><?php echo $value['org_name']; ?></option>
<?php endforeach; ?>
</select>
Now in your controller you grab the selected ID like: $this->input->post('org-list').
You have to provide value for each id attr. for corresponding <option>
<select required name="org-list" id="org-list" class="form-control">
<option value="">Select</option>
<?php foreach($org as $value): ?>
<option id="<?php echo $value['org_id'];?>" value="<?php echo $value['org_name'];?>"><?php echo $value['org_name']; ?></option>
<?php endforech; ?>
</select>

select dropdown value based on session variable

i have a php form which submits data to another php for validation. if any fields are left blank the validator php sends a message back to original php form along with all the pre filled variables using session variables so that user doesn't have to fill them again.
i can use these session variables to fill in text boxes again like this
value="<?php echo $_SESSION['fname'] ?>"
but how to go about populating drop downs, radio buttons and check boxes?
thanks
For a select, checkbox or radio box you would need to check whether the values match. Something like:
<select name="fname">
<option value="1" <?php if($_SESSION['fname'] == 1) echo 'selected'; ?>>1</option>
<option value="2" <?php if($_SESSION['fname'] == 2) echo 'selected'; ?>>2</option>
</select>
It's easier if you are iterating through the values for the option field:
<select name="fname">
<?php foreach($array AS $key => $value) { ?>
<option value="<?php echo $value; ?>" <?php if($_SESSION['fname'] == $value) echo 'selected'; ?>><?php echo $value; ?></option>
<?php } ?>
</select>
A <select> element's selected <option> is not determined by it's value= attribute. In order to mark an option selected, the <option> element must have the selected attribute set on it, as follows:
<select>
<option>1</option>
<option selected>2</option>
<option>3</option>
</select>
In order to do this programmatically, you need to iterate through the options, and manually test for the correct option, and add selected to that.
... Noticed that your double quote at the end of the statement was misplaced, it should be:
<script>
$("#fname").val("<?php echo $_SESSION['fname'] ?>");
</script>
There is a far simpler method using JQuery. You can set Selects with .val() so make the most of it.
<script type="text/javascript">
$("#fname").val("<?php echo $_SESSION['fname']; ?>");
</script>
Don't forget to set the id of the select as well as the name to fname.

Get the Selected value from the Drop down box in PHP

I am populating a Drop Down Box using the following code.
<select id="select_catalog">
<?php
$array_all_catalogs = $db->get_all_catalogs();
foreach($array_all_catalogs as $array_catalog){?>
<option value="<?= $array_catalog['catalog_key'] ?>"><?= array_catalog['catalog_name'] ?></option>
Now how can I get the selected option value using PHP (I have the code to get the selected item using Javascript and jQuery) because I want the selected value to perform a query in database.
Any help will be much appreciated. Thank you very much...
You need to set a name on the <select> tag like so:
<select name="select_catalog" id="select_catalog">
You can get it in php with this:
$_POST['select_catalog'];
Couldn't you just pass the a name attribute and wrap it in a form?
<form id="form" action="do_stuff.php" method="post">
<select id="select_catalog" name="select_catalog_query">
<?php <<<INSERT THE SELECT OPTION LOOP>>> ?>
</select>
</form>
And then look for $_POST['select_catalog_query'] ?
You have to give a name attribute on your <select /> element, and then use it from the $_POST or $_GET (depending on how you transmit data) arrays in PHP. Be sure to sanitize user input, though.
Posting it from my project.
<select name="parent" id="parent"><option value="0">None</option>
<?php
$select="select=selected";
$allparent=mysql_query("select * from tbl_page_content where parent='0'");
while($parent=mysql_fetch_array($allparent))
{?>
<option value="<?= $parent['id']; ?>" <?php if( $pageDetail['parent']==$parent['id'] ) { echo($select); }?>><?= $parent['name']; ?></option>
<?php
}
?></select>

html php and select box simplification

Is there a better way to structure this, using html and php?
If I had a billion values, this would be a bad way to set them up, not to mention time consuming as well?
I am a newb, and I am pretty sure there is a better way, however, my way of doing it seems to be the long way, as in long division, I am pretty sure there are easier methods of setting this up, and that's what I am looking for, or asking?
<select name="dimes" >
<option value=" ">--Select Dimes---</option>
<option value=".10">10c</option>
<option value=".20">20c</option>
<option value=".30">30c</option>
<option value=".40">40c</option>
<option value=".50">50c</option>
<option value=".60">60c</option>
<option value=".70">70c</option>
<option value=".80">80c</option>
<option value=".90">90c</option>
<option value="1.00">$1.00</option>
</select>
Thank you for not flaming the Newb, I am still learning.
Do you mean for generating the list?
Something similar to this should suffice:
<?php for($i=0.1;$i<=1;$i+=0.1): ?>
<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
<?php endfor; ?>
If you prefer to use mixed PHP & HTML, you can use such construction:
<select name="dimes" >
<option value=" ">--Select Dimes---</option>
<?php foreach($dates as $key=>$value): ?>
<option value="<?php echo $key; ?>">
<?php echo $value; ?>
</option>
<?php endforeach; ?>
</select>
Note, that you have to define and fill $dates array before using it in foreach statement. You can fill $dates with any data your want. In this example array has to be something like: array('0.1'=>'0.1', '0.2'=>'0.2');
But also your can go futher and use result of MySQL queries to fill array with keys and values. See foreach for more details.

Categories