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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器