JSON编码,ajax并在html中显示 [英] JSON encode, ajax and display back in html

查看:118
本文介绍了JSON编码,ajax并在html中显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要添加一些东西给我的ajax请求,以显示它减去1从>现在我的ajax只是加1的值,我需要它从相同的函数中减去1>我认为我需要用JSON来完成,但是我不知道如何将它融入到这个脚本中......如果有人能够告诉我这究竟会如何令人惊叹,我知道我的代码是sl as的,因为它和我一样。 ..

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({
类型:POST,
url:up.php,
data:dataString,
缓存:false,

成功:函数(数据){ $('#total_'+ parent.attr(id))。text(data); }
});

}
else
{

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

成功:函数(数据){$('#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与up.php相同,但值相反 $ b $如果你想返回JSON编码的数据,使用 echo json_encode($ array); - json_encode



在javascript的结尾,如果你使用jQuery,使用 jQuery.parseJson()



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

编辑



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

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

data.php

 

code> $ data = new stdClass();
$ data-> name =John;
$ data->年龄= 24;
echo json_encode($ data);

这将返回json,如下所示:

  {name:John,age:24} 

这将解析到一个JavaScript对象。我已经在jsFiddle


中包含了示例数据

I need to add something to my ajax request to show it subtracting 1 from "> right now my ajax just adds 1 to the value of "> and I need it to subtract 1 from ">in the same function. I think I need to do it with JSON but I have no idea how to incorporate that into this script... if somebody could show me exactly how that would be amazing. 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); }

});


}

});

here is 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.php just with opposite values

解决方案

If you would like to return JSON encoded data, use echo json_encode($array); - json_encode

On the javascript end of things, if you're using jQuery, use jQuery.parseJson()

If you're not using jQuery, you can use JSON.parse(xmlhttp.responseText)

EDIT

OP asked for a example of json_parse in action. http://jsfiddle.net/mxaP6/1/ Is a working example of parsing JSON in jQuery. In a real application, you would replace the string in jQuery.parseJSON() with the string returned from the server containing json data.

A better way to return data than sending an array in parse_json is to make an object, for example:

data.php

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

This would return json looking like this:

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

Which would parse to a javascript object. I've included the example data in the jsFiddle

这篇关于JSON编码,ajax并在html中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆