0

I'm using jQuery raty for a personal website. I have a form and raty generates the following for each set of stars

<input type="hidden" name="score" value="5"> 

where value contains the values I want to get.

I have 10 fields with 5 stars each, and I want to get all 10 ratings with PHP/POST to insert into database.

What do I do about the fact that the inputs have the same name? I am aware that if it was name="score[]" I could access the ratings in an array, but I don't think I can change the name that raty assigns to inputs

LemonMan
  • 2,963
  • 8
  • 24
  • 34

1 Answers1

1

If you can't change raty's names this should help you

HTML

<input type="hidden" name="score" value="5"> 
<input type="hidden" name="score" value="5"> 
<input type="hidden" name="score" value="5"> 

jQuery

    $(document).ready(){
      var score;
      $('input[name="score"]').each(function(index) {
        //YOUR LOGIC
        //IE: score = $(this).val(); console.log(score);
        //IE: POPULATE hidden submit form or store for AJAX call

      });          

});

But if you can add a class it would probably be better practice to do

HTML

<input type="hidden" class='score' name="score" value="5"> 
<input type="hidden" class='score' name="score" value="5"> 
<input type="hidden" class='score' name="score" value="5"> 

jQuery

    $(document).ready(){
      var score;
      $('.score').each(function(index) {
        //YOUR LOGIC
        //IE: score = $(this).val(); console.log(score);
      });

});

Or play with a parent div id and the child() method

Tom Tom
  • 3,680
  • 5
  • 35
  • 40
  • actually, i think I might have just found a simpler solution. it appears that I can add an argument {scoreName : 'name[]',} and then i should be able to access my ratings with $_POST['name'][0] etc... – LemonMan Nov 13 '13 at 00:42
  • Yeah if raty allows it that simpler indeed – Tom Tom Nov 13 '13 at 00:46
  • i'll see if i can get what i mentioned to work, otherwise your answer is very specific thank you! – LemonMan Nov 13 '13 at 01:22