dongtan3306 2014-01-22 16:55
浏览 29
已采纳

用PHP脚本交流javascript

I am trying to send data from Javascript to a php script. I am new to PHP,javascript and JSON etc. I am tryin to use jQuery Upvote plugin. I am reading the documentation from readme.md here. However, I find it too vague for my understanding.

I have added the following code in HTML body. The plugin is working fine in front-end.

<div id="topic" class="upvote">
    <a class="upvote"></a>
    <span class="count">0</span>
    <a class="downvote"></a>
    <a class="star"></a>
</div>
<div id="output"> </div>
<script language="javascript">
var callback = function(data) {
    $.ajax({
        url: '/vote',
        type: 'post',
        data: { id: data.id, up: data.upvoted, down: data.downvoted, star: data.starred }
    });
};
$('#topic').upvote({id: 123, callback: callback});
</script>

Can someone please tell how should I write the php counterpart so as to get the information about change of state of plugin? I think it is related to json maybe but I am stuck here..

EDIT: As per the suggestion, I have modified a bit of code and wrote PHP script. In above code I have changed URL to my PHP script url: 'voter.php'.

This is the PHP code (in brief) :

<?php require_once('Connections/conn.php'); ?>
mysql_select_db($database_conn, $conn);
if ($_POST['up']!=0){
$query_categories = "UPDATE user SET score=1 WHERE u_id =5";
$categories = mysql_query($query_categories, $conn) or die(mysql_error());
}
else if ($_POST['down']!=0) {
    $query_categories = "UPDATE user SET score=-1 WHERE u_id =5";
$categories = mysql_query($query_categories, $conn) or die(mysql_error());

}

The database connection is working fine, but still the database values are not updated by above code..

  • 写回答

1条回答 默认 最新

  • dou9022 2014-01-22 17:00
    关注

    Your script sends AJAX Post request to the php script. You can access those post variables like this, using $_POST[] array:

    $id = $_POST['id'];
    $up = $_POST['up'];
    $down = $_POST['down'];
    $star = $_POST['star'];
    

    Then you can populate the mysql table, etc.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?