如何创建新的.html网页onclick? [英] How do I create a new .html webpage onclick?

查看:83
本文介绍了如何创建新的.html网页onclick?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做的是当用户单击我的html按钮时,我想在服务器上动态创建一个新的网页.我需要新网页的url拥有自己的url,也就是说,它们应该彼此不同.

What I want to do is when the user clicks my html button I want to dynamically create a new webpage on the server. I need the url of the new webpage to have it's own url, that is, it should be different from each other.

更详细:当用户单击按钮时,我想将新的.html文件上传到服务器.因此,例如,如果我有一个名为www.check.com/index.html的网站,当用户单击index.html中的按钮时,我需要创建一个新的.html,其中包含一些html行(但没有CSS,则不会有任何CSS在页面上显示).因此,当用户单击按钮时,我希望使用唯一的URL(例如www.check/1.html)将文件上传到服务器,第二次它将是check.com/2.html,依此类推.可以使用1.html,2.html等.

In more detail: When the user clicks the button I want to upload a new .html file to the server. So for example if I have a website called www.check.com/index.html when the user clicks the button in the index.html I need to create a new .html that contains some html lines (but no CSS, nothing will be shown on the page). So when the user clicks the button I want a file to be upload to the server with a unique url like www.check/1.html, the second time it'll be check.com/2.html and so on.. I'm fine with it being 1.html, 2.html and so on.

JavaScript代码:

Javascript code:

 function makePage(){
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState==4 && xmlhttp.status==200)
    alert("webpage " + xmlhttp.responseText + " was successfully created!");
}
var content = "<html><head></head><body><meta name=\"twitter:card\" content=\"summary_large_image\"><meta name=\"twitter:site\" content=\"@nytimes\"><meta name=\"twitter:creator\" content=\"@SarahMaslinNir\"><meta name=\"twitter:title\" content=\"Parade of Fans for Houston’s Funeral\"><meta name=\"twitter:description\" content=\"Blah Blah Blah content.\"><meta name=\"twitter:image\" content=\"\"><script>document.getElementById(\"imgTweet\").innerHTML.write(img);</script></body></html>";
xmlhttp.open("GET","makePage.php?content=" + content,true);
xmlhttp.send();}

'img'是我的另一个页面中的变量.通过代码创建的网页未执行.

'img' is a variable in a different page of mine. The webpage gets created by the code is not executed.

推荐答案

您必须使用 AJAX 执行php.使用 file_put_contents()在php中创建新文件非常简单.

You must use AJAX to execute php. Creating new file in php is really simple with file_put_contents().

这是一个示例:
makePage.html:

here is an example:
makePage.html:

    <html>
    <body>
    <button onclick="makePage()">click</button>
    <script src="makePage.js">
    </script>
    </body>
    </html>

makePage.php:

makePage.php:

<?php
$content = $_GET["content"];
$file = uniqid() . ".html";
file_put_contents($file, $content);
echo $file;
?>

makePage.js

makePage.js

function makePage(){
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function(){
    if(xmlhttp.readyState==4 && xmlhttp.status==200)
        alert("webpage " + xmlhttp.responseText + " was successfully created!");
    }
    var content = "<html><head><meta charset=\"utf-8\" /> </head><body>new website<script>alert(\"test\")</script></body></html>";
    xmlhttp.open("GET","makePage.php?content=" + content,true);
    xmlhttp.send();
}

希望这会有所帮助

这篇关于如何创建新的.html网页onclick?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆