4

This is my first time using AJAX i have been reading up on it, and it is also my first time doing this with js. I think i have confused myself along the way.

I am trying to dynamically create a new restaurant page, so every time a administrator clicks the onclick button a new webpage is created, with the content from the new restaurant page, which i have already created.

At the moment i have gotten as far as on pressing a button, a new webpage is created succesfully, however, i have no idea how to access the new webpage i also wanted to display a link to the newly created webpage as it is created, like for example using before. in js to show the dynamic feature before my o'clock button for example.

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

Any suggestions? guidance or related pages i can read up on. Anything will be extremely appreciated.

1 Answer 1

2

In your js do something like this instead of alert:

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
}

This is plain javascript, but using jquery you can do it easier with ajax or get functions.

Sign up to request clarification or add additional context in comments.

1 Comment

Great! by looking at it, it looks like what i am looking for. Thank you for taking out your time. I will try it a little later, as well as look at the links you provided

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.