2016-04-29 35 views
0

私は自分のデータベースから引き出しているアイテムをユーザーが「カート」に追加する方法を理解しようとしています。データベースから項目をドロップダウンメニューに表示する方法はわかっていますが、項目を選択してカートに追加する方法については、データベースに何も挿入しなくても足りません。カートにデータベース項目を追加する

これは私が持っているものですが、動作させるのに苦労しています...どんなヘルプ?

<? 
session_start(); 
setlocale(LC_MONETARY, 'en_US'); 
$product_id = $_POST['Select_Product']; 
$action = $_POST['action']; 
switch ($action) { 
    case "Add": 
     $_SESSION['cart'][$product_id] ++; 
     break; 
    case "Remove": 
     $_SESSION['cart'][$product_id] --; 
     if ($_SESSION['cart'][$product_id] <= 0) 
     unset($_SESSION['cart'][$product_id]); 
     break; 
    case "Empty": 
     unset($_SESSION['cart']); 
     break; 
    case "Info": 
     break;   
} 

require_once(''); //the database(taken out for security) 

?> 
<!DOCTYPE html> 
<html> 
    <head> 
     <meta charset="UTF-8"> 
     <title>Cart</title> 
     <script type="text/javascript" src="view.js"></script> 
    <script> 
      function productInfo(key) { 

       //creates the datafile with query string 
       var data_file = "AdventureCart.php?prodID=" + key; 
       //this is making the http request 
       var http_request = new XMLHttpRequest(); 
       try { 
        // Opera 8.0+, Firefox, Chrome, Safari 
        http_request = new XMLHttpRequest(); 
       } catch (e) { 
        // Internet Explorer Browsers 
        try { 
         http_request = new ActiveXObject("Msxml2.XMLHTTP"); 
        } catch (e) { 
         try { 
          http_request = new ActiveXObject("Microsoft.XMLHTTP"); 
         } catch (e) { 
          // Something went wrong 
          alert("Your browser broke!"); 
          return false; 
         } 
        } 
       } 
       http_request.onreadystatechange = function() { 
        if (http_request.readyState == 4) 
        { 
         var text = http_request.responseText; 

         //this is adding the elements to the HTML in the page 
         document.getElementById("productInformation").innerHTML = text; 
        } 
       } 
       http_request.open("GET", data_file, true); 
       http_request.send(); 
      } 
     </script> 

    <link href="/CSIS2440/CodeEx/view.css" rel="stylesheet" type="text/css"> 
    </head> 
    <body> 
    <div class="form" id="form_container"> 
     <form action="AdventureCart.php" method="Post"> 
      <div> 

       <p><span class="text">Please Select a product:</span> 
        <select id="Select_Product" name="Select_Product" onchange="productInfo(this.value)" class="select"> 
         <option value=""></option> 
         <?php 
         //setting the select statement and running it 
         $search = "SELECT * FROM AdventureGear order by Item"; 
         $return = mysql_query($search); 

         if (!$return) { 
          $message = "Whole query " . $search; 
          echo $message; 
          die("Invalid query " . $mysql_error()); 
         } 
         while ($row = mysql_fetch_array($return)){ 
          if ($row['idAdventureGear'] == $product_id){ 
           echo "<option value='" .$row['idAdventureGear'] . "'selected='selected'>" 
           . $row['Item'] . "</option>"; 
          }else{ 
           echo "<option value='" . $row['idAdventureGear'] . "'>" 
           . $row['Item'] . "</option>"; 
          } 
          } 
         ?> 
        </select></p> 
        <tr> 
         <td> 
          <input id="button_Add" type="submit" value="Add" name="Add" class="button"/> 
         </td> 
         <td> 
          <input name="action" type="submit" class="button" id="button_Remove" value="Remove"/> 
         </td> 
         <td> 
          <input name="action" type="submit" class="button" id="button_empty" value="Empty"/> 
         </td> 
         <td> 
          <input name="action" type="submit" class="button" id="button_Info" value="Info"/> 
         </td> 
        </tr>      
       </table> 

      </div> 
      <div id="productInformation"> 

      </div> 
      <div> 
       <?php 
       if ($infonum > 0){ 
        $sql = "SELECT Item, Cost, Weight, ItemImage FROM AdventureGear WHERE idAdventureGear = " . $infonum; 
        echo "<table align ='left' width='100%'><tr><th>Name</th><th>Price</th><th>Weight</th></tr>"; 
        $result = mysql_query($sql); 
        if (mysql_num_rows($result) > 0){ 
         list($infoname, $infoprice, $infoweight, $infoimage) = mysql_fetch_row($result); 
         echo"<tr>"; 
         echo"<td align=\"left\" width=\"450px\">$infoname</td>"; 
         echo"<td align=\"left\" width=\"325px\">" . money_format('%(#8n', $infoproce) . " </td>"; 
         echo"<td align=\"center\">$infoweight</td>"; 
         echo"<td align=\"left\" width=\"450px\"><img src='images\\$infoimage' height=\"160\" width=\"160\"></td>"; 
         echo "</tr>"; 
        } 
        echo"</table>"; 
       } 
       ?> 
       You have no items in your shopping cart. 
      </div> 
     </form> 
     </div> 
    <? 
    if ($_SESSION['cart']){ 
     echo"<table border=\"1\" padding=\"3\" width=\"650px\"><tr><th>Name</th><th>Weight</th><th>Price</th>" 
     . "<th width=\"80px\">Line</th></tr>"; 
     foreach ($_SESSION['cart'] as $product_id => $quantity){ 
      $sql = "SELECT Item, Cost, Weight FROM AdventureGear WHERE idAdventureGear = ". $product_id . ""; 
      $result = mysql_query($sql); 
      if (mysql_num_rows($result) >0){ 
       list($name, $price, $weight) = mysql_fetch_row($result); 
       $weight = $weight * $quantity; 
       $line_cost = $price * $quantity; 
       $totWeight = $totWeight * $quantity; 
       $total = $total + $line_cost; 
       echo "<tr>"; 
       echo"<td align=\"Left\" width=\"450px\">$name</td>"; 
       echo"<td align ='\"center\" width=\"75px\">" . money_format('%(#8n', $price) . "</td>"; 
       echo"<td align=\"center\">" . money_format('%(#8n', $line_cost) . "</td>"; 
       echo"</tr>"; 
      } 
     } 
    } 
    ?> 
    </body> 
</html>`enter code here` 
+0

<?phpが欠けているように見えますか? – Slime

+0

セッションとして保存する必要があります。 –

+0

管理性を向上させるには、この機能を複数の機能に分割する必要があります。特定の再利用可能なタスクを突破する機能があれば、何をすべきか分かりやすいでしょう。 – Rasclatt

答えて

0

あなたは、クッキーやセッションとして保存し、彼らが何かをクリックするとライン#1

+0

これを自分のコードに追加するのを忘れました。それは私のサーバー上にあります。 –

関連する問題