我有php代码和一些html我希望我的所有html都包含在echo("")中,用于ajax响应。 请帮忙
请帮助
这是我的代码
<?php
if (isset($_POST['getcart'])){
$cart_item_query="SELECT * FROM cart";
$cart_item_run = mysqli_query($conn,$cart_item_query);
if (mysqli_num_rows($cart_item_run)>0){
$grand_total=0;
while($cart_row=mysqli_fetch_array($cart_item_run)){
$pro_id = $cart_row['product_id'];
$pro_qty = $cart_row['qty'];
$product_query="SELECT * FROM products WHERE product_id = '$pro_id'";
$product_run = mysqli_query($conn, $product_query);
$product_row = mysqli_fetch_array($product_run);
$pro_name = $product_row['product_name'];
$pro_image = $product_row['product_image'];
$pro_sell_price = $product_row['sell_price'];
$total = $pro_sell_price*$pro_qty;
$grand_total =$grand_total+ $total;
?>
<div class='row'>
<div class="col-md-3 col-xs-3"><img src="img/<?php echo $pro_image; ?>" alt="" width="50px"></div>
<div class="col-md-3 col-xs-3"><p><?php echo $pro_name; ?></p><a href=""><p><i class="fa fa-trash" aria-hidden="true"></i> Remove</p></a></div>
<div class="col-md-2 col-xs-2">
<select class="change_qty" size="1" style="width:50px;">
<?
for ($i = 1; $i <= 9; $i++) {
$selected = $pro_qty == $i? 'selected' : '';
echo "<option class='qty' data-pro='{$pro_id}' value='{$i}' {$selected}>{$i}</option>";
}
?>
</select>
</div>
<div class="col-md-2 col-xs-2"><input data-pro="<?php echo $pro_id; ?>" id="price-<?php echo $pro_id; ?>" type="text" value="<?php echo $pro_sell_price; ?>" disabled class="form-control dis-input price"></div>
<div class="col-md-2 col-xs-2"><input id="total-<?php echo $pro_id; ?>" data-pro="<?php echo $pro_id; ?>" type="text" value="<?php echo $total; ?>" disabled class="form-control dis-input total"></div>
</div>
<hr>
<?
}
}
}
?>
答案 0 :(得分:1)
只需将输出保存在php var中,并在完成后回显它。如果您只想在某些DOM中将其输出为html,则无需执行此操作(如上所述)。 `
$cart_item_query="SELECT * FROM cart";
$cart_item_run = mysqli_query($conn,$cart_item_query);
if (mysqli_num_rows($cart_item_run)>0){
$grand_total=0;
while($cart_row=mysqli_fetch_array($cart_item_run)){
$pro_id = $cart_row['product_id'];
$pro_qty = $cart_row['qty'];
$product_query="SELECT * FROM products WHERE product_id = '$pro_id'";
$product_run = mysqli_query($conn, $product_query);
$product_row = mysqli_fetch_array($product_run);
$pro_name = $product_row['product_name'];
$pro_image = $product_row['product_image'];
$pro_sell_price = $product_row['sell_price'];
$total = $pro_sell_price*$pro_qty;
$grand_total =$grand_total+ $total;
$return_value = "<div class=\"row\">
<div class=\"col-md-3 col-xs-3\"><img src=\"img/".$pro_image."\" alt=\"\" width=\"50px\"></div>
<div class=\"col-md-3 col-xs-3\"><p>".$pro_name."</p><a href=\"\"><p><i class=\"fa fa-trash\" aria-hidden=\"true\"></i> Remove</p></a></div>
<div class=\"col-md-2 col-xs-2\">
<select class=\"change_qty\" size=\"1\" style=\"width:50px;\">";
for ($i = 1; $i <= 9; $i++) {
$selected = $pro_qty == $i? 'selected' : '';
$return_value .= "<option class=\"qty\" data-pro=\"".$pro_id."\" value=\"".$i."\" ".$selected.">".$i."</option>";
}
?>
$return_value .= "</select>
</div>
<div class=\"col-md-2 col-xs-2\"><input data-pro=\"".$pro_id."\" id=\"price-".$pro_id."\" type=\"text\" value=\"".$pro_sell_price."\" disabled class=\"form-control dis-input price\"></div>
<div class=\"col-md-2 col-xs-2\"><input id=\"total-".$pro_id."\" data-pro=\"".$pro_id."\" type=\"text\" value=\"".$total."\" disabled class=\"form-control dis-input total\"></div>
</div>
<hr>";
}
}
echo $return_value;
} ?&GT;`