通过调用PHP脚本从Javascript更新数据库 [英] Updating Database from Javascript by calling PHP script

查看:73
本文介绍了通过调用PHP脚本从Javascript更新数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力争取年龄



我正在构建一个有活动墙的网站。除了像按钮和按钮之外,我有整个工作。我有他们目前只显示我喜欢的文本框或我不喜欢

 < a href ='#'onclick = '喜欢()' >像< / A> 

 < a href ='#'onclick ='different()'>不像< / a> 

现在这些调用这些脚本

 <脚本> 
函数像()
{
alert(我喜欢); $< / a> | 1生命< hr />;与< / a>的不同之处在于
document.getElementById(p1)。innerHTML =< a href ='#'onclick =
}
函数不同于()
{
alert(不喜欢了);
document.getElementById(p1)。innerHTML =< a href ='#'onclick ='like();'>与< / a> | 0 Lifes< hr />;
}
< / script>

我有一个连接到数据库的php脚本,并添加或删除了我所知道的工作。我不需要返回任何JavaScript的,但我怎么称之为php脚本给出postID和用户名??






<好的,所以使用这个我已经修改了一下。但它仍然不起作用:S



== LikeUnlink.php ==

 <?php 
包括'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'AND ActivityID = $ postID;
}
$ result = mysql_query($ query)或die(mysql_error());

== MembersHome.php ==

 < script type =text / javascript> 
函数进程(LikeId,Username,currentLikes,likeUnlike)
{
//您的验证码
$ .ajax(
{
类型:'GET ',
url:LikeUnlike.php,//文件类似数据库中的状态变化
data:{like:LikeId,username:用户名,模式:likeUnlike},
success:function(数据)
{
alert('It Works');
}
});
}
< / script>

== Like link ==

 < a href ='Javascript:void(0)'onclick ='process(。$ row ['ID']。,\。$ username。\ ,。likes。,0);'> $ linkText< / a> 


解决方案

试试这个

 < script type =text / javascript> 
函数过程(LikeId){
//您的验证码
$ .ajax({
类型:'POST',
url:LikeUnlike.php,//文件类似于数据库中的状态变化
data:{like:LikeId},
success:function(data){
//成功完成后要执行的代码
}
});
}
< / script>

在html中进行更改

 < a href ='Javascript:void(0)'onclick ='process(1)'>与< / a> 
< a href ='Javascript:void(0)'onclick ='process(0)'>与< / a>


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>

解决方案

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>

这篇关于通过调用PHP脚本从Javascript更新数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆