This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I get which radio is selected via jQuery?
I am attempting to put a user rating system on my site but only the first value gets passed on, so in the table the rating column is always a one. Its been a while since I have worked with radios.
Here is what I have.
<form id="add-rateing">
<input type="radio" name="MOVIE_RATING" value="1" > 1
<input type="radio" name="MOVIE_RATING" value="2" > 2
<input type="radio" name="MOVIE_RATING" value="3" checked="yes"> 3
<input type="radio" name="MOVIE_RATING" value="4" > 4
<input type="radio" name="MOVIE_RATING" value="5" > 5 <br>
<input type="hidden" name="MOVIE_ID" value="<?php echo $id; ?>">
<input type="hidden" name="MOVIE_TITLE" value="<?php echo $title; ?>">
<input type="hidden" name="USER_ID" value="<?php echo $loggedinusername; ?>">
<input type="submit" value="I Drank To The Credits" onclick="$('#add-rateing').hide('fast')">
</form>
<script type="text/javascript">
$("#add-rateing").submit(function(event){
event.preventDefault()
addrateing();
});
function addrateing()
{
var movie_rating_s = $("#add-rateing [name='MOVIE_RATING']").val();
var movie_id_s = $("#add-rateing [name='MOVIE_ID']").val();
var movie_title_s = $("#add-rateing [name='MOVIE_TITLE']").val();
var user_id_s = $("#add-rateing [name='USER_ID']").val();
var errors = '';
$.ajax({
type : "POST",
url : "movie_watched.php",
data : { rating: movie_rating_s,
movie : movie_id_s,
title: movie_title_s,
user : user_id_s, },
cache : false, timeout: 10000,
success : function() {
alert("You have played <?php echo $title; ?> ");
},
error : function() {
alert("there is a problom");
},
complete : function() {
}
});
};
</script>
Try chaging this :
var movie_rating_s = $("#add-rateing [name='MOVIE_RATING']").val();
into this:
var movie_rating_s = $("#add-rateing [name='MOVIE_RATING']:checked").val();
You need to select the selected radio element, like so:
$('input[name="MOVIE_RATING"]:checked').val();
It looks like the problem is with your jQuery selector. It's not specifying to grab the value of the "checked" radio button. Try this when loading up your movie_rating_s variable:
var movie_rating_s = $("#add-rateing [name='MOVIE_RATING']:checked").val()
Related
This is the HTML for the check box, I have a function that only allows three to be selected
<form class="interestSearchCriteria">
<input type="checkbox" name="walking" value="Walking"> Walking
<input type="checkbox" name="running" value="Running" > Running
<input type="checkbox" name="hiking" value="Hiking" > Hiking
<input type="checkbox" name="surfing" value="Surfing" > Surfing
<input type="checkbox" name="powerkiting" value="Kiting" > Kiting
<input type="checkbox" name="gym" value="GYM" > GYM
<input type="checkbox" name="cycling" value="Cycling" > Cycling<br><br><br>
<input type="submit" value="Submit" id="updateInterest">
</form>
Ajax call, I have three variables but how do i assign the first, second and third ticked values to each individual variable so I can send post to three separate columns?
$(document).ready(function() {
$(document).on('click', '#updateInterest', function() {
var firstInterest = $('input[type=checkbox]:checked').val();
var secondInterest=$('input[type=checkbox]:checked').val();
var thirdInterest=$('input[type=checkbox]:checked').val();
$.ajax({
type: "POST",
url: "http://localhost:8888/link",
data: {
option1: firstInterest,
option2:secondInterest,
option3:thirdInterest
}
});
})
})
This will allow up to 3 options to be selected, and also put the data into the correct option fields for the ajax call.
$(document).ready(function() {
$(document).on('click', '#updateInterest', function(e) {
e.preventDefault(); //stop default postback behaviour
var valid = true;
var data = { option1 : null, option2 : null, option3 : null };
$("input[type=checkbox]:checked").each(function(i, obj) {
if (i < 3) {
data["option" + (i+1).toString()] = this.value;
}
else {
alert("Please select up to 3 items");
valid = false;
return;
}
});
if (valid == false) { return; }
alert(JSON.stringify(data)); //just to test
$.ajax({
type: "POST",
url: "http://localhost:8888/link",
data: data
});
});
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form class="interestSearchCriteria">
<input type="checkbox" name="walking" value="Walking"> Walking
<input type="checkbox" name="running" value="Running" > Running
<input type="checkbox" name="hiking" value="Hiking" > Hiking
<input type="checkbox" name="surfing" value="Surfing" > Surfing
<input type="checkbox" name="powerkiting" value="Kiting" > Kiting
<input type="checkbox" name="gym" value="GYM" > GYM
<input type="checkbox" name="cycling" value="Cycling" > Cycling<br><br><br>
<input type="submit" value="Submit" id="updateInterest">
</form>
$(document).on('click', '#updateInterest', function() {
var checked = $('input[type=checkbox]:checked');
var firstInterest = checked.eq(0).val();
var secondInterest = checked.length > 1 ? checked.eq(1).val() : "";
var thirdInterest = checked.length > 2 ? checked.eq(2).val() : "";
console.log(firstInterest);
console.log(secondInterest);
console.log(thirdInterest);
return false;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form class="interestSearchCriteria">
<input type="checkbox" name="walking" value="Walking"> Walking
<input type="checkbox" name="running" value="Running"> Running
<input type="checkbox" name="hiking" value="Hiking"> Hiking
<input type="checkbox" name="surfing" value="Surfing"> Surfing
<input type="checkbox" name="powerkiting" value="Kiting"> Kiting
<input type="checkbox" name="gym" value="GYM"> GYM
<input type="checkbox" name="cycling" value="Cycling"> Cycling<br><br><br>
<input type="submit" value="Submit" id="updateInterest">
</form>
To select all checked checkboxes:
$('input[type=checkbox]:checked')
To get first, second, third or any other index(say n):
.eq(n)
I've been having this issue for a while now and I guess I'm stuck. I've been reading on how to use AJAX to send data to a PHP file.
The thing is, I don't really get how to take the value of a radio button in a HTML page and then send that value through AJAX to a PHP file which will then process that value.
So for example:
If I had 2 radio buttons
<input type="radio" name="radio" value="yes">
<input type="radio" name="radio" value="no">
And I would like to take the value of those radio buttons and send it to my PHP file using AJAX.
Next I would be able to process those values in PHP.
How would I make it so that AJAX sends the value of the selected radio button to PHP?
Thanks, in advance!
EDIT:
Full code:
HTML File
<script type="text/javascript">
function updatePremium() {
var input1= $("#premium-yes").val();
var input2= $("#premium-no").val();
$.post( "upd_premium.php", { input1: input1, input2: input2 } );
}
</script>
<div class="usersRow2">
<form action="" method="POST"><label>Premium:</label> <p class="text-info-premium"><?php echo ucwords($premium_check) ?><i class="icon-star"></i></p> <div class="controls-premium"><i class="fa fa-pencil"></i> Edit</div></form>
</div>
<script>
$('#edit-premium').click(function() {
var text = $('.text-info-premium').text();
var input = $('<input type="radio" name="premium" id="premium-yes" value="yes">Yes <input type="radio" name="premium" id="premium-no" value="no">No <div id="premium"></div>')
$('.text-info-premium').text('').append(input);
$('#edit-premium').remove();
$('Update').insertAfter('#premium');
$('<i class="fa fa-times" title="Cancel Edit"></i> <br /><br /><br />').insertAfter('#update-premium');
$('.fa-times').click(function() {
location.reload();
});
});
</script>
PHP File
if($_POST['input1'] == 'yes') {
print_r($_POST);
}
Use jQuery. From the docs, a post is easy:
$.post( "test.php", { name: "John", time: "2pm" } );
Now, you need to get the values of the inputs and pass them to the post:
<input type="radio" name="radio" value="yes" id="input1">
<input type="radio" name="radio" value="no" id="input2">
<script>
var input1= $( "#input1" ).val();
var input2= $( "#input2" ).val();
$.post( "yourphpfile.php", { input1: input1, input2: input2 } );
</script>
In the php script when I echo the $_POST['AllItems '] then it shows only the last selected value
instead of a whole string of values or array.
Below is the javascript am using :
$(function () {
$('#senditems').click(function () {
var items = $('input[name^="item"]:checked').map(function () {
return this.value;
}).get();
*****************************
//here when i do alert(items); ,
then it shows comma separated values - 1,22,321
****************************
$.post("saveitems.php", {
AllItems: items
});
});
});
Form is :
<form>
<input type="checkbox" name="items[1]" value="1" />
<input type="checkbox" name="items[22]" value="22" />
<input type="checkbox" name="items[321]" value="321" />
</form>
This is how your form should look.
<form id="my_form">
<input type="checkbox" name="items[]" value="1" />
<input type="checkbox" name="items[]" value="22" />
<input type="checkbox" name="items[]" value="321" />
</form>
Then in your php, you can iterate through all the $_POST['items']
If you want to use jQuery to post, you can use this, but is not necessary as you can add a method and action to the html in your form:
$('#senditems').on('click', function(){
var form_data = $('#my_form').serialize();
$.post({
url: "your_php.php",
data: form_data
});
});
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();
}
});
});
My code snippet for radio buttons is -
<input type="radio" name="isPush" id="isPushYes" <? if($isPush) {?>checked="checked"<?}?> value="<?= $isPush;?>" > <? echo "Yes"?></input>
<input type="radio" name="isPush" id="isPushNo" <? if(!$isPush) {?>checked="checked"<?}?> value="<?= $isPush;?>" > <? echo "No"?></input>
<table id="emailTable"><tr><td>...</td></tr></table>
The value of isPush will be 0 or 1. So one of my radio buttons will always be selected. And selecting either of the radio buttons initially is not in my hands.
Now, For the table, I want to set display: none when when second radio button is selected and set display: visible when first radio button is selected.
So which event should I put? I certainly can't put onclick.
You need to check the selected radio onload, as well as onchange. This means the table will be hidden to start with, if No is selected by default.
jsFiddle Demo
function checkRadio()
{
var tbl = document.getElementById("emailTable");
if(document.getElementById("isPushYes").checked)
{
tbl.style.display = "";
}
else
{
tbl.style.display = "none";
}
}
window.onload = function()
{
document.getElementById("isPushYes").onchange = checkRadio;
document.getElementById("isPushNo").onchange = checkRadio;
checkRadio();
}
Another option is without the onload event, you set the display with PHP:
<table id="emailTable" <?php if(!$isPush) echo 'style="display:none"'; ?>><tr><td>...</td></tr></table>
i think this is what you want:
<label><input type="radio" name="myradio" value="1" checked="true" onchange="hideActive(this.name);" /> radio 1</label>
<label><input type="radio" name="myradio" value="2" onchange="hideActive(this.name);" /> radio 2</label>
<label><input type="radio" name="myradio" value="3" onchange="hideActive(this.name);" /> radio 3</label>
<script type="text/javascript">
function hideActive(radioGroupName) {
var all = document.getElementsByName(radioGroupName);
for(var n = 0; n < all.length; n++) {
if(true === all[n].checked) {
all[n].setAttribute('style', 'display: none;');
}
else {
all[n].removeAttribute('style');
}
}
}
hideActive('myradio');
</script>
if you support jquery try this
<label><input type="radio" name="myradio" value="1" checked="true" /> radio 1</label>
<label><input type="radio" name="myradio" value="2" /> radio 2</label>
<label><input type="radio" name="myradio" value="3" /> radio 3</label>
<script type="text/javascript">
jQuery(function() {
var all = jQuery('input[name=myradio]');
var change = function() {
all
.show()
.filter(':checked').hide();
};
all.change(function(){
change();
});
//call init
change();
});
</script>
You want to perform some action when the radio button is checked by the user, so you'll want to bind a handler for the onchange event.
you should write a javascript function that changes the display attribute and then call it in 2 places.
1- When the document loads.
window.onload = function () { javascript_function(); }
if you have JQuery you can do this:
$(document).ready(function(){
javascript_function();
)};
2- When there's a change in the radio button.
Either set a onchange="javascript_function" or use JQuery.
$(document).ready(function(){
javascript_function();
$("#radio_id").change(javascript_function());
)};
Its a test. Change it as like as you want
<script type="text/javascript">
function fnc(myid,otherid)
{
if(document.getElementById(myid).checked)
{document.getElementById(otherid).disabled='disabled';}
}
</script>
<input type="radio" name="isPush" id="isPushYes" onchange="fnc(this.id,'isPushNo')">
<input type="radio" name="isPush" id="isPushNo" onchange="fnc(this.id,'isPushYes')">
Onchange, for example (in JQuery):
$('input[name="isPush"]').change(function() {
// your code
});
Or:
<input onchange="myFunction();" type="radio" name="isPush" id="isPushYes" <? if($isPush) {?>checked="checked"<?}?> value="<?= $isPush;?>" > <? echo "Yes"?></input>
<input onchange="myFunction();" type="radio" name="isPush" id="isPushNo" <? if(!$isPush) {?>checked="checked"<?}?> value="<?= $isPush;?>" > <? echo "No"?></input>
I personally would look at onChange, this will catch occasions where the selected value changes. If doing this in jquery I'd do something like:
$('input[type=radio][name=isPush]').bind('change', function(e){
console.log('We had a change');
});
Note that if the item is dynamically added at all you should change bind to live.
here is the fidddle..
http://jsfiddle.net/vfuV5/2/
for example:
if($('input:radio[name="isPush"]:checked').val() == 'Yes') // used yes u can use 1 here
{
$('#emailTable').show();
}else{
$('#emailTable').hide();
}
$("input[name='isPush']").change(function() {
if($('input:radio[name="isPush"]:checked').val() == 'Yes') // used yes u can use 1 here
{
$('#emailTable').show();
}else{
$('#emailTable').hide();
}
});
OR
make a function
function checkRadioValue()
{
if($('input:radio[name="isPush"]:checked').val() == 'Yes') // used yes u can use 1 here
{
$('#emailTable').show();
}else{
$('#emailTable').hide();
}
}
Call this function on document.ready and onchange function
$(document).ready(function(){
checkRadioValue();
$("input[name='isPush']").change(function() {
checkRadioValue();
});
})