I have a simple HTML form that is posting to a URL like this...
<form>
<select name="option_dropdown">
<option value="option1">Option 1</option>
<option value="option2">Option 2</option>
<option value="option3">Option 3</option>
<option value="option4">Option 4</option>
</select>
<input class="textinput1" type="text"></input>
<input class="textinput2" type="text"></input>
<input class="textinput2" type="text"></input>
</form>
This generates a URL like this www.example.com/?option_dropdown=option1 when submitted, it does not include the text input boxes as they do not have a name attatched to them.
What I would like to do is add some logic to the text inputs that will choose one depending on its content and then include that parameter when the form is submitted.
Is javascript the way to achieve this?
You ask for javascript way so you can try this.
In order to identify 3 textboxes i've given id's to them.
<form>
<select name="option_dropdown">
<option value="option1">Option 1</option>
<option value="option2">Option 2</option>
<option value="option3">Option 3</option>
<option value="option4">Option 4</option>
</select>
<input class="textinput1" id='txt1' type="text"></input>
<input class="textinput2" id='txt2' type="text"></input>
<input class="textinput2" id='txt3' type="text"></input>
<input type='submit' id='submit'>
</form>
on submitting this form script will add name property to any of the 3 text boxes based on a logic of yours. so only that textbox content will be submitted.
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
/* Any logic which select the textbox to be submitted */
id='txt1'; // it can be txt2 or txt3
$("#submit").click(function(){
$("#"+id).attr("name","text");
});
});
</script>
1.Create a PHP file in the same directory as the html.
2.Edit your form to point to that php file and name your text fields so you could refer to them.
<form method="post" action="formTester.php">
<select name="option_dropdown">
<option value="option1">Option 1</option>
<option value="option2">Option 2</option>
<option value="option3">Option 3</option>
<option value="option4">Option 4</option>
</select>
<input name="textinput1" class="textinput1" type="text"></input>
<input name="textinput2" class="textinput2" type="text"></input>
<input name="textinput3" class="textinput2" type="text"></input>
</form>
3. In your file formTester.php
<?php
// define variables and and get data from the text fields
$text1= $_POST["textinput1"];
$text2= $_POST["textinput2"];
$text3= $_POST["textinput3"];
// Now you can manipulate the data however you want ..add it to database, trim, validate etc
?>
Hope it helps,
Best
<form>
<select name="option_dropdown">
<option value="option1">Option 1</option>
<option value="option2">Option 2</option>
<option value="option3">Option 3</option>
<option value="option4">Option 4</option>
</select>
<input type="text" class="textinput1" name="textinput[]" value="input1" />
<input type="text" class="textinput2" name="textinput[]" value="input2" />
<input type="text" class="textinput3" name="textinput[]" value="input3" />
</form>
One option is to stack your values in an array and parse it on the backend. This removes the need for JavaScript.
<form>
<select name="option_dropdown">
<option value="option1">Option 1</option>
<option value="option2">Option 2</option>
<option value="option3">Option 3</option>
<option value="option4">Option 4</option>
</select>
<input type="text" class="textinput1" name="textinput[]"></input>
<input type="text" class="textinput2" name="textinput[]"></input>
<input type="text" class="textinput3" name="textinput[]"></input>
</form>
Related
I'm trying to add a new locale and the language change option to a menu I created.
Below you can see the code that I have my progress so far
Essentially, I'd like the menu items for "sports" and "districts" to change to Chinese after the radio button for "中文“ is clicked. My JavaScript skill is very low so I'm having problems making this happen. Since it's a short list and I can write Chinese without problem, I thought there may be a simple fix, but so far I haven't made any progress. Any suggestions on how to go about this is much appreciated.
<body>
<form action="">
<input type="radio" name="language" value="english">English<br>
<input type="radio" name="language" value="chinese">中文<br>
</form>
</body>
<b>Find your sport</b>
<form>
<select name="Sports">
<option value="basketball">Basketball</option>
<option value="football">Football</option>
<option value="badminton" selected>Badminton</option>
<option value="table tennis">Table Tennis</option>
<option value="swimming">Swimming</option>
<option value="billiards">Billiards</option>
<option value="archery">Archery</option>
<option value="baseball">Baseball</option>
<option value="bowling">Bowling</option>
<option value="climbing">Climbing</option>
<option value="darts">Darts</option>
<option value="fencing">Fencing</option>
<option value="golf">Golf</option>
<option value="squash">Squash</option>
<option value="tennis">Tennis</option>
</select>
</form>
<b>Find your district</b>
<form>
<select name="Districts">
<option value="xuhui">Xuhui</option>
<option value="jingan">Jingan</option>
<option value="huangpu" selected>Huangpu</option>
<option value="changning">Changning</option>
<option value="pudong">Pudong</option>
<option value="putuo">Putuo</option>
<option value="hongkou" selected>Hongkou</option>
<option value="yangpu">Yangpu</option>
<option value="minhang">Minhang</option>
<option value="songjiang">Songjiang</option>
<option value="qingpu" selected>Qingpu</option>
<option value="jiading">Jiading</option>
<option value="baoshan">Baoshan</option>
<option value="fengxian">Fengxian</option>
<option value="jinshan" selected>Jinshan</option>
<option value="chongming">Chongming</option>
</select>
</form>
<form method="post" name="form1" id="form1">
<input id="submit" name="submit" type="submit" value="Submit" onclick="getVenues();" />
</form>
I am trying to separate multiselect with two side in different html.
In Main.html put select name from[].
Another sub.html put select name to[].
Then, my main.html using iframe to display sub.html.
My quection is, how can I passing selected value in Main Frame Value to iframe
Here is my Main.html code (from[]) https://jsfiddle.net/mouseshop318/yv7fdpj7/21/
<div class="col-xs-5">
<select name="from[]" class="form-control" size="8" multiple="multiple">
<option value="1">Item 1</option>
<option value="2">Item 5</option>
<option value="2">Item 2</option>
<option value="2">Item 4</option>
<option value="3">Item 3</option>
</select>
</div>
<iframe src ="sub.html"></iframe>
sub.html code (to[])
https://jsfiddle.net/mouseshop318/khqkv628/6/
<div class="col-xs-5">
<select name="to[]" id="search_to" class="form-control" size="8" multiple="multiple"></select>
</div>
js
jQuery(document).ready(function($) {
$('.multiselect').multiselect();
});
ref:http://crlcu.github.io/multiselect/examples/data-options.html
Hello I have two select menus, that contain values, my intention is to multiple the values of the two select menus and show them in a TextField,
I've done this successfully carried this out by doing this :
<script type="text/javascript"><!--
function updatesum() {
document.form1.textfield3.value = (document.form1.selectt.value -0) * (document.form1.selecttt.value -0);
}
//--></script>
<form id="form1" name="form1" method="post" action="val.php">
<label><strong><br />
Chelsea vs Arsenal</strong>
<select name="selectt" onchange="updatesum()">
<option value="0">select</option>
<option value="100">100</option>
<option value="200">200</option>
</select>
</label>
<label><strong>odds</strong>
<select name="selecttt" onchange="updatesum()">
<option value="0">select</option>
<option value="100">L</option>
<option value="200">W</option>
<option value="400">D</option>
</select>
</label>
<input name="textfield3" type="text" /><br />
</form>
Now I'm trying to do a double of this select menus using the same name attribute, but it refuses to calculate.
This is what I'vve tried :
document.form1.textfield3.value = (document.form1.selectt.value -0) * (document.form1.selecttt.value -0) }
//--></script>
<form id="form1" name="form1" method="post" action="val.php">
<label><strong><br />
Chelsea vs Arsenal</strong>
<select name="selectt" onchange="updatesum()">
<option value="0">select</option>
<option value="100">100</option>
<option value="200">200</option>
</select>
</label>
<label><strong>odds</strong>
<select name="selecttt" onchange="updatesum()">
<option value="0">select</option>
<option value="100">L</option>
<option value="200">W</option>
<option value="400">D</option>
</select>
</label>
<input name="textfield3" type="text" /><br /> <label><strong><br />
Chelsea vs Arsenal</strong>
<select name="selectt" onchange="updatesum()">
<option value="0">select</option>
<option value="100">100</option>
<option value="200">200</option>
</select>
</label>
<label><strong>odds</strong>
<select name="selecttt" onchange="updatesum()">
<option value="0">select</option>
<option value="100">L</option>
<option value="200">W</option>
<option value="400">D</option>
</select>
</label>
<input name="textfield3" type="text" /><br />
</form>
I was hoping it would work this way, but it didn't, how can I achieve this?
I didn't understand what you are trying to say but i have write some pseudocode hope this will help you...
pseudocode
<script>
var globalVariable=1;
function updateSum(){
//get current element value
globalVariable*=document.form.select.value
textfield.value = globalVariable;
}
</script>
I need to show the submit button on many forms on the same page only if one option from a select is selected.
I have:
<form id="1">
<input type="text">
<select>
<option value="">Select one value</option>
<option value="1">value 1</option>
<option value="2">value 2</option>
</select>
<input type="submit">
</form>
and
<form id="2">
<input type="text">
<select>
<option value="">Select one value</option>
<option value="1">value 1</option>
<option value="2">value 2</option>
</select>
<input type="submit">
</form>
so, I need to show the submit button only if an option of the select is selected
Thanks!!!
This a working example with you html http://jsfiddle.net/g188o5eg/
$('select').on('change', function(){
if($(this).val() != ''){
$(this).parent().find('input[type="submit"]').show();
} else {
$(this).parent().find('input[type="submit"]').hide();
}
});
It will work with all forms on your site :)
Try something like this (would work with multiple select boxes (uses the nearest submit):
<form>
<input type="text">
<select class="select">
<option value="">Select one value</option>
<option value="1">value 1</option>
<option value="2">value 2</option>
</select>
<input type="submit" class="submitbutton">
<script type="text/javascript">
$('.submitbutton').hide();
$('.select').click(function() {
$(this).closest('.submitbutton').show();
});
</script>
I am having problems when I want to pass multiple value to backend from form.
Questions are:
How can I get all values in the multiple select in the backend.
How can I get the different value with same name in checkbox in the backend.
From the code in Jsfiddle, I'd like to show the select result in result.
By the way, I am using Django.
Full code is here: Jsfiddle
<form method="POST">
<select id="select-tag" name="tagging" multiple="multiple">
<option value="">Tagging Sample... </option>
<optgroup label="Type A">
<option value="Aa">Aa</option>
<option value="Ab">Ab</option>
<option value="Ac">Ac</option>
</optgroup>
<optgroup label="TypeB">
<option value="Ba">Ba</option>
<option value="Bb">Bb</option>
<option value="Bc">Bc</option>
</optgroup>
</select>
<input type="checkbox" value='00001' name="sample"></input>
<input type="checkbox" value='00002' name="sample"></input>
<input type="submit" value="Submit" class="submitted_btn" name="btn_action"></input>
</form>
You can use array type name in the html to get the value in backend.
<form method="POST">
<select id="select-tag" name="tagging[]" multiple="multiple">
<option value="">Tagging Sample... </option>
<optgroup label="Type A">
<option value="Aa">Aa</option>
<option value="Ab">Ab</option>
<option value="Ac">Ac</option>
</optgroup>
<optgroup label="TypeB">
<option value="Ba">Ba</option>
<option value="Bb">Bb</option>
<option value="Bc">Bc</option>
</optgroup>
</select>
<input type="checkbox" value='00001' name="sample[]"></input>
<input type="checkbox" value='00002' name="sample[]"></input>
<input type="submit" value="Submit" class="submitted_btn" name="btn_action"></input>
</form>