user2154508
user2154508

Reputation: 345

How to create an HTML document with PHP?

Yes, i want to create an .html or .php (whatever) document with some forms.

Example:

The user opens a page (preference: php) and see 1 form for website title and 1 button. Ok, he put the website title of your preference an click in the button. This page send the information contained in the form and create an html/php document with the title that user put in form.

This is not about to show some variables in html with "echo", is about to really create some new document.

Anyone?

Upvotes: 2

Views: 4016

Answers (4)

Styphon
Styphon

Reputation: 10447

This should do what you want:

$title = $_POST['title'];
$html = '<html>
    <head>
        <title>' . htmlspecialchars($title) . '</title>
    </head>
    <body>Some HTML</body>
</html>';
file_put_contents('/path/to/file.html', $html);

That will create a file for you with the title the user submitted (assuming you are submitting by POST).

Upvotes: 3

Hazem Mohamed
Hazem Mohamed

Reputation: 564

You would echo the contents as usual but you should send the following headers:

header('Content-Disposition: attachment; filename=file.html'); //or whatever name you like here
header('Content-Type: text/html'); //or text/php if the file is php

This will make the browser open a download dialoug for your file

Upvotes: 0

Pattle
Pattle

Reputation: 6016

So your form will need to be something like

<form action="" method="post">
    <input type="text" name="pageTitle" id="pageTitle" />
    <input type="submit" name="formSubmit" value="Submit" />
</form>

Then you will need to following code to capture the value of the page title and create the html page

if($_POST['formSubmit']) {
    $newpage = '<html><head><title>' . $_POST['pageTitle'] . '</title></head><body><-- Whatever you want to put here --></body></html>';
    file_put_contents('newpage.html', $newpage );
}

Upvotes: 2

ncrocfer
ncrocfer

Reputation: 2570

Try this :

<?php

$title = htmlspecialchars($_POST['title']);
$content = "<html><head><title>$title</title></head><body>Your content here</body></html>";

file_put_contents('index.html', $content);

Upvotes: 0

Related Questions