简体   繁体   English

HTML动态表格打印到PHP

[英]html dynamic table print to php

I need some help... 我需要协助...

I've created a form with a dynamic table in it. 我创建了一个带有动态表的表单。 I would like to learn how to pass it through php and echo/print it to a php page to save the page as pdf. 我想学习如何通过php并将其回显/打印到php页面以将该页面另存为pdf。

I am stuck on this part. 我被困在这一部分。 I've looked everywhere for some answers and I cant seem to be finding anything. 我到处都在寻找一些答案,但似乎找不到任何东西。

<form action="display_form.php" method="POST">
<table id="dataTable" width="auto" style="margin:-4px 0 0 0;" cellspacing="0px">
    <tr>
        <td style="width:20px;"><INPUT type="checkbox" name="chk" /></td>
        <td><INPUT type="text" name="step" style="width:160px;" autocomplete="on" placeholder="step" required/></td>
        <td><INPUT type="text" name="url" style="width:62px;" autocomplete="on" placeholder="url" required/></td>
        <td><INPUT type="text" name="process" style="width:63px;" autocomplete="on" placeholder="process" required/></td>
        <td>
            <SELECT name="pass-fail" style="width:100px;">
                <OPTION value="Pass">one</OPTION>
                <OPTION value="Fail">two</OPTION>
            </SELECT>
        </td>
        <td><INPUT type="text" name="comment" style="width:190px;" autocomplete="on" placeholder="Comment" required/></td>
</table>
<INPUT type="button" value="Add row" onclick="addRow('dataTable')" />
<INPUT type="button" value="Delete row" onclick="deleteRow('dataTable')" />
<INPUT type="submit" value="Send"/>

I have called the php file display_form.php where I would like to display the dynamic table data in the. 我已经调用了php文件display_form.php ,我想在其中显示动态表数据。 The function to save it as a pdf is in there as well. 也有将其另存为pdf的功能。 But I am mainly after the printing of the form to the php file. 但是我主要是在将表格打印到php文件之后。 I am having no luck there at all! 我在那里根本没有运气!

I'm not worried on posting the table to mysql. 我不担心将表发布到mysql。 I have other ways where i don't need it in mysql 我还有其他不需要mysql的方式

I would really appreciate the help 我非常感谢您的帮助

2 things 2件事

  • you have not closed tr 你还没有关闭tr

Next 下一个

Make all input elements as array. 将所有输入元素设置为数组。 Then you can fetch them in your php file 然后,您可以在您的php文件中获取它们

 <td style="width:20px;"><INPUT type="checkbox" name="chk[]" /></td>
    <td><INPUT type="text" name="step[]" style="width:160px;" autocomplete="on" placeholder="step" required/></td>
    <td><INPUT type="text" name="url[]" style="width:62px;" autocomplete="on" placeholder="url" required/></td>
    <td><INPUT type="text" name="process[]" style="width:63px;" autocomplete="on" placeholder="process" required/></td>
    <td>
        <SELECT name="pass-fail[]" style="width:100px;">
            <OPTION value="Pass">one</OPTION>
            <OPTION value="Fail">two</OPTION>
        </SELECT>
    </td>
    <td><INPUT type="text" name="comment[]" style="width:190px;" autocomplete="on" placeholder="Comment" required/></td>

In your php file, use foreach loop to get all the values. 在您的php文件中,使用foreach循环获取所有值。

  $i = 0;
    $array = array();
    foreach ($_POST['step'] as $row) {
        $array[$i]['step'] = $row;
        $array[$i]['chk'] = isset($_POST['ckh'][$i]) ? 1 : 0;
        $array[$i]['url'] = $_POST['url'][$i];
        $i++;
        //...//so on
    }

    print_R($array);

As I can see, the javascript addRow function should add another tr-row in the table (the closing is missing by the way). 如我所见,javascript addRow函数应在表中添加另一个行(顺便说一句,缺少结束符)。 Now you have in every table row inputs with the same names. 现在,在每个表行中,输入都具有相同的名称。 That won't work. 那行不通。

You need to say, that they are arrays, to access them via $_POST['step'][0] and so on. 您需要说,它们是数组,可以通过$ _POST ['step'] [0]等访问它们。 Just give them name="step[]" and the form will automatically count the index for you. 只要给他们起名字=“ =” step []“,表格就会自动为您计算索引。

Use name='step[]',name='url[]',name='process[]' as input type for your add row functionality, Now when you submit form you are getting array then store that array as below. 使用name ='step []',name ='url []',name ='process []'作为添加行功能的输入类型,现在,当您提交表单时,将获取数组,然后按以下方式存储该数组。

$step = $_REQUEST['step']; 
$url = $_REQUEST['url']; 
$process = $_REQUEST['process']; 

Now use foreach loop. 现在使用foreach循环。

foreach($step as $key => $row){
    echo $step[$key];
    echo $url[$key];
    echo $process[$key];
}

Now format your data in html then generate pdf as per below link. 现在,以html格式格式化数据,然后按照以下链接生成pdf。

http://phptopdf.com/ http://phptopdf.com/

if (isset($_POST)) {
$step = $_REQUEST['step'];
$url = $_REQUEST['url'];
$process = $_REQUEST['process'];
$pass_fail = $_REQUEST['pass-fail'];
$comment = $_REQUEST['comment'];

foreach ($step as $key => $row) {
    {
        ?>
        <table id="dataTable" width="auto" style="margin:-4px 0 0 0;" cellspacing="0px">
            <tr>
                <td><INPUT type="text" name="step[]" style="width:160px;" value="<?php echo $step[$key]; ?>"/></td>
                <td><INPUT type="text" name="url[]" style="width:62px;" value="<?php echo $url[$key]; ?>"/></td>
                <td><INPUT type="text" name="process[]" style="width:63px;" value="<?php echo $process[$key]; ?>"/>
                </td>
                <td><input name="pass-fail[]" style="width:100px;" value="<?php echo $pass_fail[$key]; ?>"/></td>
                <td><INPUT type="text" name="comment[]" style="width:190px;" value="<?php echo $comment[$key]; ?>"/>
                </td>
            </tr>
        </table>
    <?php }
}

} }

Working just fine now!! 现在工作正常!

here the table is displaying correctly 这里的表格显示正确

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM