【问题标题】:How can i wrap php code in one echo我如何将php代码包装在一个回声中
【发布时间】:2018-02-02 05:31:09
【问题描述】:

我有 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>

            <?

        }
    }
}
?>

【问题讨论】:

  • 为什么?离开php写html和从php回显html是一样的。
  • 无需回显响应,只需使用 ajax 自行设置 innerHTML。
  • 实际上这是在行动页面。我需要在购物车页面上显示它,所以在 ajax 中我会收到响应,以便我可以将响应设置为购物车页面中 div 的 innerHTML。现在它显示在操作页面本身

标签: javascript php sql ajax string


【解决方案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;

} ?>`

【讨论】:

  • 先生,这解决了我的一个问题,但现在又出现了一个问题。在这段代码中,您可以看到选择框有一个类名 change_qty... 在 js 中,当我尝试提供事件处理程序 (.change) 时,它无法识别此类。问题可能出在哪里?
  • 在上面的例子中它回显 $return_value 一个变量.....问题是我可以回显多个变量吗?如果是,那么我如何分别在 ajax 响应中接收它们并将它们显示在不同的 div 中。
  • @kohali 这是因为您可能直接在页面加载时分配侦听器。像$(document).on('change','.change_qty',function(){ your code }); 一样分配监听器
  • 你可以像这样使用 json_encode $arr_with_all_return_values[] =array('msg' =&gt; 'your message'); $arr_with_all_return_values[] = array('something_else_to_return'=&gt;'what else needs to be returned you ask?'); $arr_with_all_return_values[] = array('return_value'=&gt;$return_value); echo json_encode($arr_with_all_return_values);
猜你喜欢
  • 1970-01-01
  • 2018-04-23
  • 1970-01-01
  • 2013-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多