[英]PHP json_encode data to Jquery
I wrote a script in post.php for deleting table rows in shopping-cart. 我在post.php中编写了一个脚本,用于删除购物车中的表行。 I'm sending data with jquery post to a php file. 我将带有jquery帖子的数据发送到php文件。 Here is my php file. 这是我的PHP文件。 I created array for outputs and i convert to json with json_encode function. 我创建了输出数组,并使用json_encode函数转换为json。
//Sepetteki Ürünleri Siliyoruz
if(isset($_POST['deleteditems']) && isset($_SESSION['userid'])) {
$deleted_items = $_POST['deleteditems'];
//echo $_POST['deleteditems'];
$sql = "DELETE FROM basket WHERE productid IN ($deleted_items) AND userid = ".$_SESSION['userid'];
//echo $sql;
$query = mysql_query($sql);
$basket_array['queryresult'] = ($query) ? "<i class=\"icon-ok\"></i> Silindi" : "<i class=\"icon-remove\"></i> Hata: Silinemedi";
$basket_sql = "SELECT p.productid, p.wholesaleprice, p.minquantity, p.vat, b.quantity
FROM basket AS b, product AS p
WHERE b.productid = p.productid AND b.userid = ".$_SESSION['userid'];
$basket_query = mysql_query($basket_sql);
$num = mysql_num_rows($basket_query);
if ($num > 0) {
while ($row = mysql_fetch_array($basket_query)) {
$wholesaleprice = round($row['wholesaleprice']/(1+$row['vat']/100),2);
$total_quantity = $row['quantity']*$row['minquantity'];
$sumnovat[] = $total_quantity * $wholesaleprice;
$vats[] = array($row['vat'], round(($row['vat']/100)* $total_quantity * $wholesaleprice,2)); // KDV'yi hesaplıyoruz
}
foreach ($vats as $vat) {
$group_by_ratio_vat_sum[] = $vat[0];
}
$group_by_ratio_vat_sum = array_unique($group_by_ratio_vat_sum);
$group_by_ratio_vat_sum = array_fill_keys($group_by_ratio_vat_sum,0);
foreach ( $vats as $vat ) {
$number = str_replace( ",", ".", $vat[1] );
$group_by_ratio_vat_sum[ $vat[0] ] += (float)$number;
}
$total_vat = 0;
$basket_array['tfoot'] = '<tr class="basket_totals"><td colspan="5" class="basketresulttitle">'._('Ara Toplam').'</td><td colspan="3">'.number_format($sumnovat = array_sum($sumnovat),2).' TL</td></tr>';
foreach ($group_by_ratio_vat_sum as $vat_ratio => $vat_total) {
$basket_array['tfoot'] .= "<tr class=\"basket_totals\"><td colspan=\"5\" class=\"basketresulttitle\">"._('KDV')." (%$vat_ratio)</td><td colspan=\"3\">".number_format($vat_total,2)." TL</td></tr>";
$total_vat += $vat_total;
}
$basket_array['tfoot'] .= '<tr class="basket_totals"><td colspan="5" class="basketresulttitle">'._('Genel Toplam').'</td><td colspan="3">'.number_format($sumnovat + $total_vat,2).' TL</td></tr>';
json_encode($basket_array);
}
}
And here is my jquery code. 这是我的jquery代码。 I want to use json object in my jquery script. 我想在我的jquery脚本中使用json对象。 But i couldn't do that. 但是我做不到。 Because i'm rookie for json-jquery-php relation. 因为我是json-jquery-php关系的菜鸟。 Can you help me ? 你能帮助我吗 ?
$('#basket_delete_button').live('click',function(){
var loading = '<img src="<?php echo URL; ?>images/style/loading.gif" width="16" height="16">';
$('.loading').html(loading);
var deleteditems = $('tbody tr input:checkbox[name="delete_basket[]"]:checked')
.map(function() { return $(this).val() })
.get()
.join(",");
$.post('post.php',{deleteditems: deleteditems},function(data){
var obj = JSON.parse(data);
$('tfoot').html(obj.tfoot);
$("tbody tr :checked").each(function() {
$(this).parent().parent().remove()
});
var rowCount = $('.basket_products_row').length;
if (rowCount == 0){
$('#basket_area').html("<?php echo $basket_error; ?>");
$('#basket_count').text('0');
} else {
$('.loading').html(obj.queryresult);
}
});
});
I can't handle jquery json php relation. 我无法处理jquery json php关系。 Can you help me ? 你能帮助我吗 ? I couldn't manage it. 我无法处理。
The main issue I see right off the top is that you are encoding your data in PHP, but you are not storing or transmitting that encoded data: 我直接从顶部看到的主要问题是,您正在使用PHP编码数据,但没有存储或传输该编码数据:
json_encode($basket_array);
That call encodes and returns, but you don't do anything with the return. 该调用会编码并返回,但是您对返回不执行任何操作。 Try: 尝试:
echo json_encode($basket_array);
and see what that does for you. 看看有什么用。
Your client side code could certainly be better (as described in other answers which mention the 4th param to $.post()
) but I think your current method of JSON handling in the client will work once you start outputting the data. 您的客户端代码肯定会更好(如在其他答案中所提到的,该问题提到$.post()
的第4个参数),但是我认为,一旦开始输出数据,您当前在客户端中处理JSON的方法就可以使用。
Try this: 尝试这个:
On the server-side (PHP), put a json content type header before any output is send: 在服务器端(PHP)上,在发送任何输出之前放置一个json内容类型标头:
header('Content-type: application/json');
and write the output: 并写输出:
echo json_encode(...);
On the client-side (JS), force the $.post to read JSON data: 在客户端(JS)上,强制$ .post读取JSON数据:
$.post('someurl.php',
{foo:'bar'},
function(data){},
'json' // <--- here
);
You don't need to use JSON.parse()
here, jQuery does that for you. 您无需在这里使用JSON.parse()
,jQuery会为您完成。 You can use data
as an object. 您可以将data
用作对象。
After doing the $.post()
request, make a console.log(data)
to debug the output. 完成$.post()
请求后,创建console.log(data)
调试输出。 And use the browser's debug tools to watch the ajax request output. 并使用浏览器的调试工具来查看ajax请求的输出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.