通过php

时间:2016-09-08 14:53:32

标签: javascript php jquery mysql ajax

我怎样才能提交名为" currentRating"的Javascrip变量?在"回调中:"所以我可以通过PHP将该变量的数量输入到Mysql数据库中吗?

我有使用PHP运行的连接和数据库,我只是对Javascript一无所知,我使用的是使用Java和Ajax的评级系统



<?php
echo
"<script>
$(function() {
  $('.my-rating').starRating({
    initialRating: ". $current_rating .",
    callback: function(currentRating, element){
        window.alert('RATED')
    }
  });
}); 
</script>";?>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

你需要进行ajax调用,即调用一个监听currentRating变量的php页面。这个php页面,应该让mysql插入/更新。

请参阅:https://api.jquery.com/jquery.post/

所以,这是你的javascript函数的快速版本:

&#34;需要一些实施&#34;

   <?php
echo
"<script>
$(function() {
  $('.my-rating').starRating({
    initialRating: ". $current_rating .",
    callback: function(currentRating, element){
        window.alert('RATED');

        //AJAX CALL to page, passing currentRating

        $.ajax({
        type: 'post',
        url: 'post.php',
        data: currentRating,
        success: function () {
          alert('currentRating was saved');
        }
      });
    }
  });
}); 
</script>";?>

答案 1 :(得分:0)

您需要将当前值传递给脚本(通过GET或POST) 使用jQuery很容易

请参阅:https://api.jquery.com/jquery.post/

&#13;
&#13;
<?php
echo
"<script>
$(function() {
  $('.my-rating').starRating({
    initialRating: ". $current_rating .",
    callback: function(currentRating, element){
        window.alert('RATED');
        $.post( \"your_file_to_save.php\", { ratting : currentRating }, 
                     function( data ) {
                         alert( "RESPONSE : " + data );
                      }
        );
    }
  });
}); 
</script>";?>
&#13;
&#13;
&#13;