[英]List with elements from database in cloned jquery <tr>
我有一个js脚本,每次单击按钮时,该脚本就会将html元素插入页面。 在这些输入之一(选择)中,我想显示从数据库检索的选项列表。 我有正确的代码,但是我不知道如何组合它们,因为我无法将PHP放入JS。
我的代码:
var rowCount = 0; function add_product() { var $table = $('table'); var generateRow = function(indx){ $table.append('<tr>\\n\\ <td></td>\\ <td>\\ <b>Aantal: </b><input style="width: 60px;" id="aantal" name="aantal_'+indx+'" type="number" min="0" required>\\ </td>\\ <td>\\ <b>Product: </b><select name="products_'+indx+'" type="text" required>\\n\\ </select>\\ </td>\\ </tr>'); } generateRow(++rowCount); }
<?php $productQuery = mysql_query("SELECT * FROM voorraad") while ($productInfo = mysql_fetch_array($productQuery)) { $productID = $productInfo[0]; $product = $productInfo[1]; echo "<option value='$productID'>$product</option>"; } ?>
您可以这样做:
jQuery.ajax({
type: "POST",
url: 'your_functions_address.php',
dataType: 'json',
data: {functionname: 'add', arguments: [1, 2]},
success: function (obj, textstatus) {
if( !('error' in obj) ) {
yourVariable = obj.result;
}
else {
console.log(obj.error);
}
}
});
从:
这不是完成您要达到的目标的最佳方法,但是使用您已经开始的方法就可以像这样完成。
首先,您需要将options字符串放入一个PHP变量中,然后将该选项字符串放入一个javascript变量中。
这还假定您的PHP和Javascript都在同一文档中,并且您的PHP代码在文档中的Javascript之上。
<?php
$options = '';
$productQuery = mysql_query("SELECT * FROM voorraad")
while ($productInfo = mysql_fetch_array($productQuery))
{
$productID = $productInfo[0];
$product = $productInfo[1];
$options .= "<option value='$productID'>$product</option>";
}
?>
var options = "<?php echo $options; ?>";
var rowCount = 0;
function add_product()
{
var $table = $('table');
var generateRow = function(indx){
$table.append('<tr>\n\
<td></td>\
<td>\
<b>Aantal: </b><input style="width: 60px;" id="aantal" name="aantal_'+indx+'" type="number" min="0" required>\
</td>\
<td>\
<b>Product: </b><select name="products_'+indx+'" type="text" required>\n\
' + options + ' </select>\
</td>\
</tr>');
}
generateRow(++rowCount);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.