get the text of the selected option using php
This is not something that can be done through PHP alone. The PHP script can only "see" the information which is posted (the value
for the selected
option that is posted). You can use javascript to alter a hidden input
field with the text contents of a selected option
, and this will be included in the $_POST
array:
<form action="test.php" method="POST">
<select id="test" onchange="document.getElementById('text_content').value=this.options[this.selectedIndex].text">
<option value="1">Test One</option>
<option value="2">Test Two</option>
</select>
<input type="hidden" name="test_text" id="text_content" value="" />
</form>
This will make the $_POST['test_text']
available with the selected index (but you should also force the onchange()
function when the page loads so that it will be populated even if the user leaves the select field at the default value. Get selected text from dropdown in PHP
You can use a hidden field, and with JavaScript and jQuery you can set the value to this field, when the selected value of your dropdown changes.
<select id="itemType_id" name="cscf[country]" class="input-xlarge">
<option value="malaysia@email.com">Malaysia</option>
<option value="indonesia@email.com">Indonesia</option>
</select>
<input type="hidden" name="country" id="country_hidden">
<script>
$(document).ready(function() {
$("#itemType_id").change(function(){
$("#country_hidden").val(("#itemType_id").find(":selected").text());
});
});
</script>
Then when your page is submitted, you can get the name of the country by using$_POST["country"]
Getting value and text of Select Option with PHP
There is no way to access both the selected value and the name through something like the $_POST
array.
However, what I would generally do in a situation like this is create a table in the database of all possible organizations with their names and codes. In PHP, I would generate the list of select options using SELECT *
on that database when generating the HTML for the original page. Then, when the data is submitted you just insert the organization code into the second table that you're dealing with. When you need to know the full organization code and name associated with the record in that table, you use a JOIN
statement.
For example, say the original database that you're dealing with now represents users and each user has an organization. When someone submits the form, you insert a new user record into that table and insert the organization code into an organization_id field in that same users table. Then, when you want to know the organization code and name for a given user, you do SELECT * FROM users LEFT JOIN organizations ON user.organization_id = organization.id WHERE user.id = blah
. This will return the user record along with their organization code and name.
How to get value of option from html select tag using php
you want something like this
<?php
if (isset($_POST['category']))
{
$category = $_POST['category'];
echo "$category";
}
?>
<form method="post" action="" name="form">
<select name="category">
<option value="1">Adventure</option>
<option value="2">Drama</option>
</select>
<input name="submit" type="submit">
</form>
How I get the text instead value from a select option in CodeIgniter
Try this don't assign the value to option tag:
<select name="pos" id="pos_select" class="form_input">
<option>about_history_title</option>
<option>about_history</option>
</select>
It will return text of option. how to Get selected option value within PHP page reload
You can do with easily with a session within a form. Just do something at the top like:
<?php
if (isset($_GET['form'])) {
$_SESSION['eName'] = $_GET['event_name'];
}
?>
<div class="form-group row">
<label for="event_name" class="col-sm-2 col-md-1 col-form-label">Event Name</label>
<div class="col-sm-4 col-md-4">
<form method="GET" action="">
<select class="form-control event" name='event_name' id='event_name'>
<option value="Option_1">Option 1</option>
<option value="Option_2">Option 2</option>
<option value="Option_3">Option 3</option>
<option value="Option_4">Option 4</option>
<option value="Option_5">Option 5</option>
</select>
<input type="submit" name="form">
</form>
</div>
</div>
<h4><?php if(isset($_SESSION['eName'])) { echo $_SESSION['eName']; }?></h4>
Using $_POST to get select option value from HTML
Use this way:
$selectOption = $_POST['taskOption'];
But it is always better to give values to your <option>
tags.<select name="taskOption">
<option value="1">First</option>
<option value="2">Second</option>
<option value="3">Third</option>
</select>
Related Topics
Speeding Up Large Numbers of MySQL Updates and Inserts
Error with .Htaccess and Mod_Rewrite
MySQL Insert ....On Duplicate Update - Adds One to the Autoincrement
Disable Doctrine 2 Lazy Loading When Using Jms Serializer
Phpmyadmin Failed to Set Session Cookie. Maybe You Are Using Http Instead of Https
Setting Up PHPmailer with Office365 Smtp
PHP - Regex - How to Extract a Number with Decimal (Dot and Comma) from a String (E.G. 1,120.01)
Convert Multidimensional Objects to Array
Mail Not Sending with PHPmailer Over Ssl Using Smtp
.Htaccess Deny Access to Specific Files? More Than One
How to Resize and Convert an Uploaded Image to a Png Using Gd
What Is an Appropriate Content-Type Header for JavaScript Files
File_Get_Contents(): Stream Does Not Support Seeking/When Was PHP Behavior About This Changed
Regular Expression for French Characters
Mongodb: Benefit of Using Objectid VS a String Containing an Id
Datetime Now PHP MySQL (+ Pdo Variant)
Symfony 2.7 Cache:Clear Command Checks Every Database Connection