这是我第一次使用AJAX,我一直在阅读它,这也是我第一次使用js.我想我一直困惑自己.
我正在尝试动态创建一个新的餐馆页面,因此每次管理员点击onclick按钮时,都会创建一个新网页,其中包含我已经创建的新餐厅页面中的内容.
目前我已经按下按钮,成功创建了一个新网页,但是,我不知道如何访问新网页我还希望在创建时显示新创建的网页的链接,例如使用之前.在js中显示我的时钟按钮之前的动态功能.
click
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 = "
new website";xmlhttp.open("GET","makePage.php?content=" + content,true);
xmlhttp.send();
}
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
}
这是普通的javascript,但使用jquery,您可以使用ajax或get函数轻松完成.
标签:php,javascript,ajax,html
来源: https://codeday.me/bug/20190702/1354447.html