1

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?

2
  • How does this question anyway related to MySQL? Commented Sep 4, 2015 at 13:06
  • it should be // echo "Posted!"; no need return $_POST['value']; Commented Sep 4, 2015 at 13:09

2 Answers 2

2

replace

data: {value:"value"},

with

data: {"value":value},
Sign up to request clarification or add additional context in comments.

6 Comments

@davidb select it as your answer
Does it answer the question properly?
@Jai why not, it is correct as user's problem has been resolved
Sorry mate, I can as the problem is still occurring. When I send the data to the database I get a value of 0. I've tried your solution (data: {"value":value},) as well as the one below (without quotes) and it's still not working.
@davidb then problem is in this code also var value = $('#stars').rateit('value');
|
0

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) {
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.