让我再试一次,所以我希望我的下拉菜单中包含它所提取的数据。然后,如果需要,可以添加更多行。
所以本质上我想选择一个项目,它在同一行显示项目的属性。然后单击按钮添加更多行。
表格如下所示:
这是我到目前为止所做的:
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>";
}
答案 0 :(得分: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>";
}
}