2016-04-10 11 views
0

私が現在取り組んでいるウェブサイトでは、顧客が製品を置くことができるリスト(カートのアイデア)を作った。それは次のようにセッションを行うためのコードは、GETメソッド+セッションで動作します:SESSION GET情報をPOST連絡先フォームとPOSTフィールドと共に送信

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

特別何も、私はすべてのユニークな製品のコードを入れて、アレイとのセッションではごく普通のカートリストにあるものを覚えておいてください。顧客が製品のリストを送ることができるページに行くとき、彼らはまた、彼らが望むそれぞれの製品の数を選択することができます。彼らは数字を記入しなければなりません。そして、完了したら、「計算(私の言語でberekenen)」ボタンをクリックすると、すべての製品の小計、付加価値税(VAT)と総価格が得られます。しかし、私は、顧客が自分の個人情報とリストに加えて電子メールで送信する金額を記入できるように、このようにしたい。私は自作のPHPフォームを自分で作成しましたが、今は固くなっています。私は注文リストのためにGETを使用しますが、私はいつも私のコンタクトフォームにPOSTフォームを使用します。どのように私はリストに加えて金額と連絡フォームフィールドの入力を送信する1つのボタンを作ることができますか?この瞬間、私はそれを次のように試しました(さらに多くの方法がありましたが、これまでにすべて失敗しました)。計算機能とオーダーリスト

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

はすべて正常に動作していると私はのContactFormとして、標準のPOSTフォームを作ることができるが、私はこれを成し遂げるために管理することはできません。私は、製品ごとに与えられた金額と連絡フォームに記入された金額とリストを送信するために、「送信」ボタンを押してください。

このプロジェクトのためのURLは次のとおりです。http://www.bgc-testomgeving.nl/sem/offertelijst/ページの下側にhttp://www.bgc-testomgeving.nl/sem は、お問い合わせフォームでなければなりませんが、私はこれを構築しようとするたびに私は私の完璧な順序リストを解体します。

答えて

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>'; 

ユーザーdoesntのは、ここで、あなたは隠された要素としてそれらを作成することができ、それらの入力を参照する必要があるため、各要素の入力を作成した項目のタイトルのための一例であり、

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

あなたはすべての入力を作成した後も、この1の近傍の第2ボタンを作成し、

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("[email protected]","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> 
関連する問題