0

我需要在我的 ajax 请求中添加一些东西,以显示它从 "> 减去 1 现在我的 ajax 只是将 1 的值加到 "> 并且我需要它在同一个函数中从 "> 减去 1。我想我需要用 JSON 来做,但我不知道如何将它合并到这个脚本中......如果有人能告诉我这将是多么惊人。我知道我的代码是草率的......

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

通用.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 相同,只是值相反

4

1 回答 1

1

如果您想返回 JSON 编码的数据,请使用echo json_encode($array);- json_encode

在 javascript 方面,如果您使用的是 jQuery,请使用jQuery.parseJson()

如果你不使用 jQuery,你可以使用JSON.parse(xmlhttp.responseText)

编辑

OP 要求提供 json_parse 的示例。 http://jsfiddle.net/mxaP6/1/是一个在 jQuery 中解析 JSON 的工作示例。在实际应用程序中,您可以将 jQuery.parseJSON() 中的字符串替换为从服务器返回的包含 json 数据的字符串。

比在 parse_json 中发送数组更好的返回数据的方法是创建一个对象,例如:

数据.php

$data = new stdClass();
$data->name = "John";
$data->age = 24;
echo json_encode($data);

这将返回如下所示的 json:

{"name":"John","age":24}

这将解析为一个 javascript 对象。我已经在 jsFiddle 中包含了示例数据

于 2013-02-22T04:52:13.327 回答