【问题标题】:change two html id's with one ajax function [duplicate]用一个ajax函数更改两个html id [重复]
【发布时间】:2013-02-07 09:24:10
【问题描述】:

我需要在我的 ajax 请求中添加一些内容,以显示它从 <span id="total_vote_down_<?php echo $mes_id2; ?>"> 中减去 1 现在我的 ajax 只是将 <span id="total_vote_up_<?php echo $mes_id1; ?>"> 的值加 1,我需要它从 <span id="total_vote_up_<?php echo $mes_id1; ?>"> 中减去 1 在同一个函数中.我知道我的代码太草率了......请耐心等待......

顺便说一句,我知道 ajax 实际上并没有添加和减去任何东西,它只是为客户演示它,只是不知道有什么更好的方式来表达我的问题

general.js

$(".vote").click(function() 
{

var id = $(this).attr("id");
var name = $(this).attr("name");
var eData = $(this).attr("data-options");
var dataString = 'id='+ id + '&' + eData ;
var parent = $(this);


if(name=='up')
{

$(this).fadeIn(200).html('');
$.ajax({
type: "POST",
url: "up.php",
data: dataString,
cache: false,

success: function(data) { $('#total_' + parent.attr("id")).text(data); }
});

}
else
{

$(this).fadeIn(200).html('');
$.ajax({
type: "POST",
url: "down.php",
data: dataString,
cache: false,

success: function(data) { $('#total_' + parent.attr("id")).text(data); }

});


}

});

index.php

<div id="main">
<div id="left">
<span class='up'><a title="vote_down_" id="vote_up_<?php echo $mes_id1; ?>" class="vote" name="up" data-options="key1=<?php echo $mes_id1;?>&key2=<?php echo $mes_id2;?>&key3=<?php echo $totalvotes1;?>&key4=<?php echo $totalvotes2;?>"> <img src="up.png" alt="Down" /></a></span><br />
<span id="total_vote_up_<?php echo $mes_id1; ?>"><?php echo $totalvotes1; ?></span><br />
</div>
<div id="message">
    <?php echo $message1; ?>
</div>
<div class="clearfix"></div>
</div>
<div id="main">
<div id="right">
<br />
<span id="total_vote_down_<?php echo $mes_id2; ?>"><?php echo $totalvotes2; ?></span><br />
<span class='down'><a id="vote_down_<?php echo $mes_id2; ?>" class="vote" name="down" data-options="key1=<?php echo $mes_id1;?>&key2=<?php echo $mes_id2;?>"><img src="down.png" alt="Down" /></a></span>
</div>
<div id="message">
    <?php echo $message2; ?>
</div>
<div class="clearfix"></div>
</div>

这是 up.php

<?php

session_start();
include("config.php");

$ip=$_SERVER['REMOTE_ADDR']; 

$mes_id1 = $_POST['key1'];
$mes_id2 = $_POST['key2'];
$totalvotes1 = $_POST['key3'];
$totalvotes2 = $_POST['key4'];
$new_totalvotes1 = $totalvotes1 + 1;
$new_totalvotes2 = $totalvotes2 - 1;

$ip_sql=mysql_query("select ip_add from Voting_IP where mes_id_fk='$mes_id1' and ip_add='$ip'");
$count=mysql_num_rows($ip_sql);

$ip_sql2=mysql_query("select ip_add from Voting_IP where mes_id_fk='$mes_id2' and ip_add='$ip'");
$count2=mysql_num_rows($ip_sql2);


// if the user has already voted, execute script
if($count==0 && $count2!=0)
{
$sql = "update Messages set totalvotes=totalvotes+1  where mes_id='$mes_id1'";
mysql_query( $sql);

$sql_in = "insert into Voting_IP (mes_id_fk,ip_add) values ('$mes_id1','$ip')";
mysql_query( $sql_in);

$sql = "update Messages set totalvotes=totalvotes-1  where mes_id='$mes_id2'";
mysql_query( $sql);

$sql_in = "DELETE FROM Voting_IP WHERE mes_id_fk='$mes_id2'";
mysql_query( $sql_in);

echo $new_totalvotes1;

// if the user has not voted, execute script
}
else if($count==0 && count2==0)
{
$sql = "update Messages set totalvotes=totalvotes+1  where mes_id='$mes_id1'";
mysql_query( $sql);

$sql_in = "insert into Voting_IP (mes_id_fk,ip_add) values ('$mes_id1','$ip')";
mysql_query( $sql_in);

echo $new_totalvotes1;

}
?>

down.php 与 up 相同,只是值相反

【问题讨论】:

  • 所以看起来你的up.php(并且,大概是down.php)返回了点击的一项的当前投票数,并且你有一个依赖关系,当其中一项被点击时,您也需要更改向下计数(大概是当有人之前投票否决并改变主意时)。也许您最好的方法是让您的 PHP 函数为每个影响项返回带有 idvotes 的 JSON 响应,并让您的 AJAX 成功函数解析它?
  • 完全正确!!!好在我不知道该怎么做,哈哈,你能帮我在这里实现一些 JSON 吗......?非常感谢@MattBurland
  • 使用json_encode。您可以在 PHP 中创建一个关联数组并使用json_encode 回显它。然后,如果您将 AJAX 调用设置为 json,jQuery 会为您将其转换为 JavaScript 对象。
  • 顺便说一句,以防万一它对我正在尝试做的事情带来任何澄清,我的代码的上下方面将真正归功于 Will 和 Henry,lol 用户将会能够投票给他们认为“赢得”他们的每场播客辩论的人......这个网站是给我兄弟的......我做这一切都是免费的,但我想为他做一份杀手锏
  • @MattBurland 所以...我把$array1 = array($totalvotes1, $totalvotes2); print_r($array1); 放在我的up.php 文件中...据我所知...我在哪里/如何用json_encode 回显它...?

标签: php javascript html ajax json


【解决方案1】:

如果您在 PHP 中创建关联数组,您可以使用 json_encode 将其转换为 JSON。像这样的:

// obviously this will be constructed dynamically from your database
$d = array('Will' => 10, 'Henry' => 12);
// echo as json
echo json_encode($d);

然后在你的 JavaScript 中:

$.ajax({
    type: "POST",
    url: "down.php",
    data: dataString,
    cache: false,
    dataType: "json",

    success: function(data) { 
        // here data will look like { "Will":10, "Henry": 20 };
        // I'm going to assume `total_will` is the total votes for william
        var candidate;
        for (candidate in data) {
            $("#total_" + candidate).text(data[candidate]);
        }
        // now #total_Will has the text 10
        // and #total_Henry has the text 20
    }
});

【讨论】:

  • javascript 怎么知道 for(candidate in data) 是什么?
  • @PatrickLawler:它会知道您是否在 HTML 中设置了 id,以便您可以根据收到的数据轻松构建它。
猜你喜欢
  • 1970-01-01
  • 2017-05-24
  • 1970-01-01
  • 2018-07-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-18
  • 2014-02-15
相关资源
最近更新 更多