2012-08-08 71 views
1

讓我再試一次,所以我想讓我的下拉菜單與它所拖動的數據在同一行中。然後如果需要可以添加更多的行。填充行的其餘部分的下拉列表

所以本質上我想選擇一個項目,它顯示該項目的屬性在同一行。然後點擊一個按鈕添加更多行。

表是這樣的:

example

這是我到目前爲止有:

HTML:

<div id='main'> 
       <!--Dropdown Will not work inside of table--> 
     <select id='ddName' onchange="showUser(this.value)"> 
       <option id='none'>Select a Food:</option> 
       <?php $sql = new Mysql(); $sql->diary(); ?> 
       </select> 
     <input type='button' id='addRows' value='Add Rows'/> 
     <table id="diary"> 
     <thead> 
      <tr> 
       <th scope="cols">Check</th> 
       <th scope="cols">Name</th> 
       <th scope="cols">Units</th> 
       <th scope="cols">Amounts</th> 
       <th scope="cols">Calories</th> 
       <th scope="cols">Sugars</th> 
      </tr> 
      </thead> 
      <tbody> 
       <tr id='txtHint'></tr> 
      </tbody> 
      </table> 
    </div> 

阿賈克斯下拉:

//Retrived from w3schools.com 
function showUser(str) 
    { 
    if (str=="") 
     { 
     document.getElementById("txtHint").innerHTML=""; 
     return; 
     } 
    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 (xmlhttp.readyState==4 && xmlhttp.status==200) 
     { 
     document.getElementById("txtHint").innerHTML=xmlhttp.responseText; 
     } 
     } 
    xmlhttp.open("GET","classes/ajax.php?q="+str,true); 
    xmlhttp.send(); 
    }   

PHP填表:

//Retrived from w3school.com 
$q=$_GET["q"]; 

require_once dirname(dirname(__FILE__)).'/includes/constants.php'; 

$con = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME) or 
        die('There was a problem connecting to the database.'); 

$result = $con->query("SELECT * FROM ingredient WHERE name = '".$q."'"); 


while($row = $result->fetch_array()) 
{ 
    echo "<td><input type='checkbox' /></td>"; 
    echo "<td>".$row['Name']."</td>"; 
    echo "<td>" . $row['Units'] . "</td>"; 
    echo "<td>" . $row['Amount'] . "</td>"; 
    echo "<td>" . $row['Calories'] . "</td>"; 
    echo "<td>" . $row['Sugar'] . "</td>"; 
    echo "</tr>"; 
} 
+0

一件事。代碼太多 – codingbiz 2012-08-08 22:44:49

+0

我解決了問題,但無法發佈7個小時。 – 2012-08-08 23:58:52

+0

JQuery可以讓你的生活更輕鬆 – codingbiz 2012-08-09 00:04:37

回答

1

我能弄清楚我的問題。我將這兩個php代碼合併爲一個。

PHP修復:

同時
$q=$_GET["q"]; 

require_once dirname(dirname(__FILE__)).'/includes/constants.php'; 

$con = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME) or 
        die('There was a problem connecting to the database.'); 

$result = $con->query("SELECT * FROM ingredient WHERE Name = '".$q."'"); 
$dd  = $con->query("SELECT * FROM ingredient"); 

if($q == 'Select a Food:') 
{ 
    echo "<td><input type='checkbox' /></td>"; 
    echo "<td><select id='ddName' onchange='showUser(this.value)'>"; 
    echo "<option id='none'>Select a Food:</option>"; 
    while($ddrow = $dd->fetch_assoc()) 
    { 
     if($ddrow['Name'] != $q) 
      { 
      echo "<option value='".$ddrow['Name']."'>".$ddrow['Name']."</option>"; 
      } 
     else 
      { 
      echo "<option value='".$ddrow['Name']."' selected='selected'>".$ddrow['Name']."</option>"; 
      } 
    } 
    echo "</select></td>\n"; 
    echo "<td></td>"; 
    echo "<td></td>"; 
    echo "<td></td>"; 
    echo "<td></td>"; 
    echo "</tr>"; 
} 

else{ 

while($row = $result->fetch_array()) 
    { 
    echo "<td><input type='checkbox' /></td>"; 
    echo "<td><select id='ddName' onchange='showUser(this.value)'>"; 
    echo "<option id='none'>Select a Food:</option>"; 
     while($ddrow = $dd->fetch_assoc()) 
    { 
     if($ddrow['Name'] != $q) 
     { 
     echo "<option value='".$ddrow['Name']."'>".$ddrow['Name']." </option>"; 
     } 
     else 
     { 
      echo "<option value='".$ddrow['Name']."' selected='selected'>".$ddrow['Name']."</option>"; 
     } 
    } 
     echo "</select></td>\n"; 
     echo "<td>" . $row['Units'] . "</td>"; 
     echo "<td>" . $row['Amount'] . "</td>"; 
     echo "<td>" . $row['Calories'] . "</td>"; 
     echo "<td>" . $row['Sugar'] . "</td>"; 
     echo "</tr>"; 
    } 
}