I have a page page1.html
with a form. I POST
and receive the values of the inputs on page2.php
.
To explain the matter the inputs will send: (Values are NOT always the same)
$title = "This is a Title";
$url = "www.this_is_url.com";
$message = "This is a message";
Then (page2.php
):
$title = $_POST['title'];
$url = $_POST['url'];
$message = $_POST['message'];
So I take the values and with the function myfunction()
create and print a new page (with all the tags of a website, from <Doctype>
until </html>
)
What I want is to display this "new page" (without creating a file) on a div
or other element of the page2.php
.
So here´s the code of page2.php
<?php
if($_POST)
{
function myfunction() {
$title = $_POST['title'];
$url = $_POST['url'];
$message = $_POST['message'];
echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><head><title>Title WEB</title><meta http-equiv="content-type" content="text/html;charset=utf-8" />';
echo '<style> body { width: 100%; margin: 0; padding: 0; } </style></head><body>';
echo "<br />" . $title . "<br /><br />" . $url . "<br /><br />" . $message;
echo "</body></html>"
}
echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><head><title>Title WEB</title><meta http-equiv="content-type" content="text/html;charset=utf-8" />';
echo '</head><body>';
echo "<div>";
myfunction();
echo "</div>";
echo "</body></html>"
}
?>
A function cannot be defined inside an "if" block. You can call it within an "if" block, but should not define.
Here's the working code.
function myfunction() {
$title = $_POST['title'];
$url = $_POST['url'];
$message = $_POST['message'];
echo "<br>" . $title . "<br >". $url . "<br />" . $message;
}
if(!empty($_POST['title']) && !empty($_POST['url']) && !empty($_POST['message']) )
{
myfunction();
}
It's a good idea to use empty() with $_POST
You can also use isset()
function myfunction() {
$title = $_POST['title'];
$url = $_POST['url'];
$message = $_POST['message'];
echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><head><title>Title WEB</title><meta http-equiv="content-type" content="text/html;charset=utf-8" />';
echo '<style> body { width: 100%; margin: 0; padding: 0; } </style></head><body>';
echo "<br />" . $title . "<br /><br />" . $url . "<br /><br />" . $message;
echo "</body></html>";
}
if(isset($_POST['title'] && $_POST['url'] && $_POST['message'])) {
myfunction();
}