1

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>
4
  • var movie_rating_s = $("#add-rateing [name='MOVIE_RATING'][checked='yes'").val();
    – Musa
    Commented Apr 25, 2012 at 21:40
  • Consider using the jquery form plugin. It takes care of properly serializing your form. Commented Apr 25, 2012 at 21:42
  • You should consider spelling "rateing" correctly. It is correctly spelled "rating".
    – Evik James
    Commented Apr 25, 2012 at 21:42
  • Thanks for the help, I will take a look into the form plugin. As for the spelling that's always been something I have had a hard time with.
    – terry
    Commented Apr 25, 2012 at 22:09

3 Answers 3

2

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();
0
0

You need to select the selected radio element, like so:

$('input[name="MOVIE_RATING"]:checked').val();
0

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()

Not the answer you're looking for? Browse other questions tagged or ask your own question.