0
我試圖從數據庫PHP插入追加結果使用echo json_encode($ results)。 我是,從JSON中獲取所有來自帖子的值,但追加不起作用。jQuery append不工作使用json_encode
這裏是jQuery的一面:
$('#postsubmit').click(function(){
error = 0;
// get form data
post = $('#comments').val();
topicname = $('#topicName').val();
topicid = $('#topicID').val();
pageid = $('#pageID').val();
page = $('#page').val();
sendmail = $('#sendmail').val();
galleria_pointer = $('#galleria_pointer').val();
showstatus = $('#showstatus').val();
$(this).attr('disabled','disabled');
if ($("#comments").val() == ''){
$("#comments").css({ "border": "1px solid red"});
$('.hideAdd').show();
$(this).attr('enabled','enabled');
}
$.ajax({
type: "POST",
url: "../foorumi/AjaxPost.php",
dataType: 'text',
data: "topicid="+topicid+"&pageid="+pageid+"&topicname="+topicname+"&page="+page+"&post="+encodeURIComponent(post)+"&sendmail="+sendmail+"&galleria_pointer="+galleria_pointer+
"&showstatus="+showstatus,
beforeSend : function(){
$("#loading_image").show();
$("body").css("cursor", "wait");
},
error : function(XMLHttpRequest, textStatus, errorThrown){
error = 1;
},
success : function(data) {
$("#loading_image").hide();
$("body").css("cursor", "default");
//alert(data);
dataobject = $.parseJSON(data);
var json_post_result = dataobject.post_result;
var json_error_code = dataobject.errors;
var json_topicname = dataobject.topicname;
var json_postid = dataobject.postid;
var json_topicid = dataobject.topicid;
var json_pageid = dataobject.pageid;
var url = dataobject.url;
var json_showstatus= dataobject.showstatus;
var json_post = dataobject.post;
var json_dateNow = dataobject.sentNow;
var json_galleria_pointer = dataobject.sentNow;
var json_data_html = dataobject.data_html;
var decoded = $("<div/>").html(json_data_html).text();
//alert(decoded);
if(json_post_result == true){
url_to = url;
//window.location.replace(url_to);
$(".hideAdd").fadeOut("fast",function(){
$('#ShowNew').show();
$(".divWrapperAppend").append(decoded);
});
}
}
});
});
而這裏的PHP端:
$user_ = ($_SESSION['users_id']);
$post_single = utf8_decode($_POST['post']);
$pageid = $_POST['pageid'];
$topicid_ = $_POST['topicid'];
$topicname = $_POST['topicname'];
$page = $_POST['page'];
$galleria_pointer = ($_POST['galleria_pointer']);
$sendmail = ($_POST['sendmail']);
$showstatus = ($_POST['showstatus']);
$microseconds_ = ((double) microtime()) * 1000000;
$results = array();
$errors = '';
$data = array();
$sqluserinfo = "select u.username as username, u.userlevel, (SELECT 1 FROM userphotos up WHERE up.user_= '$user_' and defaultphoto = '1') as photonum
,(SELECT count(1) FROM posts p1 WHERE u.users_id = p1.user_) as usermessagecount FROM users u WHERE u.users_id = '$user_'";
$sqlusersql = mysql_query($sqluserinfo);
$username = mysql_result($sqlusersql, 0, username);
$count_posts = mysql_result($sqlusersql, 0, usermessagecount);
$ulevel = mysql_result($sqlusersql, 0, userlevel);
$photonum = mysql_result($sqlusersql, 0, photonum);
$userSendMailSQL = "select *, (SELECT 1 from mail_topic where sendmail = '1' and user_ = '$user_' and theader_ = '$topicid_') as sendmail1 FROM mail_topic WHERE user_='$user_' and theader_ = '$topicid_' order by id";
$resultsqlquery = mysql_query($userSendMailSQL)or die(mysql_error());
$sendMailto = mysql_result($resultsqlquery, 0, sendmail1);
if ($post_single == "" || strlen(preg_replace('/\W/', '', $post_single)) < 5)
{
$errors = 1;
}
if (!empty($errors)){
$data['errors'] = $errors;
$data['post_success'] = false;
$results = array("errors"=>$errors, "post_result" => $data['post_success']);
}
else{
$data['post_success'] = true;
$new_post = htmlspecialchars($post_single, ENT_QUOTES | ENT_SUBSTITUTE, 'ISO-8859-1');
$insert="insert into posts (topicID, user_, Post, editdate_, updatedmicrosec) values ('$topicid_','$user_','$new_post','editdate_','$microseconds_')";
$timetoday = date("d.m.Y H:i:s");
$dateNow = date_diff_as_text($timetoday,"now") . " sitten";
//Insert data to db
mysql_query($insert);
$postid_ = mysql_insert_id();
if ($sendmail > 0){
sendmail($topicid_,$topicname,$user_,$postid_,$page);
}
updatelastread($topicid_);
$tableStyle_ajax = 'postStyleThisDay';
$tableStyleUser_ajax = 'postStyleThisDayUser';
$tableStyleNoBorder_ajax = 'postStyleThisDayNoBorder';
$styleBorder_ajax = 'style="min-height: 100%; border-right: 2px solid #87b1c8;"';
$aid_ajax = '<a name="'.$postid_.'"></a>';
$idcolor_ajax = 'color: #e82020; font-weight: bold;';
$borderbottom_ajax = 'border-bottom: 2px solid #e82020;';
$bordertop_ajax = 'border-top: 2px solid #e82020;';
if ($photonum > 0)
{
$photodiv = ('<div style="width: 89px; border: 1px solid #dadada; padding: 3px; margin: 0px 5px 5px 0px;"><a href="../kayttajat/index.php?id='.$user_.'&album=1&img='.$galleria_pointer.'"><img src="../profiili/userAlbums/'.$folder.'/'.$thumbnail.'"" style="width: 85px; height: 55px; border: none; padding: 2px;"/></a></div>');
}
$data_html = ('
<div id="'.$tableStyle_ajax.'" style="min-height: 30px; width: 885px; padding: 5px; margin-top: 5px; '.$bordertop_ajax.'>
<div style="padding: 8px 5px 3px 5px; min-height: 20px; border-bottom: 1px solid #dadada; background-color: #f7fdff;">
<div class="forumPostUser" style="float: left; width: 145px;"><a href="../kayttajat/index.php?id='.$user_.'>'.$username.'</a></div>
<div style="float: left; padding-left: 5px; width: 640px;">Lähetetty: '.$dateNow.'</div>
<div style="float: left; padding-left: 5px; '.$idcolor_ajax.'">ID: '.$postid_.'</div>
</div>
</div>
<div class="column" id="'.$tableStyleNoBorder_ajax.'" style="width: 130px; min-height: 65px; padding: 5px 5px 10px 5px;">
'.$photodiv.'
<div style="font-size: 11px;">Viestejä: '.$count_posts.' kpl</div>
<div style="font-size: 11px; padding-top: 3px;">Taso: '.$showstatus.'
</div>
</div>
<div class="column" id="'.$tableStyleNoBorder_ajax.'" style="min-width: 5px; min-height: 65px; padding: 5px 5px 10px 5px;">
<div> </div>
</div>
<div class="column" id="'.$tableStyleNoBorder_ajax.'" style="width: 730px; min-height: 65px; padding: 5px 5px 10px 5px;">
<span style="font-size: 16px; line-height: 130%;">'.$new_post.'</span>
</div>
<div id="'.$tableStyle_ajax.'" style="min-height: 30px; width: 885px; padding: 5px 5px 5px 5px; margin-top: 5px; '.$borderbottom_ajax.'">
<div style="padding: 8px 5px 3px 0px; min-height: 20px;">
<div style="font-size: 12px; width: 155px; padding-top: 20px; float: left;" class="forumPostUser"><a href="#alkuun">alkuun</a></div>
<div style="float: left; width: 300px; padding-top: 12px; border-top: 1px solid #dadada;"> </div>
<div style="float: left; width: 425px; padding-top: 5px; border-top: 1px solid #dadada;"><div style="float: left; margin-left: 240px;">
<form method="post" action="index.php?id='.$pageid.'&show='.$topicid_ .'&page='.$page.'#quoted"><input type="hidden" name="postID" value="'.$postid_.'" />
<input type="hidden" name="lainaa" value="Lainaa" />
<input type="image" src="../png/lainaa.png" /></form>
</div>
<div style="float: left;">
<form method="post" action="index.php?id='.$pageid.'&show='.$topicid_.'&page='.$page.'&postid='.$postid_.'&tag=#'.$postid_.'"><input type="hidden" name="postID" value="'.$postid_.'" /><input type="hidden" name="muokkaa" value="submit" />
<input type="hidden" name="edited" value="edited" />
<input type="image" src="../png/muokkaa.png" /></form></div>
</div>
</div>
<div style="clear: both;"></div>
</div>
</div>
<div style="clear: both;"></div>');
$data_html = htmlspecialchars($data_html, ENT_QUOTES | ENT_SUBSTITUTE, 'ISO-8859-1');
if (mysql_error() != ""){
echo ('<br><p><center><b><FONT SIZE="-1" FACE="Trebuchet MS,Arial,Helvetica">There was a MySQL Error -- '.mysql_error().'</b></center></p>');
exit;
}
$url = 'http://intra.tobiasfransman.net/foorumi/index.php?id='.$pageid.'&show='.$topicid_.'&page='.$page.'&postid='.$postid_.'&tag=#'.$postid_.'';
$results = array(
"data_html" => $data_html,
"url" => $url,
"post_result" => $data['post_success']
);
}
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');
//header('Content-type: application/x-www-form-urlencoded, charset=UTF-8');
echo json_encode($results);
這是我想追加$ data_html,但它不會追加。 任何人都可以看到我做錯了什麼?
控制檯日誌輸出中:
{"data_html":"\r\n <div id="postStyleThisDay" style="min-height: 30px; width: 885px; padding: 5px; margin-top: 5px; border-top: 2px solid #e82020;>\r\n <div style="padding: 8px 5px 3px 5px; min-height: 20px; border-bottom: 1px solid #dadada; background-color: #f7fdff;">\r\n <div class="forumPostUser" style="float: left; width: 145px;"><a href="..\/kayttajat\/index.php?id=0>Tobba<\/a><\/div>\r\n <div style="float: left; padding-left: 5px; width: 640px;">L&auml;hetetty:&nbsp; < 1 minuutti sitten<\/div>\r\n <div style="float: left; padding-left: 5px; color: #e82020; font-weight: bold;">ID: 0<\/div>\r\n <\/div>\r\n <\/div>\r\n <div class="column" id="postStyleThisDayNoBorder" style="width: 130px; min-height: 65px; padding: 5px 5px 10px 5px;">\r\n <div style="width: 89px; border: 1px solid #dadada; padding: 3px; margin: 0px 5px 5px 0px;"><a href="..\/kayttajat\/index.php?id=0&album=1&img=2"><img src="..\/profiili\/userAlbums\/\/"" style="width: 85px; height: 55px; border: none; padding: 2px;"\/><\/a><\/div>\r\n <div style="font-size: 11px;">Viestej&auml;: 1104 kpl<\/div>\r\n <div style="font-size: 11px; padding-top: 3px;">Taso:&nbsp;<span>LIFE.IS.A..<\/span>\r\n <\/div>\r\n <\/div>\r\n <div class="column" id="postStyleThisDayNoBorder" style="min-width: 5px; min-height: 65px; padding: 5px 5px 10px 5px;">\r\n <div>&nbsp;<\/div>\r\n <\/div>\r\n <div class="column" id="postStyleThisDayNoBorder" style="width: 730px; min-height: 65px; padding: 5px 5px 10px 5px;">\r\n <span style="font-size: 16px; line-height: 130%;">Testing testing<\/span>\r\n <\/div>\r\n <div id="postStyleThisDay" style="min-height: 30px; width: 885px; padding: 5px 5px 5px 5px; margin-top: 5px; border-bottom: 2px solid #e82020;">\r\n <div style="padding: 8px 5px 3px 0px; min-height: 20px;">\r\n <div style="font-size: 12px; width: 155px; padding-top: 20px; float: left;" class="forumPostUser"><a href="#alkuun">alkuun<\/a><\/div>\r\n <div style="float: left; width: 300px; padding-top: 12px; border-top: 1px solid #dadada;">&nbsp;<\/div>\r\n <div style="float: left; width: 425px; padding-top: 5px; border-top: 1px solid #dadada;"><div style="float: left; margin-left: 240px;">\r\n <form method="post" action="index.php?id=1&show=41&page=9#quoted"><input type="hidden" name="postID" value="0" \/>\r\n <input type="hidden" name="lainaa" value="Lainaa" \/>\r\n <input type="image" src="..\/png\/lainaa.png" \/><\/form>\r\n <\/div>\r\n <div style="float: left;">\r\n <form method="post" action="index.php?id=1&show=41&page=9&postid=0&tag=#0"><input type="hidden" name="postID" value="0" \/><input type="hidden" name="muokkaa" value="submit" \/>\r\n <input type="hidden" name="edited" value="edited" \/>\r\n <input type="image" src="..\/png\/muokkaa.png" \/><\/form><\/div>\r\n <\/div>\r\n <\/div>\r\n <div style="clear: both;"><\/div>\r\n\r\n <\/div>\r\n <\/div>\r\n <div style="clear: both;"><\/div>","url":"http:\/\/intra.tobiasfransman.net\/foorumi\/index.php?id=1&show=41&page=9&postid=0&tag=#0","post_result":true,"errors":null}
如果你正在返回JSON,設置'dataType'爲 'JSON',並擺脫'dataobject = $ .parseJSON(data);' – Mikey 2014-12-03 08:06:55
請console.log(data);並在這裏粘貼結果 – MixedVeg 2014-12-03 08:14:18
控制檯日誌更新 – user2023042 2014-12-03 08:28:41