Javascript 如何从ajax返回两个html内容?
我正在用PHP开发一个购物车应用程序,我有两个功能,第一个功能是显示购物车中的所有商品,第二个功能是列出购物车中的所有商品 如何从AJAX返回这两个函数的html输出 以下是php服务器端:Javascript 如何从ajax返回两个html内容?,javascript,php,jquery,html,ajax,Javascript,Php,Jquery,Html,Ajax,我正在用PHP开发一个购物车应用程序,我有两个功能,第一个功能是显示购物车中的所有商品,第二个功能是列出购物车中的所有商品 如何从AJAX返回这两个函数的html输出 以下是php服务器端: <?php require '../init.php'; require $ROOT . '/functions/basic.php'; require $ROOT . '/functions/products.php'; if(isset($_REQUEST['id'])) { $pro
<?php
require '../init.php';
require $ROOT . '/functions/basic.php';
require $ROOT . '/functions/products.php';
if(isset($_REQUEST['id']))
{
$product_id = $_REQUEST['id'];
$_SESSION['cart'][$product_id] = isset($_SESSION['cart'][$product_id]) ? $_SESSION['cart'][$product_id] : "";
if($product_id && !productExists($product_id)) {
die("Error. Product Doesn't Exist");
}
$qty = dbRow("SELECT id, quantity FROM products WHERE id = $product_id");
$quantity = $qty['quantity'];
if($_SESSION['cart'][$product_id] < $quantity)
{
$_SESSION['cart'][$product_id] += 1; //add one to the quantity of the product with id $product_id
}
}
function writeCartTotal()
{
echo '
<div class="col-md-4">
<a href="carts"><h1><span class="glyphicon glyphicon-shopping-cart"></span>
';
if(isset($_SESSION['cart']))
{
$cart_count=count($_SESSION['cart']);
echo $cart_count;
}
else{
echo "0";
}
echo '
items
</h1></a>
</div>
<div class="col-md-4">
<h1>
';
if(!empty($_SESSION['cart'])) { //if the cart isn't empty
$total = "";
$total_score = "";
//iterate through the cart, the $product_id is the key and $quantity is the value
foreach($_SESSION['cart'] as $product_id => $quantity) {
//get the name, description and price from the database - this will depend on your database implementation.
//use sprintf to make sure that $product_id is inserted into the query as a number - to prevent SQL injection
$sql = dbQuery("SELECT product_title, product_description, price, product_score FROM products WHERE id = " . $product_id);
$count = $sql->rowCount();
//Only display the row if there is a product (though there should always be as we have already checked)
if($count > 0) {
list($name, $description, $price, $score) = $sql->fetch(PDO::FETCH_NUM);
$line_cost = $price * $quantity; //work out the line cost
$line_score = $score * $quantity;
$total = $total + $line_cost; //add to the total cost
$total_score = $total_score + $line_score;
}
}
//show the total
echo '<span class="label label-success">'. number_format($total) . " <span> ₭</span></span>";
echo '
</h1>
</div>
<div class="col-md-4">
<h1>
';
echo '<span class="label label-success">'. number_format($total_score) . " <span > PG</span></span>";
}else{
//otherwise tell the user they have no items in their cart
echo "0";
}
echo '
</h1>
</div>
';
}
function writeCartSummary()
{
echo '<h1>Welcome to Your Cart</h1>';
if(!empty($_SESSION['cart'])) { //if the cart isn't empty
$total = "";
$total_score = "";
//show the cart
echo "<table class=\"table table-hovered table-bordered\" border=\"1\" padding=\"3\" width=\"40%\">"; //format the cart using a HTML table
echo '<tr>
<th align="center">Product Name</th>
<th align="center">Quantity</th>
<th align="center">Price</th>
<th align="center">Score</th>
<th align="center">Delete</th>
</tr>';
//iterate through the cart, the $product_id is the key and $quantity is the value
foreach($_SESSION['cart'] as $product_id => $quantity) {
//get the name, description and price from the database - this will depend on your database implementation.
//use sprintf to make sure that $product_id is inserted into the query as a number - to prevent SQL injection
$sql = dbQuery("SELECT product_title, product_description, price, product_score FROM products WHERE id = " . $product_id);
$count = $sql->rowCount();
//Only display the row if there is a product (though there should always be as we have already checked)
if($count > 0) {
list($name, $description, $price, $score) = $sql->fetch(PDO::FETCH_NUM);
$line_cost = $price * $quantity; //work out the line cost
$line_score = $score * $quantity;
$total = $total + $line_cost; //add to the total cost
$total_score = $total_score + $line_score;
echo "<tr>";
//show this information in table cells
echo "<td align=\"center\">$name</td>";
//along with a 'remove' link next to the quantity - which links to this page, but with an action of remove, and the id of the current product
echo "<td align=\"center\">$quantity</td>";
echo "<td align=\"center\"><h4>". number_format($line_cost) . " <span class='label label-success'> ₭</span></h4></td>";
echo "<td align=\"center\"><h4>". number_format($line_score) . " <span class='label label-success'> PG</span></h4></td>";
echo '<td><a href="?action=delete&id='.$product_id.'" class="btn btn-danger btn-sm">Delete</a>';
echo "</tr>";
}
}
//show the total
echo "<tr>";
echo "<td colspan=\"2\" align=\"right\"><h1>Total</h1></td>";
echo "<td align=\"right\"><h1>". number_format($total) . " <span class='label label-success'> ₭</span></h1></td>";
echo "<td align=\"right\"><h1>". number_format($total_score) . " <span class='label label-success'> PG</span></h1></td>";
echo "</tr>";
//show the empty cart link - which links to this page, but with an action of empty. A simple bit of javascript in the onlick event of the link asks the user for confirmation
echo "</table>";
}else{
//otherwise tell the user they have no items in their cart
echo "<div class='well'><h3>You have no items in your shopping cart.</h3></div>";
}
}
$value = array("response"=>"OK","totals"=>writeCartTotal(), "summaries"=>writeCartSummary());
echo json_encode($value);
?>
您是在回显html,而不是将其存储在字符串中。要解决此问题,最快的方法是将echo输出重定向到php变量:
ob_start();
writeCartSummary();
$cartSummary = ob_get_clean();
ob_start();
writeCartTotal();
$cartTotal = ob_get_clean();
$value = array("response"=>"OK","totals"=>$cartTotal , "summaries"=>$cartSummary );
echo json_encode($value);
使用ob\u start
和ob\u get\u clean
可以将回波从标准输出缓冲区重定向到变量。这样,您就可以将变量内容放入json数据中
此外,使用json数据作为响应,您需要在ajax调用中使用dataType:'json'
:
$.ajax({
/* ... */
dataType: "json",
/* ... */
});
无论在php文件中,您将在成功函数(数据)中得到什么,数据都包含您在ajax调用文件中的echo值,您可以使用它。您不应该在函数中进行echo,而是将它添加到一个变量并返回该值。@jeroen您的意思是为这两个函数声明一个变量,而不是返回函数的名称吗?不,您不应该在函数中回显html,而应该使用该html构建一个字符串,并从函数中返回该字符串。请注意,@MarioA的解决方案也会起作用,但这是一种围绕问题而不是解决问题的方法。你能举一些例子吗?因为如果我只回显只返回一个函数,那么它工作得很好,但我需要两个,因为我需要刷新两个html内容。我尝试了你的代码,但仍然收到错误警报,这是由AJAX的条件引起的吗?你需要在AJAX调用中使用数据类型:“json”
,而不是html
。如果仍然存在错误,请尝试使用javascript控制台进行调试。哇。。。谢谢,更改数据类型:“json”正如您所提到的,现在可以完美工作了!非常感谢你的帮助。
$.ajax({
/* ... */
dataType: "json",
/* ... */
});