繁体   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