0

Is there a way to create a new HTML page from Javascript based on user inputs?

I have a webpage that asks which input fields user wants, based on the selection it should create a new HTML page.

i.e. If i select 3 textboxes and 1 button then it should create new HTML page with 3 textboxes and 1 button.

how to do the same thing using PHP?

edit: my objective is newly created page should also be saved on the server at the time of creation

4
  • Of course. You can create a complete DOM tree with Javascript. Start reading here. But I guess your question is if there's a library to make this easy and maintainable? Commented Mar 7, 2012 at 12:42
  • Yes it is possible to add new elements to page using javascript. Commented Mar 7, 2012 at 12:42
  • my objective is newly created page should also be saved on the server at the time of creation Commented Mar 7, 2012 at 12:51
  • @Parthpatel: If you're going to completely change the question (which your comment above does), best to use the "edit" link and do it properly. I've done it for you on this occasion. Commented Mar 7, 2012 at 13:08

4 Answers 4

2

Update:

You've added a comment to your question:

my objective is newly created page should also be saved on the server at the time of creation

That completely changes your question. To create a file on the server, you'll have to involve a server-side language (which could be JavaScript, via NodeJS or Rhino or several other projects) as well as JavaScript on the client. You'll need to post the user's choice to the server and generate the file there.


Original answer: (Prior to the comment above)

Yes, you can do that. You can either show them a page where they make these choices and then replace that page's content with what they asked for, or you can open a new window and show their selection there.

In either case, you'd probably use the DOM:

...and/or a good JavaScript library like jQuery, Prototype, YUI, Closure, or any of several others to help smooth over browser differences and provide significant utility functionality.

Here's a really minimalist example using only DOM and JavaScript (no libraries, but I do strongly recommend using one, the code would be leaner and more robust):

Live copy | Live source

HTML:

<div id="question">
<label>How many text boxes would you like?
<select id="numboxes">
    <option value="1">One</option>
    <option value="2">Two</option>
    <option value="3" selected>Three</option>
</select></label>
<input type="button" id="btnGo" value="Go">
</div>

JavaScript:

(function() {

  document.getElementById("btnGo").onclick = genPage;

  function genPage() {
    var sel = document.getElementById("numboxes"),
        num = parseInt(sel.options[sel.selectedIndex].value, 10),
        counter,
        box;

    document.getElementById("btnGo").onclick = "";
    document.body.removeChild(document.getElementById("question"));
    for (counter = 0; counter < num; ++counter) {
      box = document.createElement('input');
      box.type = "text";
      document.body.appendChild(box);
    }
  }

})();
Sign up to request clarification or add additional context in comments.

Comments

1

No, it's not possible using only browser-based code. The code in the browser runs on the client machine. It has no way to save a file on the server by itself. You will have to use some code on the server to achieve such thing.

You have two options:

  1. Create new window with the new content - once closed it will be gone as you can't save it to the server but for all other matters it will act as real HTML page.

  2. Using simple server side logic you can use AJAX to interact with it and create the pages.

7 Comments

You underestimate Javascript. And the OP isn't asking how to create a new HTML file on the server.
create new HTML page - sorry, to me it sounds like he is asking to create new HTML file on the server.
What the OP wants (having dynamically created controls that the user can post) doesn't require creating a file on the server. As long as you have something on the server that you can post to!
"JavaScript is client side language" No, no, a thousand times no. JavaScript is just a language. It is no more tied to the client-side browser than Java is tied to thick applications written with Spring. NodeJS and Rhino are just two examples of JavaScript on the server. Heck, even classic ASP a'la 1996 supported server-side JavaScript, as did Netscape server even earlier. I wrote whole apps back in the date using JavaScript and ASP. I still use JavaScript server-side (and in console scripts) nearly every day.
@MrLister yep, matter of long years of experience of newbie questions. :)
|
0

There are several ways to achieve this. One way is simply creating the page dynamically from your server using whichever templating or page creation mechanisms you have in place. You could use e.g. jquery or zeptojs for an ajax request and then reload the current page with the new elements returned from the server. Another way would be to create a js (jquery and similar libraries are good for this stuff) function which changes the html elements after the submit button has been clicked.

Check out jquery

Comments

0

Below are some crude step by you can achieve this (only if your's is plain .html page)

  1. Add a blank div to your html page e.g.<div id="page"></div> (all the controls will be added in this container).
  2. Capture the user entered control info into some variables.
  3. Run a for loop over (count can be calculated based on number of controls user wants) $('#page').html(\\plain html code to add controls)

Comments

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.