Creating HTML table with POST

落爺英雄遲暮 提交于 2021-02-09 09:19:34

问题


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.

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>

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.


回答1:


$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>';
    }

Use this. By the way, You forgot to put a closing </tr> in the heading row.



来源:https://stackoverflow.com/questions/65228595/creating-html-table-with-post

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!