簡體   English   中英

我如何獲得總價並將其顯示在同一頁面上PHP

[英]How can i get the total price and display it in the same page PHP

我正在使用php,我有一個產品列表頁面,該頁面從數據庫中獲取產品並將其顯示給用戶。 然后,用戶選擇數量。

問題是我不知道如何在同一頁面上顯示總價。 下一頁是計算總價的頁面。

誰能幫我?

我面臨的主要問題是,當我更改數量時,它正在重置頁面,因此數量變為0。由於需要在另一個頁面中進行計算,因此我需要發布工作。

 include 'Accessories.php'; include_once 'Database.php'; session_start(); $db = new Accessories(); $accessories = $db->getAllAccessories(); echo 'second time'; echo '<br>'; echo '<br>'; echo '<br>'; echo '<form action=" '?> <?php $_SERVER['PHP_SELF']?> <?php echo'" method="post">'; echo '<div class="row"><div class="col-md-1"></div><div class="col-md-10"><input type="submit" name="submited" value="Procced" class="btn btn-block btn-primary"></div></div>'; echo '<div class="container">'; foreach ($accessories as $ac) { echo '<div class = "form-group"> <label for="qty_list[' . $ac->taskID . "-" . $ac->taskID . ']">'.$ac->taskName . " " . " " . $ac->description . " " . $ac->price . " BHD".'</label>'; echo ' <input id="" type="number" min="0" onchange="this.form.submit()" class="form-control" name="qty_list[' . $ac->taskID . "-" . $ac->taskID . ']" value="' ?> <?php $_POST['qty_list'] ?> <?php echo'" placeholder="">'; echo'</div>'; echo'<br>'; } '</div> </div> </div>'; echo '</form>'; 

 <?php session_start(); include_once 'Database.php'; include 'Ac_car_res.php'; include 'Accessories.php'; include 'Reservation.php'; $reserved = false; //$dateFrom = $_SESSION['dateFrom']; //$dateTo = $_SESSION['dateTo']; //echo $dateFrom; foreach ($_POST['qty_list'] as $key => $qty) { $r = 102; //$_SESSION['resId'] = $r; $qty; $acAdd = new Ac_car_res(); // session_start(); $resId = $r; $index = strrpos($key, "-"); $sCarId = substr($key, $index + 1); $acId = substr($key, 0, $index); $acAdd->initWith($acId, $resId, $sCarId, $qty); $acAdd->addAcCarRes(); } $db = Database::getInstance(); $access = new Accessories(); $res1 = new Reservation(); //echo $Id; echo "<br>"; $r = 102; $sql = "select * from ac_car_res where resId=" . $r; $a = $db->multiFetch($sql); foreach ($a as $m) { $access->initWithAcId($m->acId); $res1->initWithResId($m->resId); echo "accessories" . $access->getPrice() . "qty" . $m->qty; echo "<br>"; $totalPriceForRes = ($res1->getTotalPrice() + $access->getPrice() * $m->qty); $res1->setTotalPrice($totalPriceForRes); echo $res1->getTotalPrice(); echo "<br>"; $res1->updateReservation(); } ?> 

 <?php session_start(); include_once 'Database.php'; include 'Ac_car_res.php'; include 'Accessories.php'; include 'Reservation.php'; $reserved = false; //$dateFrom = $_SESSION['dateFrom']; //$dateTo = $_SESSION['dateTo']; //echo $dateFrom; foreach ($_POST['qty_list'] as $key => $qty) { $r = 102; //$_SESSION['resId'] = $r; $qty; $acAdd = new Ac_car_res(); // session_start(); $resId = $r; $index = strrpos($key, "-"); $sCarId = substr($key, $index + 1); $acId = substr($key, 0, $index); $acAdd->initWith($acId, $resId, $sCarId, $qty); $acAdd->addAcCarRes(); } $db = Database::getInstance(); $access = new Accessories(); $res1 = new Reservation(); //echo $Id; echo "<br>"; $r = 102; $sql = "select * from ac_car_res where resId=" . $r; $a = $db->multiFetch($sql); foreach ($a as $m) { $access->initWithAcId($m->acId); $res1->initWithResId($m->resId); echo "accessories" . $access->getPrice() . "qty" . $m->qty; echo "<br>"; $totalPriceForRes = ($res1->getTotalPrice() + $access->getPrice() * $m->qty); $res1->setTotalPrice($totalPriceForRes); echo $res1->getTotalPrice(); echo "<br>"; $res1->updateReservation(); } ?> 

在標記時,需要使用ajax,我將使用jQuery,因為它很簡單。

大概您的第一個片段是您的選擇項目頁面,但是在此之前,我可能會在配置文件中創建一個自動加載器,該文件將包含在每個頂級頁面中:

/config.php

<?php
define('DS', DIRECTORY_SEPARATOR);
# Set the root directory
define('ROOT_DIR', __DIR__);
# I would put classes in this folder instead of root
define('VENDOR', ROOT_DIR.DS.'vendor');
# Create a class autoloader so you don't have to manually include files for classes
spl_autoload_register(function($class){
    # If you have classes in the root
    $root   = ROOT_DIR.DS.$class.'.php';
    # See if there are any in vendor folder
    $vendor = VENDOR.DS.$class.'.php';
    # Check both folders, include if available
    if(is_file($root))
        include_once($root);
    elseif(is_file($vendor))
        include_once($vendor);
});
# Start session here, then you only ever have to write it once, provided you always
# include this config on the top level page at the top
session_start();

/product_list_page.php

<?php
# Add config
require(__DIR__.DIRECTORY_SEPARATOR.'config.php');
# Create instances
$db          = new Accessories();
$accessories = $db->getAllAccessories(); ?>

second time<br><br><br>

<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
    <div class="row">
        <div class="col-md-1">
            <div class="col-md-10">
                <input type="submit" name="submited" value="Procced" class="btn btn-block btn-primary" />
            </div><!-- close col-md-10 -->
        </div><!-- close col-md-1 -->
        <div class="container">
            <?php foreach($accessories as $ac): ?>

            <div class="form-group">
                <label for="qty_list[<?php echo $ac->taskID . "-" . $ac->taskID; ?>]"><?php echo $ac->taskName . " " . " " . $ac->description . " " . $ac->price . " BHD" ?></label>
                <input id="" type="number" min="0" class="form-control" name="qty_list[<?php echo $ac->taskID . "-" . $ac->taskID ?>]" value="<?php echo $_POST['qty_list'] ?>" placeholder="" />
            </div><!-- close form-group -->
            <br>

            <?php endforeach ?>
        </div><!-- close container -->
    </div><!-- close row -->
</form>
<!-- value will returned to this div -->
<div id="total-value"></div>

<script>
    // Make sure to add the jquery library link
    $(function(){
        // Use a listener instead of inline javascript
        $('form').on('change',function(e){
            $.ajax({
                // This page will process the form and calculate total
                'url': '/calculate.php',
                // Send via post
                'type': 'post',
                // Serialize the data
                'data': $(this).serialize(),
                // If/when the response from calculate is successful
                'success': function(response) {
                    // Put that total into the blank div in the above html
                    $('#total-value').html(response);
                }
            });
        });
    });
</script>

/calculate.php

<?php
# Include config
include(__DIR__.DIRECTORY_SEPARATOR.'config.php');
/**
* Do your total calculations here based on POST then echo the final number. Whatever is 
* echoed on this page will show up in the other page inside the "total-value" div
*/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM