views:

93

answers:

1

Okay, How do you generate your ajax-forms?

Do you keep the <form>-code in the javascript-files and load the data with ajax (e.g. json), or do you load a generated html-file (with all the <form> and <input> that you just push to the browser)? Or is there another simpler way?

If you use any framework, do you get your forms generated automatically from your models?

What do you find easiest to keep up and maintain? Is there any plug-in or libraries you find useful, maybe something to jQuery?

Share your thoughts!

+1  A: 

There is a solution in the jQuery library

 jQuery.get( url, [data], [callback], [type] )

Load a remote page using an HTTP GET request. This is an easy way to send a simple GET request to a server without having to use the more complex $.ajax function. It allows a single callback function to be specified that will be executed when the request is complete (and only if the response has a successful response code). If you need to have both error and success callbacks, you may want to use $.ajax.

$.get() returns the XMLHttpRequest that it creates. In most cases you won't need that object to manipulate directly, but it is available if you need to abort the request manually.

Have a look at the jQuery Documentation

Example:

$.get("someForm.pl", { name: "John" },
  function(data){
    $(data).appendTo(document.body); // you might place it somewhere else
  });

Edit:

Example where you only change the values of the existing dom:

<form id="myForm"><input id="myName" /></form>

$.get("someForm.pl", { name: "John" },
  function(data){
    $("#myForm").each(function(){
      this.value = data[this.id];
    });
  },"json");

Where your server response would:

{ 'myName' : 'John' }
Ghommey
So in the someForm.pl you print out the `<form>` and everything? Why isn't it better to have that in the javascript and set the value using $("#myinput").val(data.myfield) ?
Erik
Sry I didn't understand you question correctly. It would be faster to use the existing DOM and to change only the values. You can use $.get to request JSON data as well.
Ghommey
Yeah, the thing is that you now have 2 files, the javascript, and the one that outputs your data. And when you should add another field, you must do changes in both of your files, and I find that annoying.
Erik
No as you see 'myName' is only stored in the HTML and in the JSON but not in the javascript.
Ghommey