Get Text from <Option> Tag Using PHP

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.

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>

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>

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


Related Topics



Leave a reply



Submit