davidb
davidb

Reputation: 1603

Getting jquery value into php using ajax

I'm having some difficulty with passing a value from jquery into php. I'm using a plugin rateit for jquery to create a star rating system. I managed to connect the rating with my database to retrieve data and display an average rating etc. Currently I'm struggling with how to allow users to insert the date(stars) they've selected into database. This is how my code looks like:

 <div id="itemreview">
    <form action="" name="review" id="review" method ="post">   
          Rating: <input type="range" value="0" step="0.5" id="rating">
          <div id="stars" class="rateit" onclick="" data-rateit-backingfld="#rating" data-rateit-resetable="false"  data-rateit-ispreset="true"
          data-rateit-min="0" data-rateit-max="10">
         </div>
         <input type="submit" name="submitcomment" id="submit"  value="Comment!">
         </form></div>

This is my Jquery/Ajax:

 <script>
      $(document).ready(function () {
          $('#submit').click(function () {

              var value = $('#stars').rateit('value');

              $.ajax({
                url: 'itemreview.php',
                type: "POST",
                data: {value:"value"},
                success : function(data) {
                }
              });
          });
      });

  </script>    

And my php:

      if(isset($_POST['submitcomment'])) {
      $userrating = $_POST['value'];


      echo "Posted!";
      echo $_POST['value'];
      }

After submitting the form I can't echo $userrating. However if I alert that I do get the value displayed:

var value = alert($('#stars').rateit('value'));

As soon as I click on submit button I get the alert with the value and after that the Posted! code appears. Could somebody have any idea what might be wrong in this code? Am I missing something? How can I assign the value of var value = $('#stars').rateit('value') into my $userrating?

Upvotes: 1

Views: 868

Answers (2)

Erik Flitman
Erik Flitman

Reputation: 163

you need to actually set the variable in your code. The way you have it there now, you are setting the value: "value". Change it to (without quotes):

var value = $('#stars').rateit('value');
$.ajax({
    url: 'itemreview.php',
    type: "POST",
    data: {value: value},
    success : function(data) {
    }

Upvotes: 0

Satender K
Satender K

Reputation: 581

replace

data: {value:"value"},

with

data: {"value":value},

Upvotes: 2

Related Questions