让我再试一次,所以我希望我的下拉菜单与它提取的数据位于同一行中。然后可以根据需要添加更多行。
所以本质上我想选择一个项目,它在同一行显示项目的属性。然后单击按钮添加更多行。
该表如下所示:
这是我到目前为止所拥有的:
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>
用于下拉列表的 Ajax:
//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>";
}