2017-04-23 11 views
0

私はテーブルに商品を置く(検索可能な)配列を持っていますので、データベースのカートテーブルに項目を追加できます。具体的には、「カートに追加」リンクからSQLコマンドを実行します。これは現在設定されている方法です。 onClick JSはうまくいくかもしれませんが、私はそれを実装する明確な方法を見つけることができませんでした。PHPで "カートに追加"リンクを使用してSQLコマンドを実行

<?php 

      $table="SELECT * FROM Product ORDER BY ProductID ASC"; 
      $sq=mysqli_query($db,$table); 
      if(mysqli_num_rows($sq) == 0){ 


      echo "Search For a Product"; 
       ?> 
       </td> 
       </tr> 
       <?php 
      }else{ 
      while ($row2=mysqli_fetch_array($sq)) { 
        $pname = $row2['ProductName']; 
        $pdesc = $row2['ProductDescription']; 
        $pprice = $row2['ProductPrice']; 
        $pinv = $row2['ProductInventory']; 
        $pid = $row2['ProductID']; 
     ?> 
       <tr> 
        <td><?php echo $row2['ProductName'] ?></td> 
        <td><?php echo $row2['ProductDescription'] ?></td> 
        <td>$<?php echo $row2['ProductPrice'] ?></td> 
        <td><?php echo $row2['ProductInventory'] ?></td> 
        <td><input type="submit" name="<?php echo $pid ?>" value="Add To Cart"></td>     
       </tr> 
     <?php 

      } 
     } 
     ?> 
+0

を役に立てば幸いだから問題は?上記のいずれもdbに挿入されません。データを取り込むコードだけが含まれています。 – RamRaider

+0

現在、私はフォームにエコーを投稿し、データを挿入して製品ページにリダイレクトする別のPHPページから呼び出そうとしていましたが、明らかにうまくいきませんでした。 – Indyvette

+0

私は "カートに追加"リンクをSQL挿入コマンドに変える方法を見つけ出そうとしています – Indyvette

答えて

0
むしろあなたは、AJAXを利用して、同じページ(または異なる)に要求を送ることができる別のページへのポストより

ので、それはそれをあなたのユーザーのためのより良い経験になります。下記のコードは、すばやくまとめられて、目的の達成目標を達成する方法を示しています。

クリック処理機能のparamsオブジェクトへの簡単な追加 - リクエストとともに送信される各アイテムの数量を含めることをお勧めします。

<?php 
    if($_SERVER['REQUEST_METHOD']=='POST'){ 
     if(!empty($_POST['action'])){ 
      switch($_POST['action']){ 
       case 'add': 
        $sql='insert into `table` (f1,f2,f3) values (?,?,?)'; /*etc*/ 
       break; 
       case 'delete': 
        $sql='delete from `table` where `id`=?'; 
       break; 
       case 'update': 
        $sql='update `table` set f1=? where `id`=?'; 
       break; 
       case 'checkout': 
        /* other stuff, probably best handled with another page */ 
       break; 
      } 
     } 
    } 
?> 

<html> 
    <head> 
     <title>example</title> 
    </head> 
    <body> 
     <!-- 
      the products would be generated by a db call and rendered rather than statically typed as here 
     --> 
     <form> 
      <!-- product #1 --> 
      <div> 
       <!-- /* product details */ --> 
       <a href='#' class='bttn-add' id='product-1'>Add to cart</a> 
      </div> 

      <!-- product #2 --> 
      <div> 
       <!-- /* product details */ --> 
       <a href='#' class='bttn-add' id='product-2'>Add to cart</a> 
      </div> 


      <!-- product #3 --> 
      <div> 
       <!-- /* product details */ --> 
       <a href='#' class='bttn-add' id='product-3'>Add to cart</a> 
      </div> 
     </form> 


     <script> 

      function evtClickHandler(e){ 
       ajax.call(this, 'post',location.href,{ action:'add',id:this.id },evtCallback,{/* options */}); 
      } 
      function evtCallback(response){ 
       alert(r); /* process the response accordingly to manipulate the DOM etc */ 
      } 

      /* utility function for making ajax requests */ 
      function ajax(method, url, params, callback, options){ 
       var xhr=new XMLHttpRequest(); 
       xhr.onreadystatechange=function(){ 
        if(xhr.readyState==4 && xhr.status==200)callback.call(this, xhr.response, options, xhr.getAllResponseHeaders()); 
       }; 

       var async=params.hasOwnProperty('async') ? params.async : true; 
       var query=[]; 

       for(var n in params)query.push(n+'='+params[n]); 

       switch(method.toLowerCase()){ 
        case 'post': query=query.join('&'); break; 
        case 'get': url+='?'+query.join('&'); query=null; break; 
       } 

       xhr.open(method.toUpperCase(), url, async); 
       xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); 
       xhr.send(query); 
      } 



      /* Assign event listeners to the various buttons/anchors on the html page rather than inline handlers */ 
      var col=document.querySelectorAll('a.bttn-add'); 
      if(col){ 
       for(var n in col){ 
        if(col[ n ].nodeType==1){ 
         col[ n ].addEventListener('click',evtClickHandler.bind(col[n]),false); 
        } 
       } 
      } 
     </script> 
    </body> 
</html> 

これだけではなく決定的な解決策というより指導を与えることを目的としている - による書き込みなどの速度にそこに潜む小さなmistooksがあるかもしれないので、それはテストされていないままですが、私はそれが少し

+0

ありがとう!私はこれを調べるつもりです – Indyvette

関連する問題