【发布时间】:2016-04-10 13:21:44
【问题描述】:
在我目前正在开发的网站上,我制作了一个列表(购物车创意),客户可以在其中放置产品。它使用GET方法+会话,会话的制作代码如下:
`<?php session_start();
require("dbconnect.php");
?>
<?php
if(!isset($_SESSION['cart'])) {
$cart = array();
$_SESSION['cart'] = $cart;
}
if(isset($_GET['action']) && $_GET['action']=="add"){
$id=intval($_GET['id']);
if(in_array($id, $_SESSION['cart'])){
if (($key = array_search($id, $_SESSION['cart'] !== false))){
unset($_SESSION['cart'][$key]);
}
}
else {
array_push($_SESSION['cart'],$id);
}
}
if(isset($_GET['action']) && $_GET['action']=="delete"){
$id = intval($_GET['id']);
if (in_array($id, $_SESSION['cart'])){
$key = array_search($id, $_SESSION['cart']);
unset($_SESSION['cart'][$key]);
}
}
?>
没有什么特别的,只是一个普通的购物车在一个带有数组的会话中,我把所有唯一的产品代码放在其中以记住列表中的内容。现在,当客户转到可以发送产品列表的页面时,他们还可以选择他们想要的每种产品的数量。他们必须填写一个数字,完成后点击“计算(用我的语言表示)”按钮,他们会得到所有产品的小计价格、增值税和总价。但是,我希望这样客户可以填写他们的个人信息以及列表以及要在电子邮件中发送的金额。我之前自己制作了自制的 PHP 表单,但现在我被卡住了。我使用 GET 作为订单列表,但我总是使用 POST 表单作为我的联系人表单。如何制作一个按钮,将列表、金额以及联系表单字段的输入发送给我?此时我尝试如下(还有更多方法,但到目前为止都失败了)。
<main>
<div class="main-center">
<div class="offerte-container">
<form action="" method="get" value="offertelijst">
<ul class="offerte-list">
<?php
$per_page = 9;
$args = array(
'post_type'=> 'wpcproduct',
'order' => 'ASC',
'orderby' => 'menu_order',
'posts_per_page' => $per_page
);
$products = new WP_Query($args);
?>
<?php
while($products->have_posts()): $products->the_post();
$id = get_the_ID();
$title = get_the_title();
$permalink = get_permalink();
$price = get_post_meta(get_the_id(),'wpc_product_price',true);
$product_id = get_post_meta(get_the_id(), 'product_ID', true);
if(in_array($id, $_SESSION['cart'])){
echo '<li class="wpc-product-item">';
echo '<a href="index.php?action=delete&id=' .$id. '">Verwijder </a>';
echo '<input alt="hoeveelheid" maxlengt="2" value="' .$_GET["amount$id"]. '" min="1" type="number" max="99" name="amount'.$id.'" size="3" required> </input>';
echo '<a href="'. $permalink .'"><div class="item-title"> ' .$title. ' </div></a>';
echo '<a href="'. $permalink .'"><div class="item-take"> <img width="25px" src="http://bgc-testomgeving.nl/sem/wp-content/themes/sem/images/pijltje.png" /> </div></a>';
echo '<a href="'. $permalink .'"><div class="item-nr"> '.$product_id. '</div></a>';
if((isset($_GET["amount$id"]) && $_GET["amount$id"] == 1) || $_GET["amount$id"] == "" ){
if (is_numeric($price) && (floor($price) == $price)) {
echo '<div class="item-price"> €' .number_format ($price , 0 , "," , "." ). ',- </div>';
}
else {
echo '<div class="item-price"> €' .$price. '</div>';
}
echo '</li>';
}
else if(isset($_GET["amount$id"]) && floatval($_GET["amount$id"]) > 1){
changeFormat($price);
$priceTotal = number_format($price * floatval($_GET["amount$id"]), 2);
if (is_numeric($priceTotal) && (floor($priceTotal) == $priceTotal)) {
echo '<div class="item-price"> €' .$priceTotal . ',- </div>';
}
else {
echo '<div class="item-price"> €' .$priceTotal . '</div>';
}
echo '</li>';
}}
endwhile;
?>
</ul>
<input type="submit" value="Bereken"> </input>
</form>
<div class="totalprice">
<?php
(float)$total = 0;
while($products->have_posts()): $products->the_post(); {
$id = get_the_ID();
$title = get_the_title();
$permalink = get_permalink();
$price = get_post_meta(get_the_id(),'wpc_product_price',true);
$product_id = get_post_meta(get_the_id(), 'product_ID', true);
if(in_array($id, $_SESSION['cart'])){
if (is_numeric($price) && (floor($price) == $price)) {
$price = number_format($price, 2);
}
else {
$price = str_replace(',', '.', $price);
}
$total += (floatval($price) * floatval($_GET["amount$id"]));
}}
endwhile;
(String)$total;
number_format($total, 2);
$totalDecimal = str_replace('.', ',', $total);
echo 'Subtotaal: €' .$totalDecimal. '<br />';
echo 'BTW: €' . str_replace('.',',', number_format($total * 0.21,2)). '<br />';
echo 'Totaal: €' . str_replace('.',',', number_format($total * 1.21,2));
function changeFormat($var) {
if(is_numeric($var) && (floor($var) == $var)){
return number_format($var, 0) + ',-';
}
else {
if (is_numeric($var)) {
return number_format($var, 2, ',', '.');
}
else if (is_string ($var)){
return str_replace(',', '.', $var);
}
else {
echo "What the hell is dit voor een formaat?";
}
}}
?>
</div>
</div>
</div>
</main>
计算功能和订单列表都工作正常,我可以制作一个标准的 POST 表单作为联系表单,但我无法完成这项工作。我希望“发送”按钮发送列表以及每个产品的给定数量和填写的联系表格。
这个项目的网址是:http://www.bgc-testomgeving.nl/sem 在http://www.bgc-testomgeving.nl/sem/offertelijst/ 页面下方应该是联系表格,但每次我尝试构建它时,我都会破坏我完美的订单列表。
【问题讨论】:
标签: php forms session post get