Never Stop Learning
Never Stop Learning

Reputation: 775

Table Generator

Hi I have a question about generating rows and columns. would like to ask about how can I make it in one page only. This is what I have tried.

HTML:

<html>
<head>
<title>Table Generator</title>

<body>
<center><h1>Generate Your Table</h1></center>

<div id="div1">
<center><h4>Enter number of Row and Column</h4>
    <form action="get_table/execute_table.php" method="POST">
    <label for="title">Row</label>
    <input type="text" name="title1" placeholder="Row">
    <br>
    <label for="title">Column</label>
    <input type="text" name="title2" placeholder="Column">
    <br>
    <input type="submit" name="submit" value="Generate Table"> </center>
    </form>

</div>



</body>

PHP:

<?php

$row = $_POST['title1'];
$column = $_POST['title2'];

echo "<table border='1'>"; 

for($tr=1;$tr<=$row;$tr++){ 

echo "<tr>"; 
    for($td=1;$td<=$column;$td++){ 
           echo "<td>row: ".$tr." column: ".$td."</td>"; 
    } 
echo "</tr>"; 
} 

echo "</table>"; 
?>

Yes, it is fully running but I want it in 1 page only. Thanks.

Upvotes: -1

Views: 975

Answers (3)

CyberEd
CyberEd

Reputation: 875

You need to post to the same page, and check if the PhP Post array has data yet.

Replace the form action with:

<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="POST">

Note: although most (all?) browsers support self-posts with a blank action, this is not technically W3C compliant.

Then when the page is submitted it will reload the same page and populate the POST array. Somewhere on your page add a condition similar to:

if($_POST['title']){
  //do whatever get_table/execute_table.php did
}else{
  //echo the form here or, if you're allowed, use an include()
}

More info on self-posting: How do I make a PHP form that submits to self?

Upvotes: 1

Gokigooooks
Gokigooooks

Reputation: 794

to achieve this by using only one page just leave the action attribute empty. and add your php block on top.and make sure to save it as .php and place your php code block on top of all html


so in the end it will look like this

<?php

    $row = $_POST['title1'];
    $column = $_POST['title2'];

    echo "<table border='1'>"; 

    for($tr=1;$tr<=$row;$tr++){ 

    echo "<tr>"; 
        for($td=1;$td<=$column;$td++){ 
               echo "<td>row: ".$tr." column: ".$td."</td>"; 
        } 
    echo "</tr>"; 
    } 

    echo "</table>"; 
    ?>

    <html>
    <head>
    <title>Table Generator</title>

    <body>
    <center><h1>Generate Your Table</h1></center>

    <div id="div1">
    <center><h4>Enter number of Row and Column</h4>
    <form action="" method="POST">
    <label for="title">Row</label>
    <input type="text" name="title1" placeholder="Row">
    <br>
    <label for="title">Column</label>
    <input type="text" name="title2" placeholder="Column">
    <br>
    <input type="submit" name="submit" value="Generate Table"> </center>
    </form>

    </div>



    </body>

Upvotes: 1

Kevin
Kevin

Reputation: 41885

Normally, if you want it on the same page, you just omit the action="" with its value.

Then of course, put the php process in the same page as the form:

<div id="div1">
<center><h4>Enter number of Row and Column</h4>
    <form action="" method="POST">
    <!--      ^^ no more value, well you could just put the same filename -->
    <label for="title">Row</label>
    <input type="text" name="title1" placeholder="Row">
    <br>
    <label for="title">Column</label>
    <input type="text" name="title2" placeholder="Column">
    <br>
    <input type="submit" name="submit" value="Generate Table"> </center>
    </form>

</div>

<?php

$out = ''; // initialize a string holder and when the submission is done, concatenate all the strings
if(isset($_POST['submit'])) { // catch submission button

    $row = $_POST['title1'];
    $column = $_POST['title2'];

    $out .= "<table border='1'>";

    for($tr=1;$tr<=$row;$tr++){

    $out .= "<tr>";
        for($td=1;$td<=$column;$td++){
               $out .= "<td>row: ".$tr." column: ".$td."</td>";
        }
    $out .= "</tr>";
    }

    $out .= "</table>";

}

echo $out; // finally echo it

Upvotes: 2

Related Questions