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

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
ParthPatel
  • 108
  • 1
  • 11
  • Of course. You can create a complete DOM tree with Javascript. Start reading [here](http://reference.sitepoint.com/javascript/Document). But I guess your question is if there's a library to make this easy and maintainable? – Mr Lister Mar 07 '12 at 12:42
  • Yes it is possible to add new elements to page using javascript. – arunes Mar 07 '12 at 12:42
  • my objective is newly created page should also be saved on the server at the time of creation – ParthPatel Mar 07 '12 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. – T.J. Crowder Mar 07 '12 at 13:08

4 Answers4

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

})();
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
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.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
  • 3
    You underestimate Javascript. And the OP isn't asking how to create a new HTML file on the server. – Mr Lister Mar 07 '12 at 12:44
  • **create new HTML page** - sorry, to me it sounds like he is asking to create new HTML file on the server. – Shadow The GPT Wizard Mar 07 '12 at 12:47
  • 1
    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!_ – Mr Lister Mar 07 '12 at 12:51
  • 1
    *"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. – T.J. Crowder Mar 07 '12 at 13:04
  • @MrLister yep, matter of long years of experience of newbie questions. :) – Shadow The GPT Wizard Mar 07 '12 at 13:38
  • @T.J strictly speaking you are correct. But 99.9999% the author of this question has no idea of all this and I was answering his question for his eyes first of all and didn't want to complicate things. Will try to see how I can edit so it will reflect what you said. – Shadow The GPT Wizard Mar 07 '12 at 13:40
  • @ShadowWizard: I would still change it markedly, but that's me. Regarding the 99.9999%: All the more reason **not** to reinforce that mistaken belief. – T.J. Crowder Mar 07 '12 at 13:45
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

joidegn
  • 1,078
  • 9
  • 19
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)
Misam
  • 4,320
  • 2
  • 25
  • 43