views:

42

answers:

1

Hello So i have this:

yes: <input type="radio" value="Y" id="SCvoteY" name="vote"></input>  no: <input type="radio" id="SCvoteN" value="N" name="vote"> </input>

How do i write that it should transfer name="vote" data ? #vote doesnt work, .vote either (guess it cause thats for class and ID) but what about name then?

function DoSCInsert(){
    $("#SCres").html("please wait..");
    var nocache = '0';
    var data = { fID : $("#fID").val(), vote : $("#vote").val(), comment: $("#comment").val(), nocache: nocache };
    $.get('insertSC.php', data, onSCInsertComplete);
}

And how do i shorten this, ive heard that you can you a function in jquery called serialize to pass strings but can you(if you know how) show example by this script how to pass it like this one does?

Updated code:

function DoSCInsert(){
    $("#SCres").html("to sek..");
    var nocache = '0';
    var voteRadios = $('input[name="vote"]:checked'); 
    var data = {`enter code here` fID : $("#fID").val(), voteRadios, numChecked: $(voteRadios).length, comment: $("#comment").val(), nocache: nocache };
    $.get('insertSC.php', data, onSCInsertComplete);
}

insertSC.php:

<?php
if($_GET['numChecked'] == '1'){
$fID= $_GET['fID'];
$vote= $_GET['vote'];
$comment= $_GET['comment'];
 if(empty($vote)){ echo "blank"; }else{ echo $vote; }
}else{
echo $_GET["numChecked"];
}
?>
+1  A: 
data = { voteValue: $('input[name="vote"]:checked').val() }

of course you can optimize the selector as you wish... and maybe add logic to make sure at least one of the radios is checked.

meder
doesnt work either
Karem
I have echo $_GET["vote"]; on insertSC.php, and i just get undefined index vote..
Karem
please check my updated question for the updated code
Karem
why do you have `$_GET['vote']` when you set it to `voteValue` ? Try `$_GET['voteValue']`. Also, debug it before you actually use `$.get` if that doesn't solve it.
meder
Thanks that worked, can you help me how to do/give me link if you dont have time, to show me how to do so when neither is checked then give alert or something
Karem
`var voteRadios = $('input[name="vote"]:checked'); if ( !voteRadios.length ) { alert("Please check one") }`
meder
If you have groups of radio buttons you will need to use `.each` or you'll query every single checked radio on the page.
meder
please check updated code in the question, ive accepted your answer for your working answer, the question was about, i hope you further help me with this one only, the script wont work at all now
Karem
`voteRadios` is a jquery object. You are passing it through object literal. If you want the # of checked or not, do this: `data = { numChecked: $(voteRadios).length }`
meder
OK so that works good, check updated code, but as you see in the updated code i also took voteRadios in the data line, and this gives me nonworking script again. I mean i want to check if its empty AND if not then show results.. thank you so far
Karem
sorry if im too newbeginner im just going to make a new question i think, as this one is already answered thanks
Karem
hey i figured it out var data = { vote: $('input[name="vote"]:checked').val(), numChecked: $(voteRadios).length } now it works thank you took me some time but yes!
Karem