dsimib1625 2013-05-01 06:07
浏览 45
已采纳

通过调用PHP脚本从Javascript更新数据库

Ive been trying to get this for age

I am building a website which has an activity wall. I have the whole thing working except the like and unlike buttons. I have them currently just showing a text box I like or I don't like

<a href='#' onclick='like()'>Like</a>

or

<a href='#' onclick='unlike()'>Unlike</a>

Now these call these scripts

<script>
    function like()
    {
        alert("I like");
        document.getElementById("p1").innerHTML="<a href='#' onclick='unlike();'>Unlike</a> | 1 Life<hr/>";
    }
    function unlike()
    {
        alert("Dont like anymore");
        document.getElementById("p1").innerHTML="<a href='#' onclick='like();'>Like</a> | 0 Lifes<hr/>";
    }
</script>

I have a php script that connects to the database and adds or removes the like which I know works. I don't need to return anything to the javascript but how do I call that php script giving the postID and username??


Ok so using this I have modified it a little. But it still doesnt work :S

==LikeUnlink.php==

<?php
include 'phpScripts/OpenConnection.php';    
$mode = $_GET['mode'];
$username = $_GET['username'];
$postID = $_GET['LikeID'];
echo $username."<br/>";
echo $mode."<br/>";
echo $postID."<br/>";
if($mode == 0)
{
    $query = "INSERT INTO tbl1Ups (Username, ActivityID) VALUES ('$username', $postID)";    
}
else
{
    $query = "DELETE FROM `tbl1Ups` WHERE Username='$username' AND ActivityID=$postID";
}
$result = mysql_query($query) or die(mysql_error());

==MembersHome.php==

<script type="text/javascript">
function process(LikeId, Username, currentLikes, likeUnlike)
{
//your validation code
    $.ajax(
    {
    type: 'GET',
        url: LikeUnlike.php, //file where like unlike status change in database
    data:{like:LikeId, username:Username, mode:likeUnlike},
    success: function(data)
        {
            alert('It Works');
    }
    } );
}
</script>

==Like link==

<a href='Javascript:void(0)' onclick='process(".$row['ID'].", \"".$username."\", ".$likes.", 0);'>$linkText</a>
  • 写回答

1条回答 默认 最新

  • dsjbest2014 2013-05-01 07:00
    关注

    try this

    <script type="text/javascript">
    function process(LikeId) { 
    //your validation code
    $.ajax( {
            type: 'POST',
            url: LikeUnlike.php, //file where like unlike status change in database
            data:{like:LikeId},
            success: function(data) {
                //code you want to do after successfull process
            }
        } );
    }
    </script>
    

    make changes in html

    <a href='Javascript:void(0)' onclick='process(1)'>Like</a>
    <a href='Javascript:void(0)' onclick='process(0)'>Unlike</a>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有偿求码,CNN+LSTM实现单通道脑电信号EEG的睡眠分期评估
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路