Form input value based on option value selected - php

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;
}
});

Related

How do I POST noUiSlider values along with the rest of my form?

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];
});

Pass the "name" text in option to php form

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.

Using <select> and <input type='text'> together

I'm trying to use both a <select> field and <input type='text'> field together with a $_GET method. Basically I'm hoping to have something like this:
<select>
<option name='artist'>Artist</option>
<option name='song'>Song</option>
</select>
<input type='text' name='value' />
And then hopefully for it to redirect as ?artist=value or ?song=value depending on the selected. I hope this makes sense and any help would be appreciated.
No, you cannot do that. Not directly.
You should attach the name attribute to the select element. Then $_GET will contain your_select_name=option_value.
Then just correlate your $_GET['type'] and $_GET['value'] in the backend.
<form method="GET">
<select name="type">
<option value='artist'>Artist</option>
<option value='song'>Song</option>
</select>
<input type='text' name='value' />
</form>
<?php
echo $_GET['type']; // 'artist' or 'song'
echo $_GET['value']; // value of text input
P.S.: If you need to be strict about forming your URL, you can either provide two inputs and rig up a simple JS script that will hide the input not related to your select choice.
Actually, this idea calls for a little elaboration:
<form method="GET">
<select id='type'>
<option name='artist'>Artist</option>
<option selected name='song'>Song</option>
</select>
<input class='inp' id='song' type='text' name='song' />
<input class='inp' style='display:none' id='artist' type='text' name='artist' />
</form>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script>
$('#type').change(function() {
$('.inp').val('').hide().prop('disabled', true); // hide and clear both inputs
$('#'+$('#type').val() ).prop('disabled', false).show(); //show input corresponding to your select
// Note the prop('disabled') calls - you want to disable the unused input so it does not add an empty key to your query string.
}
</script>
I think you should put the name attribute on the <select> tag.
<select name="Category">
<option>Song</option>
<option>Artist</option>
</select>
<input type="text" name="Value"/>
then it should be fairly easy to validate what have been chosen
exemple (PHP):
if($_GET['Category'] == "Song")
{
//Do stuff here with $_GET['Value']
}
else if($_GET['Category'] == "Artist")
{
//Do stuuf here with $_GET['Value']
}

How to retrieve information from $.post

I am currently running JavaScript after a click of a button in my PHP page which saves data.
a.js:
function save(){
var oldItems = JSON.parse(localStorage.getItem('itemsArray')) || [];
var newItem = {};
var num = document.getElementById("num").value;
newItem[num] = {
"methv": document.getElementById("methv").value
,'q1': document.getElementById("q1").value,
'q2':document.getElementById("q2").value,
'q3':document.getElementById("q3").value,
'q4':document.getElementById("q4").value,
'comm':document.getElementById("comm").value
};
oldItems.push(newItem);
localStorage.setItem('itemsArray', JSON.stringify(oldItems));
$.post('edit.php', { items: JSON.stringify(oldItems) }, function(response) {
});
In the PHP page, my form looks like this:
edit.php
<form action="" method="post" enctype="multipart/form-data">
<select name="methv" class="textfields" id="methv" style="width:110px" >
<option value= "dont know">dont know </option>
<select name="q1" class="textfields" id="q1" style="width:50px" >
<option value= "-">-</option>
<option value= "L">L</option>
<select name="q2" class="textfields" id="q2" style="width:50px" >
<option value= "-">-</option>
<option value= "L">L</option>
<select name="q3" class="textfields" id="q3" style="width:50px" >
<option value= "-">-</option>
<option value= "L">L</option>
<select name="q4" class="textfields" id="q4" style="width:50px" >
<option value= "-">-</option>
<option value= "L">L</option>
<textarea rows="4" cols="40" id="comm" name="comm" style="width:300px"><?php echo $post['addcomment'] ;?></textarea>
</form>
When I run the PHP page and click the button, in the console, I get a post:
[{"1173627548":{"methv":"dont know","q1":"-","q2":"-","q3":"U","q4":"-","comm":""}},{"1173627548":{"methv":"dont know","q1":"-","q2":"-","q3":"U","q4":"-","comm":""}}]
These are the results from the local storage- one from before and one after it- being clicked (adds every time the button is clicked). How do I get this information and display it on the PHP page?
Do you already have an edit.php file to handle the POST request? This link might help you if not. http://edwin.baculsoft.com/2011/12/how-to-handle-json-post-request-using-php/
Once edit.php is set up to handle the request and return data, you'll probably want to manipulate that data within your response function.
$.post('edit.php', { items: JSON.stringify(oldItems) }, function(data) {
// Do something here, for example...
alert(data);
});
Hopefully this is what you're looking for.

How to pass the value of an option field as well as an input box via post in jquery/PHP?

I have a search bar like
<div id="searchbar">
<form>
<select name="Cars">
<option value="big">Big Cars</option>
<option value="small">Small Cars</option>
</select>
<input type ='text' name="searchkey" id ="search" onblur ='setTimeout('removeSearchSuggestions()', 20);' onkeyup ='getSearchSuggestions(this.value);'/>
<br/>
<div id = 'searchsuggest'></div>
<input type ='submit' value = 'Search' />
</form>
</div>
and i have the following jquery code snippet
function getSearchSuggestions(value){
if (value !=""){
$.post("searchSuggest.php", {searchPart:value}, function(data) {
$("#searchsuggest").html(data);
doSearchCSS();
});
} else {
removeSearchSuggestions();
}
}
So searchSuggest has access to "searchPart" i.e. the value that is typed into the input box. I am modifying something I got to work for a case without options. How do I modify the jquery function to access the selected option in searchSuggest.php?
Thanks!
This will help you: http://api.jquery.com/selected-selector/

Categories