Pete
Pete

Reputation: 170

How to run HTML code that has been generated with PHP?

I made a PHP function that creates the html code for making a table based on the user's choice of column and row number.

So the user opens an HTML page, enters the number of columns and rows, and the PHP generates the HTML code for that table.

However, I'd like it so that instead of creating the code for the table, the PHP would just create an HTML table.

How can I achieve this?

HTML File:

<html>
<form action="tableCode.php" method="get" />
<p> Enter the number of rows
  <input type="text" name="row"/>
  and the number of columns:
  <input type="text" name="col"/>
  <input type="submit" name="Submit" value="Submit" />

</p>
</form>
</body>
</html>

PHP File

<?php


$row = $_GET['row'];
$col = $_GET['col'];

function makeTable($row,$col)
{
  $begin = "&lthtml&gt <br> &lthead&gt <br> &ltstyle&gt <br> table, th, td {border: 1px solid black;} 
  <br> &lt/style&gt <br> &lt/head&gt <br> &ltbody&gt <br> &lttable style=width:100%&gt <br>"; 
  for($i = 0; $i < $row; $i++)
  {
    $begin .= "&lttr&gt <br>";
    for($j = 0; $j < $col; $j++)
    {
        $begin .= "&lttd&gt Enter column data here &lt/td&gt <br>";
    }
    $begin .= "&lt/tr&gt <br>"; 
  }
  $begin .= "&lt/table&gt <br> &lt/body&gt <br> &lt/html&gt";
  echo $begin;  
}
makeTable($row,$col);
?>

String returned when calling makeTable(2,2)

<html> 
<head> 
<style> 
table, th, td {border: 1px solid black;} 
</style> 
</head> 
<body> 
<table style=width:100%> 
<tr> 
<td> Enter column data here </td> 
<td> Enter column data here </td> 
</tr> 
<tr> 
<td> Enter column data here </td> 
<td> Enter column data here </td> 
</tr> 
</table> 
</body> 
</html>

Upvotes: 0

Views: 61

Answers (3)

Use an Ajax Call, with Javascript, and just "print" in the PHP, the HTML code for the table, instead of the whole page:

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){
        $.ajax({url: "tableCode.php",type:"get",data:{"row":document.getElementById("rows").value,"col":document.getElementById("cols").value}
            success: function(result){
                $("#div1").html(result);
            }});
    });
});
</script>
</head>
<body>
<p>how many rows?</p><br>
<input id="rows" value="2"/><br>
<p>how many cols?</p><br>
<input id="cols" value="2"/><br>
<div id="div1"><h2>Here will be your table</h2></div>

<button>Get the table!</button>

</body>
</html>

So, your php file now just echo:

<?php
    $row = $_GET['row'];
    $col = $_GET['col'];

    function makeTable($row,$col)
    {
      $begin = "<table style='width:100%'>\n"; 
      for($i = 0; $i < $row; $i++)
      {
        $begin .= "<tr>\n";
        for($j = 0; $j < $col; $j++)
        {
            $begin .= "<td> Enter column data here </td>\n";
        }
        $begin .= "</tr>\n"; 
      }
      $begin .= "</table>\n";
      echo $begin;  
    }
    makeTable($row,$col);
    ?>

Upvotes: 0

Pinx0
Pinx0

Reputation: 1258

HTML is a markup language, it doesn't have to be "run".

You are outputing the table using "&lthtml&gt" instead of <html> so that is why it is not working.

Upvotes: 0

j08691
j08691

Reputation: 207861

Change all your &lt to < and all your &gt to > and remove the <br> from your code as that would generate invalid HTML. If you care about the way the underlying HTML renders you can change the <br> to \n.

For example:

$begin .= "&lttr&gt <br>";

becomes

$begin .= "<tr>";

or

$begin .= "<tr>\n";

Upvotes: 4

Related Questions