I'm making a form with checkboxes and radiobuttons
I use the value as price, which I calculate with a script so I get the total price of the selected checkboxes and radiobuttons.
<script>
function calcscore() {
score = 0;
$(".calc:checked").each(function () {
score += Number($(this).val());
});
$("#price").text(score.toFixed(2));
$("#sum").val(score)
}
$().ready(function () {
$(".calc").change(function () {
calcscore()
});
});
</script>
<input type="radio" id="1" value="40" class= "calc" name = "780143050"> Gezichtsbehandeling (incl.)<br>
<input type="radio" id="2" value="40" class = "calc" name = "780143050"> Massage<br>
<input type="radio" id="3" value="75" class = "calc" name = "780143050"> beide
<span id="output"></span><input type="hidden" name="sum" id="sum" value="0">
<p>Totaal extra's : € <span id="price">0</span>
I have the following PHP
test.php
<?php
echo("keuze : " . $_POST['780143050'] . "<br />\n");
?>
I in my php script the value and the ID. How can I echo the ID of the selected radiobutton?
There are 2 ways to do this:
change the value of value to the value of the id
add the value of the id to the value
html:
<input type="radio" id="2" value="40|2" class = "calc" name = "780143050"> Massage<br>
js:
<script>
function calcscore() {
score = 0;
$(".calc:checked").each(function () {
score += Number($(this).val().split('|')[0]);
});
$("#price").text(score.toFixed(2));
$("#sum").val(score)
}
$().ready(function () {
$(".calc").change(function () {
calcscore()
});
});
php
<?php
echo("keuze : " . split('|',$_POST['780143050'])[1] . "<br />\n");
?>
See below code. Selected radio button ID will be received as $_POST['selID']:
$('form').submit(function(){
var selID = $('form input[type=radio]:selected').attr('id');
$(this).append('<input type="hidden" name="selID" value="'+selID+'" />');
});
Related
I have a form which contain radio buttons, which is in a loop. If the loop has 10 count, it will show ten set of radio buttons. I have a structure like below, so I need to get all the values and insert in the database.
HTML:
<form id="sendForm">
<?php
$n = 0;
for ($x=0; $x < count($q_id); $x++)
{
?>
How is our Services?
<label>
<input type="radio" name="Choice_<?php echo $n;?>" id="Choice_<?php echo $n;?>" value="poor"/>
</label>
<label>
<input type="radio" name="Choice_<?php echo $n;?>" id="Choice_<?php echo $n;?>" value="Bad"/>
</label>
<label>
<input type="radio" name="Choice_<?php echo $n;?>" id="Choice_<?php echo $n;?>" value="Good"/>
</label>
<label>
<input type="radio" name="Choice_<?php echo $n;?>" id="Choice_<?php echo $n;?>" value="VeryGood"/>
</label>
<?php
$n = $n+1;
}
?>
</form>
Here is how validation happen:
JavaScript:
var names = [];
$('input:radio').each(function () {
var rname = $(this).attr('name');
if ($.inArray(rname, names) == -1) names.push(rname);
});
//do validation for each group
$.each(names, function (i, name) {
if ($('input[name="' + name + '"]:checked').length == 0) {
$("#Error").after('<span class="error">Choose the above field</span>');
valid=false;
}
});
if(valid)
{
SubmitForm();
}
return valid;
Ajax:
function SubmitForm() {
var formdata = $('#sendForm').serialize();
$.ajax({
type: 'POST',
url : '/post_page.php',
data: formdata,
async: false
}).done(function(msg)
{
//Success or error
}
}
I got stuck now how to get each values of radio buttons selected and get by $_POST[] and save in database.
Description: On the form appears radio buttons with textboxes. Their count = count that you entered in textbox. In test.php file I need get all radio buttons and their textbox values, it does not matter - checked, unchecked radio buttons state.
Error is that I get only the most recent values of radiobutton and textbox. I do not see values of previous radiobuttons and textboxes.
<!doctype html>
<html lang="en">
<head>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
function getnewElementID()
{
var elementID = 0;
if($(".form-element").length > 0)
elementID = $(".form-element").length;
return elementID;
}
$(document).ready(function() {
$('#ok1').click(function () {
var n = $('#box1').val();
var index = 0;
var newElementID = getnewElementID();
if($(".RadioDiv").length > 0)
index = $(".RadioDiv").length;
var newElement = "<div class='RadioDiv form-element'><input type='hidden' name='RadioElements["+newElementID+"]' value='radiogroup'/>\n<input type='text' size='50' name='RadioElementsQueations["+newElementID+"]' placeholder='question...' >";
for (var i=1;i<=n;i++)
{
newElement+="<ol><input type='radio' name='RadioElementsValues["+newElementID+"]' value='radio_"+index+"' for='RadioElementsValuesText["+newElementID+"]'>\n\<input type='text' id='radtext' name='RadioElementsValuesText["+newElementID+"]' size='30' value='choice.."+index+"' ></ol>";
index++;
}
newElement+="</div>";
$("#myForm").append(newElement);
});});
$(document).ready(function() {
$('#submit').click(function () {
var data = $("#myForm").serialize();
if($('input[type="radio"]:checked').length == "0") {
alert("Select any value");
} else {
$.post (
"test.php",
data,
function (response) {
$('#message').html(response);
}
);
}
return false;
});
});
</script>
</head>
<body>
<form id ="myForm" action="test.php">
<label>Select your favourite Activities:</label><br/>
<input type="button" name="ok" value="ok" id="ok1">
<input type="text" value="" id="box1" name="count" size="1"><br/>
<input type="submit" id="submit"/> <br/>
</form>
<div id="message"></div>
</body>
</html>
test.php
<?php
var_dump($_POST);
$elements = $_POST['RadioElementsValuesText'];
if(is_array($elements));
for ($index = 0; $index<sizeof($elements);$index++)
{
$astring = implode("<br/>", $elements);
echo "Choices: ". $astring."<br/>";
}
?>
You should use checkboxes instead of radio button.
In a post request, only one radio button value will be send for the group name (here 'RadioElementsValuesText').
Use javascript to reproduce the radio button behavior on your checkboxes (only one checked at a time).
I am currently struggling to get the value I return from a checkbox to multiply with the value in the input box with jQuery.
I display a div when a checkbox is ticked to ask it for the quantity, thereafter I must multiply the quantity with the value of the checkbox.
Here is the current code:
$(document).ready(function () {
var sum = 0;
var qty = 0;
$("input[type=checkbox]").change(function () {
recalculateQty();
recalculateCheck();
if ($('#1').is(':checked')) {
$('#checked-1').show();
} else {
$('#checked-1').hide();
}
if ($('#2').is(':checked')) {
$('#checked-2').show();
} else {
$('#checked-2').hide();
}
if ($('#3').is(':checked')) {
$('#checked-3').show();
} else {
$('#checked-3').hide();
}
});
function recalculateQty() {
$('.qty').keyup(function () {
$('.qty').each(function () {
qty += parseInt(this.value, 10);
recalculateCheck();
});
});
}
function recalculateCheck() {
var sum = 0;
$("input[type=checkbox]:checked").each(function () {
sum += parseInt($(this).val());
});
$('.qty').keyup(function () {
$('.qty').each(function () {
qty += parseInt(this.value, 10);
});
});
$('#total').val(sum * qty);
}
});
<input type="checkbox" id="1" value="30" name="1" />
<input type="checkbox" id="2" value="60" name="2" />
<input type="checkbox" id="3" value="90" name="3" />
<div style="display:none;" id="checked-1">
<input type="text" name="product_1_qty" id="product_1_qty" placeholder="Quantity" class=" qty" value="0" />
</div>
http://jsfiddle.net/isherwood/9vRtJ/1/
You are trying to set an element value with total ID as a final result, but there is no such element in your html code. Just add this element in your html code (here i put another textbox):
<input type="text" id='total'>
Check out the Live demo for your fix
Or you can use better coding:(Read the comments) - Working DEMO
$(document).ready(function () {
var sum = 0;
var qty = 0;
$("input[type=checkbox]").change(function () {
if ($('#1').is(':checked')) {
$('#checked-1').show();
} else {
$('#checked-1').hide();
}
});
$('.qty').keyup(function () { // if user typed anyting in the Quantity
sum = parseInt($('#1').val()); // get checkbox value and change the data type to int
qty = parseInt($(this).val()); // get Quantity value and change the data type to int
//console.log(sum,qty);
if(sum && qty) { // if the values was not empty
$('#total').val(sum * qty); // show the result in the total element
} else { // if the values was empty
$('#total').val(''); // clear the result textbox
}
});
});
I want to hide and show input field on radio button click event my HTML is
<input type="radio" id="abc" name="abc1" checked = "checked" value="Experienced" />
<label> Experienced </label>
<input type="radio" id="xyz" name="xyz1" checked = "checked" value="Fresher" />
<label>Fresher</label>
<input type="text" name="cardno" id="tyx" /><br />
<p > Number</p>
What I want is when radio button with value="Fresher" is clicked the input box name="cardno" should be hidden.
I tried to solve this by using jQuery but it is not working.
$(document).ready(function () {
$("input[name$='abc1']").click(function () {
var value = $(this).val();
if (value == 'Experianced') {
$("#tyx").show();
} else if (value == 'Fresher') {
$("#tyx").hide();
}
});
Can any body help me how to solve this?
name of your radio button should be same to check on click event or make array name of your radio button
like below code
<input type="radio" id="abc" name="abc1" checked = "checked" value="Experienced" />
<label> Experienced </label>
<input type="radio" id="xyz" name="abc1" checked = "checked" value="Fresher" />
<label>Fresher</label>
<input type="text" name="cardno" id="tyx" /><br />
<p > Number</p>
and then create function like
$(document).ready(function () {
$("input[name$='abc1']").click(function () {
var value = $(this).val();
if (value == 'Experianced') {
$("#tyx").show();
} else if (value == 'Fresher') {
$("#tyx").hide();
}
});
please reply if i can help you more..
change the spelling to Experienced
you used
if (value == 'Experianced')
Besides the typo in Experienced, try setting up a click event for each radio button:
$(document).ready(function() {
$('input[name="abc1"][value="Experienced"]').click(function() {
$("#tyx").show();
});
$('input[name="xyz1"][value="Fresher"]').click(function() {
$("#tyx").hide();
});
});
Note:
You can simplify your selectors to use ids with:
$('#abc')
and
$('#xyz')
Fix with:
$(document).ready(function () {
$("input[type='radio']").click(function () {
var value = $(this).val();
if (value == 'Experianced') {
$("#tyx").show();
} else if (value == 'Fresher') {
$("#tyx").hide();
}
});
});
Html:
<input type="radio" id="abc" name="jobtype" checked = "checked" value="Experienced" />
<label> Experienced </label>
<input type="radio" id="xyz" name="jobtype" checked = "checked" value="Fresher" />
<label>Fresher</label>
<input type="text" name="cardno" id="tyx" /><br />
<p > Number</p>
Radio buttons of same group should have same name
Js:
$(document).ready(function () {
$("input[name='jobtype']").click(function () {
var value = this.value;
if (value == 'Experienced') {
$("#tyx").show();
}
else{
$("#tyx").hide();
}
});
});
<script src="http://www.google.com/jsapi"></script>
<script>
google.load("jquery", "1.4.0");
</script>
<script type="text/javascript">
$(function()
{
$('input[type=button]').click(function ()
{
var txtbox = $('#buttoncheck1').attr('name');
var var_name = $("input[name='radio1']:checked").val();
$('#btn_get').val(var_name);
});
});
</script>
<html>
<body>
<?php
for($i = 0; $i < 2; $i++)
{
echo "
<div id = 'div1' name = 'div1'>
<input type='radio' value='Blue' name='radio1'>Blue</input>
<input type='radio' value='White' name='radio1'>White</input>
<input type='radio' value='Red' name='radio1'>Red</input>
<input id='buttoncheck1' type='button' name='btn' value='Click'></input>
<br />
<input type='text' id='btn_get' name='get_btn_value'></input>
</div>
";
}
?>
</body>
</html>
I can clearly get all the values of the radio buttons in this code but the only problem I've been solving for almost 3 days is that I cant display the value on other textbox. all the values from all the radio button groups are only displayed on the first textbox produced by the loop. please help me . thanks!
id values should always be unique. You can change your duplicated ids to class values and do the following:
$('input[type=button]').click(function ()
{
var $parent = $(this).parent();
var var_name = $parent.find("input[name='radio1']:checked").val();
$parent.find('.btn_get').val(var_name);
});