我需要添加一些東西給我的ajax請求顯示它減去1從「>現在我的ajax只增加1的值」>,我需要它從減去1但是我不知道如何將它加入到這個腳本中......如果有人能夠告訴我這究竟是多麼的令人驚歎,我知道我的代碼是sl as不馴的哈克....忍着我...JSON編碼,ajax和回顯在html
btw我知道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.php只是相對值
您將需要在JS接收端JSON.parse不要忘了:) – Sir 2013-02-22 04:52:49
沒錯 - 將增加 – 2013-02-22 04:53:18
那麼假設他是使用jQuery應該添加這個非的jQuery用戶:https://developer.mozilla.org/en/docs/JSON – Sir 2013-02-22 04:56:51