2010-10-27 101 views
1

大家好,我对编程相当陌生。我一直在教自己的PHP和CSS,并试图将它与一些jQuery结合起来。使用jQuery/PHP/mySQL更新数据库并显示新结果

我有一个网站,其中包含我希望允许用户投票的项目列表。 (www.blueskycouncil.com你可以/日志在堆栈本)

目前我通过发送此更新数据库:

<a href=\"idea_karma.php?pagetype=index&karmatype=ideaspace&id={$row['id']}&sort_id=$sort_id\"> 

到idea_karma.php,检查用户是否已经投票就可以了如果不更新数据库。

这工作正常(小白代码除外)

现在我想用jQuery做到这一点,而不是让我只更新了点,而不是整个页面。

两个变量是动态的(id和sort_id),具体值get的分配在一个循环中。

我的问题是如何处理这个问题?

我尝试了几件事情,但我似乎无法让他们工作。

这是sript

<script type="text/javascript"> 
function get() { 
$.get('idea_karma.php', {"pagetype": index, "karmatype": ideaspace, "id": id, "sort_id": sortid}, 

function (output) { 
    $('#karma').html(output).show(); 
    }}; 

} 

这是我做的调用脚本

<div class=\"karma-btn\"> 
<a href=\"idea_karma.php?pagetype=index&karmatype=ideaspace&id= {$row['id']}&sort_id=$sort_id\" onClick=\"get();\"> 
<img src=\"images/btn_lrg_karma.png\" alt=\"Alternative text\"> 
<span class=\"voted\"><div id="karma">{$row['karma']</div>}</span></a>              </div> 
+1

向我们展示您的尝试。发布一些代码。 – Cfreak 2010-10-27 17:12:42

+0

请记住,使用JavaScript来显示操作仍然需要一个php(或其他服务器端)中介来实现这些更改。 – 2010-10-27 17:24:06

+0

有一些问题......对于你的get()函数,变量'id'和'sortid'不存在。另外,'index'和'ideaspace'是字符串,你需要引号。看到我下面的例子... – 2010-10-27 17:31:09

回答

4

您需要做的是更改该链接,以便在点击它时调用带有正确参数的javascript函数。 JavaScript函数会做AJAX,并且是这个样子:

function updateKarma(id, sortId){ 
    $.post("idea_karma.php", 
    {pagetype: "index", karmatype: "ideaspace", id: id, sort_id: sortId}, 
    function(){ 
    //in here you can do you stuff when the call returns 
    alert("karma updated"); 
    }); 
} 

然后,你的链接代码如下:

<a href=\"javascript:void(0);\" onclick=\"updateKarma('{$row['id']}', '$sort_id')\"> 

我觉得这是你以后在做什么?

+0

你为什么要添加斜杠? – RobertPitt 2010-10-27 17:28:33

+0

这正是我之后的事情。它的工作,thnx很多。 – ThomPete 2010-10-27 17:29:06

+0

我迟迟不能提供与您一样的解决方案。 :) – Alpesh 2010-10-27 17:30:28

0

看看jQuery的API为AjaxGetPost。如果您需要使用已经编写的代码的特定帮助,我会建议发布它,并且我们可以帮助您排除故障。

2

您可以分配一个onclick功能锚如下 -

<a href="#nodo" onclick="update_vote('<?=$row['id']?>','<?=$sort_id?>')">vote</a> 

然后你就可以在你的js文件下面写一个函数 -

function update_vote(id,sort_id){ 


//Now you can call the url you were calling through the of the jquery. 

// You can update spection section in page in the `sucess` callback function of ajax. 

} 

阿贾克斯的详细文档可在 - http://api.jquery.com/jQuery.ajax/

2

以下是让你用ajax去的基本思路:

function doKarma(id, sort_id) { 
    var keyValue = { 
     'method' : 'doKarma', 
     'pageType' : 'index', 
     'karmaType' : 'ideaspace', 
     'id' : id, 
     'sort_id' : sort_id 
    };  
    $.post('/idea_karma.php', keyValue, function(xml) { 
     var stat = $(xml).find('rsp').attr('stat'); 
     // stat will represent whether your api call failed or was a success 
     if (stat === 'ok') { 
      // update your counter 
      alert('success'); 
     } 
     else { 
      // user has already voted or something went wrong 
      alert('failed'); 
     } 
    },'xml'); 
} 

这是你的idea_karma的一个例子。php页面

<?php 

if ($_POST['method'] == 'doKarma') { 
    //perform update of karma 

    header("Content-type: text/xml"); 
    // if success 
    echo '<rsp stat=\"ok\"></rsp>'; 
    // else echo '<rsp stat=\"error\"></rsp>'; 
} 

?> 
+0

thnx。有人打你“正确”的答案,但所有的答案都非常感谢。 – ThomPete 2010-10-27 17:41:38

相关问题