简体   繁体   中英

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?

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).

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 );
}

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);

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM