用POST创建HTML表 [英] Creating HTML table with POST
问题描述
我正在尝试创建一个接受用户输入的网页,将其发布到创建页面,然后创建另一个在html表中显示数据的网页.我正在使用file_put_contents来创建网页,并且每当尝试包含循环以从3 td中获取内容进行输出时,都会收到错误消息.到目前为止,这就是我所拥有的.我会提到我之前发布的内容,但是我删除了该帖子,因为该帖子令人困惑,缺少细节和示例.
I am trying to create a webpage that takes user input, posts it to a creation page and then creates another webpage displaying the data in a html table. I am using file_put_contents to create the webpage and I am getting an error whenever I try to include a loop to get the content from 3 td's to be output. Here is what I have so far. I will mention I posted earlier, however I deleted that as that post was confusing, lacked detail and examples.
这是用户的输入字段(不发布deleteBox)用户将输入:成分,数量和注释(txt是一个空变量)
This is the input field for user (deleteBox does not get posted) The user will input: An ingredient, the amount and a note (txt is an empty variable)
<tr><td><input type='checkbox' name='deleteBox'></td><td>" + "<input type='text' name='ingredient[]' value='" + txt + "'/>" + "</td><td>" + "<input type='text' name='amount[]' value='" + txt + "'/>" + "</td> + <td>" + "<input type='text' name='note[]' value='" + txt + "'/>" + "</td></tr>
我已经尝试了下面的方法,将for循环放入变量中,然后将其直接输入到$ strOut中.我真的不知道这是否应该做(显然不行,因为它不起作用->在第42行[$ tbl行]上给意外错误'for')并且找不到工作大约.我如何使数组数据显示在file_put_content的输出中,以免产生错误?
I've tried the below with both the for loop in a variable and directly entering it into the $strOut. I don't really know if this is how you're supposed to do it (clearly not, as it doesn't work -> gives error unexpected 'for' on line 42 [$tbl line]) and cannot find a work around. How would I make the array data display in the output for file_put_content so that it doesn't create errors?
$ingredient = $_POST['ingredient'];
$amount = $_POST['amount'];
$note = $_POST['note'];
$tbl = for ($i = 0; $i < count($ingredient); $i++) {
echo '<tr>';
echo '<td>' . $ingredient[$i] . '</td>';
echo '<td>' . $amount[$i] . '</td>';
echo '<td>' . $note[$i] . '</td>';
echo '</tr>';
}
$strOut = '<!DOCTYPE html>'
. '<table class="recipe-ingredients">'
. '<tr>'
. '<th class="table-th-large">Ingredient</th>'
. '<th class="table-th-medium">Amount</th>'
. '<th class="table-th-xl">Note</th>'
// User input tr goes here
. $tbl
. '</table>';
// Closing html tags after
file_put_contents("example.php", $strOut);
任何建议或解决方法将不胜感激.
Any suggestions or work arounds would be much appreciated.
推荐答案
$tbl = '';
for ($i = 0; $i < count($ingredient); $i++) {
$tbl.= '<tr>';
$tbl.= '<td>' . $ingredient[$i] . '</td>';
$tbl.= '<td>' . $amount[$i] . '</td>';
$tbl.= '<td>' . $note[$i] . '</td>';
$tbl.= '</tr>';
}
使用它.顺便说一句,您忘记在标题行中添加结束</tr>
.
Use this.
By the way, You forgot to put a closing </tr>
in the heading row.
这篇关于用POST创建HTML表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!