【问题标题】:Send SESSION GET information in POST contact form together with the POST fields在 POST 联系表单中发送 SESSION GET 信息以及 POST 字段
【发布时间】: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"> &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" 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: &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>

计算功能和订单列表都工作正常,我可以制作一个标准的 POST 表单作为联系表单,但我无法完成这项工作。我希望“发送”按钮发送列表以及每个产品的给定数量和填写的联系表格。

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

【问题讨论】:

    标签: php forms session post get


    【解决方案1】:

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

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

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

    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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-11
      • 2021-03-13
      • 2016-10-20
      • 2021-02-06
      • 2020-02-16
      • 1970-01-01
      • 2015-04-05
      • 2023-03-04
      相关资源
      最近更新 更多