Is there a way to pass the text in the name of the <option> to a php from?
<form action="process.php" method="post">
<select name="select1">
<option value="1" name="apple">APPLE</option>
<option value="2" name="lemon">LEMON</option>
<option value="3" name="grapes">GRAPES</option>
</select>
<input type="submit" value="submit"/>
</form>
I want to get the fruits name in simple or capital passed to the php from.
I cannot change the values in the select tag because they are used for another function.
I've searched so many sites but all they say is about passing the text of the value, but not the text of the name.
There is no attribute name in <option> tag
My Code:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js"></script>
<form action="process.php" method="post">
<select name="select1" id="select1">
<option value="1" name="apple">APPLE</option>
<option value="2" name="lemon">LEMON</option>
<option value="3" name="grapes">GRAPES</option>
</select>
<input type="hidden" id="hidField" name="xyz" value="" />
<input type="submit" value="submit" />
</form>
<script>
//function test(){
var select1= document.getElementById("select1");
$("#select1").change(function(){
//alert(select1.options[select1.selectedIndex].text);
//alert(select1.options[select1.selectedIndex].getAttribute("name"));
var res=select1.options[select1.selectedIndex].getAttribute("name");
document.getElementById('hidField').value=res; //Javascript code
//$('#hidField').val(res); //Jquery code
return false;
});
//}
</script>
If you are sure want to name field is must there please use getAttribue("name") instead of text
Code:
alert(select1.options[select1.selectedIndex].getAttribute("name"));
Want to get inner data of the <option> tag
Code:
alert(select1.options[select1.selectedIndex].text);
You pass the name-Attributes in a separate function with AJAX. All you have to do is to add an ID-Attribute (e.g. "select1") to the select Tag and read out the "textContent" attribute (Fruits in capital letters) of the selected option. Then pass the function "sendToServer" to the "onchange" event of the select-tag.
function sendToServer()
{
var select = document.getElementById("select1");
var sel = select.options[select.selectedIndex];
var name = select.getAttribute("textContent");
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
}
}
xmlhttp.open("GET", "../PHPScripts/getData.php?q=name", true);
xmlhttp.send();
}
HTML:
<select name="select1" id="select" onchange="sendToServer();">
<option value="1" name="apple">APPLE</option>
<option value="2" name="lemon">LEMON</option>
<option value="3" name="grapes">GRAPES</option>
</select>
<input type="submit" value="submit"/>
</form>
I know, its not exactly what you asked for, but maybe it helps.
Related
I have a noUiSlider with 2 handles, a min and max age, inside my form.
If I var_dump($_POST) the form, only the sex_select value is returned.
<form method="POST">
<select name="sex_select" id="sex-select">
<option selected="selected" disabled="disabled" value="">Sex</option>
<option value="1">Male</option>
<option value="0">Female</option>
</select>
<div name="ageValues[]" id="age-slider">
</form>
Solved.
Created two hidden inputs
<input type="hidden" name="ageMin" value="" id="ageMin-input">
<input type="hidden" name="ageMax" value="" id="ageMax-input">
Set the values of the inputs when the slider values changes
var ageslider = document.getElementById("age-slider");
var ageMin = document.getElementById("ageMin-input");
var ageMax = document.getElementById("ageMax-input");
ageslider.noUiSlider.on('update', function(values, handle) {
ageMin.value = values[0];
ageMax.value = values[1];
});
So, to explain my question. I have four .php codes on web server, and I have drop down list in php code also (it includes name of all four php files that are on server). I have also submit button.
I should do following: User choose one name from drop down list, click submit button, and than that php file should be called and shown on web page.
The problem is, I don't know hot to connect these three part of php code.
<p>
What Genre you want?
<select name="Ganre">
<option value="">Select...</option>
<option value="FPS">FPS</option>
<option value="JRPG">JRPG</option>
<option value="RPG">RPG</option>
<option value="Sports">Sports</option>
</select>
<input type="submit" value="Submit" />
<if (Genre == "FPS") { ?>
<form method="get" action="FPS.php"}>
<else if (Genre == "JRPG") { ?>
<form method="get" action="JRPG.php"}>
<else if (Genre == "RPG") { ?>
<form method="get" action="RPG.php"}>
<else if (Genre == "Sports") { ?>
<form method="get" action="Sports.php"}>
</p>
There are several issues with the code you provided:
You have no opening <?php for those closing ?>.
You have lots of invalid syntax, like If (Genre == "FPS") { (What is Genre?) or like
<else if
You have two spellings of "Genre" (Ganre).
There are several ways to do what you want. As you did not mention Javascript, I propose a PHP-only solution. You would just let the form submit to the
same PHP file, and there detect which choice was made. Then let PHP redirect to the
page of choice:
<?php
if (isset($_GET["genre"])) {
// User submitted their choice, so redirect to that page
// Make sure not to echo anything when using header():
header("Location: " . $_GET["genre"] . ".php");
// Make sure to not execute any other code in this file
exit();
}
// User did not yet submit a choice, so present list
?>
<form method="get">
<p>
What Genre you want?
<select name="genre">
<option value="">Select...</option>
<option value="FPS">FPS</option>
<option value="JRPG">JRPG</option>
<option value="RPG">RPG</option>
<option value="Sports">Sports</option>
</select>
<input type="submit" value="Submit"/>
</p>
</form>
Add onchange into select tag and add below javasctipt code.
<form method="get" action="">
<select name="Ganre" onchange="actionChangdde(this)">
<option value="">Select...</option>
<option value="FPS">FPS</option>
<option value="JRPG">JRPG</option>
<option value="RPG">RPG</option>
<option value="Sports">Sports</option>
</select>
<input type="submit" value="Submit" />
</form>
<script>
function actionChangdde(sel){
var getSelVal = sel.value;
var repAction = getSelVal+'.php';
sel.parentElement.setAttribute("action", repAction);
}
</script>
Try this code :
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
</head>
<form>
<p>
What Genre you want?
<select name="Ganre" id="genre">
<option value="">Select...</option>
<option value="FPS">FPS</option>
<option value="JRPG">JRPG</option>
<option value="RPG">RPG</option>
<option value="Sports">Sports</option>
</select>
<input type="submit" value="Submit" id="submit_button"/>
</p>
</form>
<script type="text/javascript">
$(document).ready(function(){
var dropDownValue = $('#genre').val();
$('#submit_button').click(function () {
if($('#genre').val() == "FPS"){
window.location="FPS.php";
}
if($('#genre').val() == "JRPG"){
window.location="JRPG.php";
}
if($('#genre').val() == "RPG"){
window.location="RPG.php";
}
if($('#genre').val() == "Sports"){
window.location="Sports.php";
}
if($('#genre').val() == ""){
return false;
}
});
});
</script>
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 want to reload my page (PHP) on changing value from the dropdownmenu and passing the value of selected item as a variable alongwith one another variable to the page. I have tried following -
<select name="application" onChange=Refresh(this.value,$query)" \>
<option>Any Application</option>
<option>ChIP</option>
<option>Enzyme Immunoassay</option>
<option>ELISA</option>
<option>Flow Cytometry</option>
<option>FACS</option>
</select>
The Refresh function is:
function Refresh(id,gene){
location.href="getnet.php?app=" + id + "&query=" + gene;
}
And i am receiving variables as:
$query = $_POST['gene'];
$app = $_POST['application'];
you must use form submit() method in onchange event of the dropdown and hidden field for second parameter, for example:
<form name="myForm" method="GET" action="<?php echo $_SERVER['PHP_SELF'];?>">
<select name="first" onChange="document.myForm.submit();">
<option value="1">Value 1
<option value="2">Value 2
<option value="3">Value 3
</select>
<input type="hidden" name="second" value="second parameter value"/>
</form>
On the server side you can get your variables in $_GET supperglobal array like:
$_GET['first'] - it's value from dropdown element and $_GET['second'] - second hidden param.
Try to write js function for dropdown onchange event. Below is sample code:
<script type="text/javascript">
function reload_url(val){
window.location.href = 'some_php_file.php?new_var='.val;
}
</script>
<select name="my_Sel" onchange="reload_url(this.value);">
<option value="1">1</option>
<option value="2">2</option>
</select>
What about trying a form submit onChange?
<form name="form" action="url" method="get">
<select name="application" onChange="this.form.submit();" >
<option>Any Application</option>
<option>ChIP</option>
<option>Enzyme Immunoassay</option>
<option>ELISA</option>
<option>Flow Cytometry</option>
<option>FACS</option>
</select>
<input type="hidden" name="gene" value="variable" />
</form>
for the following form
<form action ='search.php' method = 'post'>
<select name="filter">
<option id="A" value="A">A</option>
<option id="B" value="B">B</option>
</select>
<input type ='text' name="key" id ="field" value ="something that changes based on what option is selected" />
<input type ='submit' value = 'Search' />
</form>
How do i make the value of the input field change based on whether A or B is selected? I assume it has to be done in javascript. I have used jquery here and there so it is ok to make suggestions based on it. Thanks alot!
You don't have to use jQuery, (but jQuery does makes life easier):
HTML:
<form action ='search.php' method = 'post'>
<select id="filter" name="filter">
<option id="A" value="A">A</option>
<option id="B" value="B">B</option>
</select>
<input type ='text' name="key" id ="field" value ="something that changes based on what option is selected" />
<input type ='submit' value = 'Search' />
</form>
Javascript:
document.getElementById('filter').onchange = function () {
document.getElementById('field').value = event.target.value
}
Example
you may try this
<script type="text/javascript">
function changeValue(){
var option=document.getElementById('filter').value;
if(option=="A"){
document.getElementById('field').value="A Selected";
}
else if(option=="B"){
document.getElementById('field').value="B Selected";
}
}
</script>
<form action ='search.php' method = 'post'>
<select name="filter" id="filter" onchange="changeValue();">
<option id="A" value="A">A</option>
<option id="B" value="B">B</option>
</select>
<input type ='text' name="key" id ="field" value ="something that changes based on what option is selected" />
<input type ='submit' value = 'Search' />
</form>
give id filter to select
$(document).ready(function(){
var text_val;
$('#filter').change(function(){
text_val = $(this).val();
$('#field').attr('value',text_val);
return false;
});
});
Try....
$('select[name=filter]').change(function() {
var input_field = $('input#field');
switch ($(this).val()) {
case 'A': input_field.val('Value if A is chosen'); break;
case 'B': input_field.val('Value if B is chosen'); break;
}
});