评级插件。值0插入数据库

时间:2015-09-24 12:40:56

标签: php jquery ajax

我在我的网站(https://rateit.codeplex.com/)上使用了评级评分插件。 我试图将用户选择的值插入到我的数据库中,但每次执行此操作时都会插入值0。这是我的代码(PHP第一个):

session_start();
$username = $_SESSION['username'];

if(isset($_SESSION['username'])) {

$username = $_SESSION['username'];

}
 else {
header('Location: login.php');
}

$itemid = $_GET['id'];

(HTML):

<div id="itemreview">
    <form action="" name="review" id="review" method ="post">
    Skin Type: <select name="skintype">
        <option selected="">Skin Type</option> 
        <option value ="Oily">Oily</option>
        <option value ="Mixed">Mixed</option>
        <option value ="Sensitive">Sensitive</option>
        <option value ="Normal">Normal</option>
     </select><br>
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>
  <br>
 <textarea name="review" id="review" cols ="50" rows="8"></textarea>
  <input type="submit" name="submitcomment" id="submit"  value="Comment!">

    </form>

这是我的Jquery / Ajax:

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


              //Create a variable to hold the value entered by the user
              var stars = $('#stars').rateit('value');

              $.ajax({
                  url: 'itemreview.php',
                  type: "POST",
                  data: { "stars" : stars },
                  success: function (data) {
                      alert("Got it!"+ stars);
                  }
              });
          });
      });
  </script>    

这是我的php:

<?php 
//Insert review into database 

if(isset($_POST['submitcomment'])) {
$skintype = $_POST['skintype'];
$userrating = $_POST['stars'];
$userreview = $_POST['review'];

$insertreview = mysql_query("INSERT INTO itemcomments (itemid, username, commentcontent, ranking, userskintype) VALUE('$itemid', '$username',
           '$userreview', '$userranking', '$skintype')");

           echo "Posted!";
           echo $userrating;
}
?>

虽然我可以使用alert("Got it!"+ stars);显示值,但是当我单击提交时,我无法将正确的值插入到数据库中。 例如,我为7.5评分。点击提交后,我收到提醒信息&#34; Go it!7.5&#34;但是在数据库中,插入的值仍为0.所有其他值($ skintype,$ userreview,$ itemid和$ username)都已正确插入。 可能是什么问题?

谢谢大家的帮助。

1 个答案:

答案 0 :(得分:0)

<input type="range" value="0" step="0.5" id="rating">

我认为您需要将值更改为value =“”否则您将始终获得零值。试试吧!让我知道。如果这没有用,我会再次查看代码。