javascript - 如何创建新的 .html 网页 onclick?

标签 javascript php html server server-side

<分区>

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

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

Javascript 代码:

 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' 是我的不同页面中的一个变量。代码创建的网页未执行。

最佳答案

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

举个例子:
制作页面.html:

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

制作页面.php:

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

制作页面.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();
}

希望对你有帮助

关于javascript - 如何创建新的 .html 网页 onclick?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32379444/

相关文章:

PHPExcel : generating error due to php output?

javascript - 使用新文本更新占位符文本 onClick/onFocus

Javascript 拆分和符号转义

javascript - VS2012 的 JSLint 扩展无法识别 JSON

javascript - 当您单击该网站包含的 Iframe 内的按钮时,使整个网站变灰

jquery - 如何从 `ul li` 获取值并写入另一个 `ul li`

html - 不同宽度的均匀水平分布的图像?

javascript - 如何从 Lambda 中的回调函数返回 json?

php - MySQL/PHP : Select Where with multiple conditions does not work

php - 获取 HTML 将内容发送到 PHP