gpt4 book ai didi

javascript - PHP/JS/AJAX/HTML : Dynamically creating a webpages

转载 作者:可可西里 更新时间:2023-10-31 23:28:40 24 4
gpt4 key购买 nike

这是我第一次使用 AJAX,我一直在阅读它,这也是我第一次使用 js。我想我一路上都把自己弄糊涂了。

我正在尝试动态创建一个新的餐厅页面,因此每次管理员单击 onclick 按钮时都会创建一个新网页,其中包含我已经创建的新餐厅页面中的内容。

目前我已经按下一个按钮,成功创建了一个新网页,但是,我不知道如何访问新网页我还想显示一个指向新创建网页的链接,因为它被创建,例如之前使用。例如,在 js 中显示我的点钟按钮之前的动态功能。

HTML

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

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

PHP

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

有什么建议吗?我可以阅读的指南或相关页面。任何事情都将不胜感激。

最佳答案

在你的 js 中做这样的事情而不是警告:

if(xmlhttp.readyState==4 && xmlhttp.status==200){
var createA = document.createElement('a');
var createAText = document.createTextNode(xmlhttp.responseText); // or whatever name you need
createA.setAttribute('href', xmlhttp.responseText);
createA.appendChild(createAText);
document.body.appendChild(createA); // or you can create some <div> or whatever and append it to that
}

这是普通的 javascript,但是使用 jquery 你可以用 ajax 更容易地做到这一点或 get功能。

关于javascript - PHP/JS/AJAX/HTML : Dynamically creating a webpages,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37128178/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com