[Solved] use php in html file [closed]


html does not have variables by itself.
In order to save data you will need a programming language like PHP or GAE with Python for example. You can either save the data into a database or into a simple file using php. You can then load the data from the file into a table with a simple ‘for’ loop.

Like you mentioned, you will take the data from $_POST and save it to file. (data.txt)
And then display data.txt in a table format.

To save data:

<?php

$filename = "someData.txt";
$text = $_POST['form_field_name_here'] . ' ' .  $_POST['second_field_name_here']; //just continue with as many fields as necessary    
$fp = fopen ($filename, "a+");
if ($fp) {
    fwrite ($fp, $text);
    fclose ($fp);
}

To print a table from a file you can use a for loops like this:

<html>
<head>
    <link rel="stylesheet" type="text/css" href="https://stackoverflow.com/questions/10868987/style.css" />
</head>
<body>
    <table id="myTable">
        <?php // PHP code starts here
        $f = fopen("someData.txt", "r");
        while (($line = fgets($f)) !== false) {
            echo "<tr>";
            $cells = explode(" ", $line);
            foreach ($cells as $c) {
                echo "<td>" . htmlspecialchars($c) . "</td>";
            }
            echo "<tr>\n";
        }
        fclose($f); 
        // end of PHP code
        ?>
    </table>
</body>
</html>

And here’s your style.css

#myTable  
{
    border: solid 1px black;
}

28

solved use php in html file [closed]