添加一个“读取”按钮,它将增加进度

时间:2017-04-22 23:15:39

标签: php

我正在开展一个项目,并且会有一个页面供学生阅读内容。我的进度取决于他们阅读了多少内容,所以有人可以帮我实现“阅读”按钮,当点击/选中按钮时,它会将进度添加到他们的进度中。这也应该显示他们阅读的内容,谢谢。

1 个答案:

答案 0 :(得分:0)

以下是PHP和AJAX的一个简单示例:

HTML     

您好,请点击此按钮阅读:点击我

    

<!-- including jQuery from the google cdn -->
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>

<script type="text/javascript">
// This is our actual script
$(document).ready(function(){
    $('a#button').click(function(){
         $.ajax({
            type: "POST",
            data: {variable:"1"},
            url: "ajax.php",
              success: function(data) { $("#container").html(data) }
        });
    });
});
</script>

PHP - ajax.php

<?php

session_start(); // using session instead of database for example


$_SESSION['student_progress']++;

echo json_encode("You just marked " . $_POST['variable'] . " as readed and your progress was updated to ". $_SESSION['student_progress'] );