在POST联系表单中发送SESSION GET信息以及POST字段

时间:2016-04-10 13:21:44

标签: php forms session post get

在我正在制作的网站上,我制作了一个列表(购物车创意),客户可以在其上放置产品。它适用于GET方法+会话,会话的制作代码如下:

 rcons :: a -> ([a] -> d) -> d

没什么特别的,只是在一个带阵列的会话中的常规购物车,我把所有独特的产品代码都记住了列表中的内容。现在,当客户转到他们可以发送产品列表的页面时,他们也可以选择他们想要的每种产品的数量。他们必须填写一个数字,当他们完成后,他们点击按钮'计算(用我的语言berekenen)',他们得到所有产品,增值税和总价的小计价格。但是,我希望这样,客户可以填写他们的个人信息加上列表加上要通过电子邮件发送的金额。我自己早些时候制作了自制的PHP表单,但现在我已经陷入困境。我使用GET作为订单列表,但我总是使用POST表单作为我的contactforms。如何创建一个发送列表的按钮加上金额加上联系表单字段的输入给我?此刻我尝试了如下(还有更多方法,但到目前为止都失败了)。

`<?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]);
                    }
        }  
?>

计算功能和订单列表都运行正常,我可以将标准的POST表格作为联系表格,但我无法完成这项工作。我想按“发送”按钮发送列表加上每件产品的给定金额和填写的联系表格。

此项目的网址为:http://www.bgc-testomgeving.nl/semhttp://www.bgc-testomgeving.nl/sem/offertelijst/页面下面应该是联系表格,但每次我尝试构建这个时,我都会拆除我的完美订单。

1 个答案:

答案 0 :(得分:1)

首先将您的表单方法更改为发布。

<form action="" method="post" value="offertelijst">

然后,您必须为表单元素中的每个项目创建输入。我看到你的表单中只有Amount输入:

echo '<input alt="hoeveelheid" maxlengt="2" value="' .$_GET["amount$id"]. '" min="1" type="number" max="99" name="amount'.$id.'" size="3" required> </input>';

为每个元素创建输入,因为用户不需要看到那些可以将它们创建为隐藏元素的输入,这里是项目标题的一个示例:

echo '<input type="hidden" name="title['.$id.']" value="' .$title. '"</input>';

把这个放在这一行

echo '<a href="'. $permalink .'"><div class="item-title"> ' .$title. ' </div></a>';

创建所有输入后,还要在此附近创建第二个按钮:

<input type="submit" name="action" value="Bereken">
<input type="submit" name="action" value="Send">

因此,当用户点击Bereken时,您将进行计算,但如果是“发送”按钮,则会将其邮寄给您自己。这是示例代码:

<?php
// if send button clicked
if($_POST["action"]=="Send")
{
    /// mail to your self all element
    mail("you@www.com","New Order",implode("-",$_POST));
}
?>
<main>
    <div class="main-center">
        <div class="offerte-container">
            <form action="" method="post" 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>';
                            // i added below input for example
                            echo '<input type="hidden" name="title['.$id.']" value="' .$title. '"</input>';
                            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"> &nbsp; '.$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"> &#8364;' .number_format ($price , 0 , "," , "." ). ',- </div>';
                                }
                                else {
                                    echo '<div class="item-price"> &#8364;' .$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"> &#8364;' .$priceTotal . ',- </div>';
                                }
                                else {
                                    echo '<div class="item-price"> &#8364;' .$priceTotal . '</div>';
                                }
                                echo '</li>';
                            }}
                    endwhile;
                    ?>
                </ul>
                <input type="submit" name="action" value="Bereken">
                <input type="submit" name="action" value="Send">
            </form>
            <div class="totalprice">
                <?php
                // is bereken button clickied
                if($_POST["action"]=="Bereken") {
                (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: &nbsp;&#8364;' .$totalDecimal. '<br />';
                echo 'BTW: &nbsp;&#8364;' . str_replace('.',',', number_format($total * 0.21,2)). '<br />';
                echo 'Totaal: &nbsp;&#8364;' . 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>