2016-12-19 12 views
0

私は、データベースから製品属性を取得し、その属性でテーブル行に選択オプションに基づいてデータを取り込むための少しの助けを得ようとしています。選択オプションに基づいてテーブルデータを変更する方法

Iヘッダーである単列で、HTMLテーブルを有する:

  1. SKU
  2. アイテム説明
  3. 立方メートル
  4. 数量
  5. 価格
  6. 正価

入力された唯一の表セルは「SKU」です。選択メニューには、データベースからオプションが引き出されています。残りのテーブルは空です。私が達成しようとしているのは、データベースのWHERE sku = 'selectオプションの値'からテーブルのデータを取り出し、残りのhtmlテーブルに値を設定することです。したがって、 "sku"の選択に基づいて、 "Item Description"、 "Cubic Meters"、および "Price"でhtmlテーブルを動的に満たすことができる必要があります。ここで

は、フォームページのform.phpです:

<html> 
<head> 
<link href="ERP/styles/check_cs6.css" rel="stylesheet" type="text/css"> 
    <script type="text/javascript"> 

     //add row function 
     function onClickAdd() { 
     var tableRow = document.getElementById("tableRow"); 
     var tableRowClone = tableRow.cloneNode(true); 
     tableRow.parentNode.insertBefore(tableRowClone, tableRow.nextSibling); 
      } 
    </script> 
    <script> 

     //fetch data 
    function showProd(str) { 
      if (str == "") { 
       document.getElementById("txt").innerHTML = ""; 
       return; 
      } else { 
       if (window.XMLHttpRequest) { 
        // code for IE7+, Firefox, Chrome, Opera, Safari 
        xmlhttp = new XMLHttpRequest(); 
       } else { 
        // code for IE6, IE5 
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
       } 
       xmlhttp.onreadystatechange = function() { 
        if (this.readyState == 4 && this.status == 200) { 
         document.getElementById("prodName").innerHTML = this.responseText; 
        } 
       }; 
       xmlhttp.open("GET","getprod.php?q="+str,true); 
       xmlhttp.send(); 
      } 
     } 
    </script> 
</head> 
<body> 
    <div id="left_column"> 
     <h1>Purchase Order Form</h1> 
     <p>Supplier: 
     <select name="supplier"> 
      <option value="#">Dave</option> 
      <option value="#">Dude</option> 
     </select> 
     </p> 
    </div> 
    <div id="right_column"> 
     <p>Date: <input type='text' size='6' maxlength='10' /></p> 
     <br> 
     <p>Ship To This Address:</p> 
     <p>123 Fake Street</p> 
     <p>Plaza</p> 
     <p>Springfield, Simpsons</p> 
    </div> 
    <br> 
    <div id="POform"> 
     <p class="alignLeft">Ship Date: <input type="text" size='6' maxlength='10'></p> 
     <p class="alignRight">PO No.: <input type="text" size="6" maxlength="10"></p> 
    </div> 

<table id="myTable" width="100%"> 
      <tr> 
       <th>SKU</th> 
       <th>Item Description</th> 
       <th>CBM</th> 
       <th>Qty</th> 
       <th>Unit Price</th> 
       <th>Net Price</th> 
      </tr> 
      <tr id="tableRow"> 
       <td width ="10%"> 
       <select name="users" onchange="showUser(this.value)"> 
     <?php 

      $connection = mysql_connect('address', 'user', 'pw'); 
      mysql_select_db('ADI'); 
      $sql = mysql_query("SELECT sku FROM inventory"); 
      while ($row = mysql_fetch_array($sql)){ 
      echo "<option>". $row['sku'] . "</option>"; 
           } 
     ?> 
     </select> 
       </td> 
       <td width ="50%" id="prodName"> 

       </td> 
       <td width = "10%" id="cbm"></td> 
       <td width = "10%" id="qty"><input type="text" size="5"></td> 
       <td width = "10%" id="cost"></td> 
       <td width = "10%" id="netCost"></td> 
       <!--Delete Function doesn't work--> 
       <!--<td><input type="button" value="Delete" onclick="deleteRow ('myTable',0)"></td>--> 

      </tr> 
      <p> 
        <input type="button" name="addButton" value="Add Item" onClick="Javascript:onClickAdd()"> 
      </p> 
     </table> 
<br> 
<!--<div id="txtHint"><b>info will be listed here...</b></div>--> 

</body> 
</html> 

そして、これはフォームハンドラページgetprod.phpです:

<!DOCTYPE html> 
<html> 
<head> 
<style> 
table { 
    width: 100%; 
    border-collapse: collapse; 
} 

table, td, th { 
    border: 1px solid black; 
    padding: 5px; 
} 

th {text-align: left;} 
</style> 
</head> 
<body> 

<?php 
$q = $_GET['q']; 


$con = mysqli_connect('localhost','root','','ADI'); 
if (!$con) { 
    die('Could not connect: ' . mysqli_error($con)); 
} 

mysqli_select_db($con,"ADI"); 
$sql="SELECT prodName, sku, cbm, cost FROM inventory WHERE sku = '".$q."'"; 
$result = mysqli_query($con,$sql); 


/* 
echo "<table width=100%> 
<tr> 
<th>SKU</th> 
<th>Item Description</th> 
<th>CBM</th> 
<th>Qty</th> 
<th>Unit Price</th> 
<th>Net Amount</th> 
</tr>";*/ 
while($row = mysqli_fetch_array($result)) { 
    // echo "<tr>"; 
    //echo "<td>" . $row['sku'] . "</td>"; 
    echo "<td>" . $row['prodName'] . "</td>"; 
    //echo "<td>" . $row['cbm'] . "</td>"; 
    //echo "<td>" . $row['qty'] . "</td>"; 
    //echo "<td>" . $row['cost'] . "</td>"; 
    //echo "<td>" . $row['amt'] . "</td>"; 
    //echo "</tr>"; 
} 

mysqli_close($con); 
?> 
</body> 
</html> 

そして、ここでは、これは書式設定ができます場合はCSSです:

@charset "utf-8"; 

#container {  
    width: 968px;  
    background: #FFF;  
    margin: 0 auto;  
    padding-left: 10px;  
    padding-right: 10px;  
    overflow: hidden;  
}  

#left_column{  
    margin-left: 40px;  
    float: left;  
} 

#right_column{  
    margin-right: 100px;  
    float: right;  
} 

#header h1 {  
    color: #FF0000;  
    font-size: 96px;  
    font-family: sarina, serif; 
    margin: 0;  
    float: left;  
} 

#header {  
    color: #FFF;  
    background-color: #C8C6C6;  
    height: 100px; 
    position: relative;  
} 

body {  
    font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;  
    color: #3B3B3B;  
    background-color: #FFF;  
    margin: 0px;  
} 

#header h2 {  
    color: #FF0000;  
    font-family: "Lucida Sans Unicode", "Lucida Grande", sans-serif;  
    font-size: 30px;  
    font-variant: small-caps;  
    margin-top: 0px;  
    margin-bottom: 0px;  
    padding-top: 15px;  
} 

#header a {  
    font-size: 20px;  
    font-weight: bold;  
    font-variant: small-caps;  
    color: #FFF;  
    text-decoration: none; 
    text-align: center;  
    width: 100px;  
    display: block;  
} 

#header ul {  
    margin: 0px;  
    padding: 0px;  
    list-style-type: none;  
    position: absolute;  
    right: 0px; 
    bottom: 0px;  
} 

#main_text {  
    background-color: #FFF;  
    width: 300px;  
    position: absolute;  
    right: 60px;  
    bottom: 60px;  
} 



#header li {  
    float: left;  
} 

#addProduct {  
    margin-left: 125px;  
} 

#inventory {  
    margin-left: 20px;;  
} 

#purchaseOrders {  

} 

a.createNew {  
    float: right;  
} 

#POform{  
    margin-top: 250px;  
} 

p.alignRight{  
    float:right;  
    margin-right: 100px;  
} 

p.alignLeft{  
    float:left; 
    margin-left: 40px;  
} 

#items {  
    margin-top: 325px;  
    margin-left: 40px;  
    margin-right: 100px;  
} 

.POinput{  
    width: 50px;  
    float: right;  
} 

#items th{  
    text-align:center;  
    border-bottom: 1px dotted;  
    height: 20px;  
}  

#total{  
    margin-left: 100px;  
    margin-right: 100px;  
} 
+0

を変更クエリとデータベース接続を同じPHPファイルに入れないことを強くお勧めします。 JQueryを使用すると、LaravelのようなPHPフレームワークを使用すると、人生はより簡単になります。 あなたのプロフェッショナルに成功したことを願っています。 –

答えて

0

オプションではスキューのみを表示しますが、値属性も設定する必要があります。そうでない場合は、フォームを送信すると、選択した項目に空の値属性が設定されます。

echo '<option value="' . $row['sku'] . '">'.$row['sku'].'</option>' 
0

これを見て、フォームページに追加のjavascript関数を書いて、selectオプションに基づいてデータベースから単一の列をフェッチしました。

//fetches the ProdName 
function getProdName(str) { 
      if (str == "") { 
       document.getElementById("txtHint").innerHTML = ""; 
       return; 
      } else { 
       if (window.XMLHttpRequest) { 
        // code for IE7+, Firefox, Chrome, Opera, Safari 
        xmlhttp = new XMLHttpRequest(); 
       } else { 
        // code for IE6, IE5 
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
       } 
       xmlhttp.onreadystatechange = function() { 
        if (this.readyState == 4 && this.status == 200) { 
         document.getElementById("prodName").innerHTML = this.responseText; 
        } 
       }; 
       //php processor address 
       xmlhttp.open("GET","getProd.php?q="+str,true); 
       xmlhttp.send(); 
      } 
     } 
    // fetches the CBM 
    function getCBM(str) { 
      if (str == "") { 
       document.getElementById("txtHint").innerHTML = ""; 
       return; 
      } else { 
       if (window.XMLHttpRequest) { 
        // code for IE7+, Firefox, Chrome, Opera, Safari 
        xmlhttp = new XMLHttpRequest(); 
       } else { 
        // code for IE6, IE5 
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
       } 
       xmlhttp.onreadystatechange = function() { 
        if (this.readyState == 4 && this.status == 200) { 
         document.getElementById("cbm").innerHTML = this.responseText; 
        } 
       }; 
       //php processor address 
       xmlhttp.open("GET","getCBM.php?q="+str,true); 
       xmlhttp.send(); 
      } 
     } 
    //fetches the price 
    function getPrice(str) { 
     if (str == "") { 
      document.getElementById("txtHint").innerHTML = ""; 
      return; 
     } else { 
      if (window.XMLHttpRequest) { 
       // code for IE7+, Firefox, Chrome, Opera, Safari 
       xmlhttp = new XMLHttpRequest(); 
      } else { 
       // code for IE6, IE5 
       xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
      } 
      xmlhttp.onreadystatechange = function() { 
       if (this.readyState == 4 && this.status == 200) { 
        document.getElementById("cost").innerHTML = this.responseText; 
       } 
      }; 
       //php processor address 
       xmlhttp.open("GET","getPrice.php?q="+str,true); 
       xmlhttp.send(); 
      } 
     } 

また、各列のSQLクエリを処理するために別々のPHPファイルを作成すると、これはgetProd.phpです。 (getPrice.php)getCBM(getCBM.php)用とgetPriceのための1を作ったが、ただ、これは私は、あなたがやろうとしているもののほんの一例であるSQLクエリーを選択CBMと価格、私は願って、それぞれ

<?php 

$q = $_GET['q']; 

$con = mysqli_connect('address','user','pw','db'); 
if (!$con) { 
    die('Could not connect: ' . mysqli_error($con)); 
     } 

mysqli_select_db($con,"ADI"); 
$sql="SELECT prodName, cbm, cost FROM inventory WHERE sku = '".$q."'"; 
$result = mysqli_query($con,$sql); 


while($row = mysqli_fetch_array($result)) { 

    //echo "<td>" . $row['sku'] . "</td>"; 
    echo "<td>" . $row['prodName'] . "</td>"; 
    //echo "<td>" . $row['cbm'] . "</td>"; 
    //echo "<td>" . $row['qty'] . "</td>"; 
    //echo "<td>" . $row['cost'] . "</td>"; 
    //echo "<td>" . $row['amt'] . "</td>"; 

} 

    /*foreach($result as $row) { 
     echo "<td>" . $row['prodName'] . "</td>"; 
     echo "<td>" . $row['cbm'] . "</td>"; 
     }*/ 

mysqli_close($con); 

?> 
+0

追加列機能はかなり機能しません –

関連する問題