I know this question has been asked a lot of times in several other ways but none of them helped me. So, I have formatted it in my own words.
Supppose I have a select box like this:
<select name="something">
<option value="1"><?php echo $value1; ?> for <?php echo $value2; ?>
</select>
<input type="text" name="sometext" value="">
I want to have the <?php echo $value1; ?> in the text field updated live on change of the select box option. To be clear, I DO NOT want the value="1" in the text field and I need to have that value="1" there for some reason and I cannot replace it with value="<?php echo $value1; ?>". I strictly want the inside value <?php echo $value1; ?> to be replaced in the text field. But I do not know how I can achieve it. Please help me experts. For live changing jQuery preferred.
Try below code:
<select name="something">
<option value="1" data="<?php echo $value1; ?>"><?php echo $value1; ?> for <?php echo $value2; ?></option>
</select>
<input type="text" name="sometext" value="">
See below working HTML
jQuery(document).ready(function($){
$('#something').change(function(){
$('#sometext').val($(this).find(':selected').attr('data'));
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="something" id="something">
<option value="1" data="value1">value1 form value01</option>
<option value="1" data="value2">value2 form value02</option>
<option value="1" data="value3">value3 form value03</option>
<option value="1" data="value4">value4 form value04</option>
</select>
<input type="text" name="sometext" id="sometext" value="">
Hope this will work for you,
$(document).ready(function() {
$('select').change(function(){
var selectedOption = $("select option:selected").text(); //Get the text
selectedOption = selectedOption.split("for"); //split the 2 value by using for
alert(selectedOption[0]); //Get the value before for
$('input').val(selectedOption[0]);
});
});
You need to fetch the selected option text instead of the usual option value, it is pretty easy to do.
You don't need to add any data attributes to achieve this, as there is a method to fetch the actual option text.
Here is a working example:
PHP for populating a select element (below I show the static HTML)
<select id="something" name="something">
<?php foreach($selectList as $value=>$text){ ?>
<option value="<?php echo $value;?>"> <?php echo $text;?> </option>
<?php } ?>
</select>
HTML
<select id="something" name="something">
<option value="1"> Value 1 </option>
<option value="2"> Value 2 </option>
<option value="3"> Value 3 </option>
</select>
<input type="text" id="sometext" name="sometext" value=""/>
jQuery
$(document).ready(function(){
$('#something').on('change',function(){
var selectedText = $(this).children(':selected').text();
$('#sometext').val(selectedText);
});
});
This approach is fast and easy to maintain.
And of course a jsfiddle to see it in action: https://jsfiddle.net/kzgapkt5
Hope it helps a bit
Related
A combo box which has the option of to type an entry works correctly in HTML, but the problem I have now is if you have too many data of to display no scroll bar found there means that there are many data which are displayed out of screen.
<input type="text" name="example" list="exList">
<datalist id="exList">
<option value="1">
<option value="2">
<option value="3">
<option value="4">
<option value="n">
</datalist>
Please anyone can help me.
It is not possible to add scrollbar to DataList because it does not support any css properties, so one way is that you can use dropdownlist,
<select>
<option/>
</select>. Scrollbar automatically added to dropdownlist..
<?php $cars = array("Volvo", "BMW", "Toyota"); ?>
<script>
function getOptions()
{
document.getElementById("sel1").style.visibility = "visible";
}
function addtext(a)
{
document.getElementById("txt1").value = a.innerHTML;
document.getElementById("sel1").style.visibility = "hidden";
}
</script>
<input type="text" name="txt1" id="txt1" onClick="getOptions()"><br/>
<select id="sel1" size="4" style="width:150px;visibility:hidden;" >
<?php
foreach($cars as $x => $x_value)
{
echo "<option onClick='addtext(this)' value=".$x.">".$x_value."</option>";
}
?>
</select>
I am having issues with php and form handling. I want to be able to click a dropdown box(like you can do with a form in html, with the different options tags) in php, and allow the selection to take the designated value.
Here is what I have :
<input value="<?php echo isset($results['example']) ? $results['example']: ''; ?>" class="form-control" name="data[example]" placeholder="Example">
That populates with a place you can type in input text, which is accepted perfectly in post, but I do not want the user typing in data, only selecting from options, such as this html form does :
<form action="example.php">
<option value="11">apples</option>
<option value="12">bananas</option>
<option value="13">oranges</option>
</form>
Here is an edit using the data given :
<form>
<select name="example">
<option value="<?php echo isset($results['example']) ? $results['example']: ''; ?>">apples</option>
<option value="<?php echo isset($results['example']) ? $results['example']: ''; ?>">oranges</option>
<option value="<?php echo isset($results['example']) ? $results['example']: ''; ?>">bananas</option>
</select>
</form>
I did not add the action because it will be posted with other data, but did try both ways(with and without action="post.php") and it does not work...
Defining your options inside a select tag
<select>
<option value="11">apples</option>
<option value="12">bananas</option>
<option value="13">oranges</option>
</select>
Should give you in your post the position (value) of the selected item. Then in your php pass value to the selected items *11,12,13 = apples,bananas,oranges**
Hope it help
You need a <select name="name"> </select> around your options
I think you need something like that
<form action="example.php">
<select name="fruit">
<option value="<?php echo isset($results['example']) ? $results['example']: ''; ?>">apples</option>
</select>
</form>
I'd say the issue is not PHP, I think you just need to use the SELECT and OPTION form tags:
You are specifying an INPUT tag, which by definition allows the user to input data.
<form action="example.php">
<select class="form-control" name="data[example]">
<option value="<?php echo isset($results['example']) ? $results['example']: ''; ?>"">
</select>
</form>
So you are looking for a dropdown I guess:
<form action="example.php">
<select name="fruit">
<option value="1">Apple</option>
<option value="2">Banana</option>
<option value="3">Orange</option>
</select>
</form>
On submiting the form you can get the value like: $_POST['fruit'] (in this case).
use type="checkbox", so you can select one more
<input type="checkbox" name="vehicle[]" value="Bike"> I have a bike<br>
<input type="checkbox" name="vehicle[]" value="Car"> I have a car<br>
<input type="checkbox" name="vehicle[]" value="Boat" checked> I have a boat<br>
in php
<?php
$vehicleArray='';
foreach($_POST['vehicle'] as $value) {
$vehicleArray.=$value.',';
}
//output 'Bike,Car,Boat'
?>
I need to show the item_id when I click one of the item_name list in the dropdown. And when I click submit, the item_name and item_id got by $_POST. Can javascript use to do that?
<?php
$brg="SELECT item_id, item_name FROM tblItem";
$b=mysql_query($brg);
?>
<td align="center">
<select name="item_name">
<?php while($br=mysql_fetch_array($b)){ ?>
<option value=<?echo $br[0].'_'.$br[1]?> selected>
<?php echo $br[1]; ?>
</option>
<?php } ?>
</select>
</td>
Jup, that's possible with Javascript.
Your HTML:
<select name="item_name">
<option value="1_Cheese">Cheese</option>
<option value="2_Cars">Cars</option>
<option value="3_Planes">Planes</option>
</select>
<input type="button" onclick="alert(getSelectedValue())" value="Get Select Id" />
And your Javascript, to access the ID from the options value:
function getSelectedValue()
{
var sel = document.getElementsByName('item_name')[0];
return sel.options[sel.selectedIndex].value.split("_")[0];
}
See this fiddle: http://jsfiddle.net/acz6Q/
It would be a little easier with jQuery (also appended an event handler):
$(document).ready(function()
{
$("select[name=item_name]").on("change", function()
{
alert("new item id = " + $(this).val().split("_")[0]);
});
});
And here is the jQuery version fiddle: http://jsfiddle.net/acz6Q/1/
Hope this helps!
Try this:
<form action="" method="post">
<input type="hidden" id="item_id" name="item_id" />
<input type="hidden" id="item_name" name="item_name" />
<select id="my_select" name="my_select">
<option value="item_id_1">item_name_1</option>
<option value="item_id_2">item_name_2</option>
<option value="item_id_3">item_name_3</option>
</select>
</form>
<script type="text/javascript">
$(document).ready(function() {
$("#my_select").change(function() {
$("#item_id").val($("#my_select").val());
$("#item_name").val($("#my_select option:selected").text());
});
});
</script>
Note that you need to include the jQuery library first. Also you can see how it works from here jsfiddle.net/2FsNP/3. And with this method you don't need to combine between value and text of the option tag with the underscore.
I am trying to call a javascript function from the onchange attribute of the select tag ! My issue is that I am passing the name attribute of the select to the function which always go null.
<body>
<form action="" method="post">
<select name="slct" id="name" onchange="rohan('measurement_conversion', '<?php echo isset($_POST["slct"])?$_POST["slct"]:"null" ?>')">
<option value="yes" selected="selected"> yes </option>
<option value="nopes"> nopes </option>
<option value="may be"> May be </option>
<option value="dont know"> dont know </option>
</select>
</form>
<div id="abc">
</div>
</body>
And here my javascript function
<script>
function rohan(var1, var2)
{
document.getElementById("abc").innerHTML=var1 + " " + var2;
}
</script>
It always prints null..
Any help will be appreciated !
HTML:
<body>
<form action="" method="post">
<select name="slct" id="name" onchange="rohan(this.value)">
<option>Select</option>
<option value="yes" selected="selected"> yes </option>
<option value="nopes"> nopes </option>
<option value="may be"> May be </option>
<option value="dont know"> dont know </option>
</select>
</form>
</body>
JS:
<script>
function rohan(value)
{
//you can get the value from arguments itself
alert(value);
}
</script>
PHP is run on the server before JavaScript is ever run in the browser. It doesn't get re-evaluated when the user changes the selected item.
No put in tag HTML attribute javascript, onchange is better in the script:
<script>
var b=document.getElementById('name');
b.onchange=function (){
var a=document.getElementById('name').value;
console.log(a);
}
</script>
I am trying to use HTML select dropdowns to help sort the search results on my website and I am having some trouble. In the code below, I specifically select Option1 and when I try to echo the result after I submit the form, it always echo's 'FAIL'. I was wondering if someone can help me understand what I am doing wrong and what is the proper way to retrieve the data from the option the user selected, after the form was submitted?
<?php
echo '<form method="post" action="search.php">
<select>
<option name="one">Option1 </option>
<option name="two">Option2</option>
</select>
<input type="text" name="searchword" />
<input type="submit" value="Search" />
</form>';
if(isset($_POST['one'])){
echo 'Option1 is set';
}else{
echo 'FAIL';
}
?>
thank you
This is because the name attribute goes with the select tag.
<select name="dropdown">
<option>Option 1</option>
<option>Option 1</option>
</select>
if(isset($_POST['dropdown'])) {
echo $_POST['dropdown']; //echoes 'Option 1'
}
If you like, you can add a value attribute to the option element, but you don't have to.
If you do
<option value="foobar">Option1</option>
The form will post foobar and not Option1.
<?php
echo '<form method="post" action="search.php">
<select name="my_option">
<option value="one">Option1 </option>
<option value="two">Option2</option>
</select>
<input type="text" name="searchword" />
<input type="submit" value="Search" />
</form>';
echo "My option value is : " . $_POST['my_option'];
?>
You need to name your select tag and access that instead.
<select name="options"></select>
echo $_POST['options']; will give you your selected option
Instead of name="one" an option needs a
value="myvalue"
and your select tag needs an
name="nameofthisthinghere"