Как вы отметили, вам нужно использовать 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>
/ Рассчитать 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
*/