用一个ajax函数更改两个html id [英] change two html id's with one ajax function
问题描述
我需要在我的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。我知道我的代码是sl as的,因为....忍受我... ...
btw我知道ajax实际上并没有增加和减少任何东西,它是只是为客户展示它,只是不知道任何更好的方式来解释我的问题。
general.js
<点击(功能()
{
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,
成功:函数(数据){$('#total_'+ parent.attr(id )).text(data);}
});
}
else
{
$(this).fadeIn (200).html('');
$ .ajax({
type:POST,
url:down.php,
data:dataString,
缓存:fal se,
成功: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 =votename =updata-options =key1 =<?php echo $ mes_id1;?>& key2 =<?php echo $ mes_id2;?>& key3 =< php echo $ totalvotes1;?>& key4 =<?php echo $ totalvotes2;?>> < img src =up.pngalt =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 =votename =downdata-options =key1 =<?php echo $ mes_id1;?>& key2 =<?php echo $ mes_id2;?>>< img src =down.pngalt =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(从Voting_IP中选择ip_add,其中mes_id_fk ='$ mes_id1'和ip_add ='$ ip');
$ count = mysql_num_rows($ ip_sql);
$ ip_sql2 = mysql_query(从Voting_IP中选择ip_add,其中mes_id_fk ='$ mes_id2'和ip_add ='$ ip');
$ count2 = mysql_num_rows($ ip_sql2);
//如果用户已经投票,如果($ count == 0&& $ count2!= 0)执行脚本
{
$ sql =update消息设置totalvotes = totalvotes + 1其中mes_id ='$ mes_id1';
mysql_query($ sql);
$ b $ sql_in =insert into Voting_IP(mes_id_fk,ip_add)values('$ mes_id1','$ ip');
mysql_query($ sql_in);
$ sql =update消息集totalvotes = totalvotes-1其中mes_id ='$ mes_id2';
mysql_query($ sql);
$ sql_in =删除Voting_IP WHERE mes_id_fk ='$ mes_id2';
mysql_query($ sql_in);
echo $ new_totalvotes1;
//如果用户没有投票,执行脚本
}
else if($ count == 0&& count2 == 0)
{
$ sql =update消息集合totalvotes = totalvotes + 1其中mes_id ='$ mes_id1';
mysql_query($ sql);
$ b $ sql_in =insert into Voting_IP(mes_id_fk,ip_add)values('$ mes_id1','$ ip');
mysql_query($ sql_in);
echo $ new_totalvotes1;
}
?>
down.php与刚才相反的值相同
<如果您在PHP中创建关联数组,您可以使用 json_encode
将其转换为JSON。就像这样:
//显然这将从您的数据库动态构建
$ d = array('Will '=> 10,'亨利'=> 12);
// echo as json
echo json_encode($ d);
然后在您的JavaScript中:
<$ p $
$。ajax({
type:POST,
url:down.php,
data:dataString,
cache:false ,
dataType:json,
成功:函数(数据){
//这里数据看起来像{Will:10,Henry:20} ;
//我将假设`total_will`是william
var候选人的总票数;
(数据候选人){
$(#total_ +候选人).text(data [candidate]);
}
// now #total_Will有文本10
//和#total_Henry有文本20
}
});
I need to add something to my ajax request to show it subtracting 1 from <span id="total_vote_down_<?php echo $mes_id2; ?>">
right now my ajax just adds 1 to the value of <span id="total_vote_up_<?php echo $mes_id1; ?>">
and I need it to subtract 1 from <span id="total_vote_up_<?php echo $mes_id1; ?>">
in the same function. I know my code is sloppy as heck.... bear with me...
btw I know the ajax isn't actually adding and subtracting anything and that it is just demonstrating it for the client, just didn't know any better way to phrase my problem
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>
here is 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 is the same as up just with opposite values
If you create an associative array in PHP, you can use json_encode
to convert it to JSON. Something like this:
// obviously this will be constructed dynamically from your database
$d = array('Will' => 10, 'Henry' => 12);
// echo as json
echo json_encode($d);
Then in your 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
}
});
这篇关于用一个ajax函数更改两个html id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!