html动态表打印到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.

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. The function to save it as a pdf is in there as well. But I am mainly after the printing of the form to the php file. I am having no luck there at all!

I'm not worried on posting the table to mysql. I have other ways where i don't need it in mysql

I would really appreciate the help

2 things

  • you have not closed tr

Next

Make all input elements as array. Then you can fetch them in your php file

 <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.

  $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). 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. Just give them name="step[]" and the form will automatically count the index for you.

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.

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

Now use foreach loop.

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.

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