2017-04-23 50 views
0

我有一個數組將產品放入表(並且可以搜索),我希望能夠將項添加到數據庫中的購物車表。更具體地說,我想通過「添加到購物車」鏈接運行SQL命令。這是它目前的設置。我閱讀了onClick JS可以工作,但我找不到明確的方式來實現它。使用「添加到購物車」鏈接運行SQL命令在php

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

所以問題是?上面的任何內容都不會插入到數據庫中 - 您只包含提取數據的代碼。 – RamRaider

+0

截至目前,我一直在試圖將echo發佈到表單中,並從一個單獨的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