![]() Join Up! 96824 members and counting! |
|
|||
AJAX and PHP Part: Forms and JavaScript Limitations
J.D.Campbell
AJAX is a very powerful system which enables dynamic, constantly changing content on a page--without refreshing the page. This useful system does, however, have some limitations with current browsers. We will cover these limitations in this article, specifically those pertaining to how HTML Forms operate within AJAX dynamic content and what type of JavaScript can be used in AJAX-generated content.
The FireFox browser has what actually amounts to a bug when using AJAX pages; if you browse to a Web page which uses AJAX by specifying a URL without the "www", such as http://jdcampbell.com/ajax/ajaxpart4.html, and then try and operate the AJAX-enabled pages you will find they may not work. FireFox will only allow AJAX requests from the same web site as the current HTML page in the browser. The missing "www" causes FireFox to view embedded URLs in AJAX requests as being from a different web site. For the bug to show itself, the embedded URLs would have to be www.jdcampbell.com and the URL you entered in the browser would be jdcampbell.com, without the "www". This bug does not exist in Microsoft IE, however.
AJAX & Form Limitations
First we will cover an example of how to use HTML Forms with AJAX dynamic content. When we say Forms, you should know we are talking about the <Form> tags and the elements within the <Form> tags such as <input type=text name=city> and all other elements. The basic rules are as follows.
Now we will show the example itself. The first is the primary HTML page enabled with AJAX that updates a section of the page (itself) when you click a button. The section that is updated is specified as DIV tags with an ID (shown at the bottom of the listing).
Listing #A: Main HTML page, AJAX enabled
The above div tag <div id="div1"> is automatically updated dynamically by AJAX. The content that is displayed by AJAX when you press the "Press for AJAX" button is the following HTML file content (Listing #B).
Listing #B: Content file displayed by AJAX within above HTML page.
Both Listing #B above and Listing #C below are dynamically displayed using AJAX without refreshing the main page shown in Listing #A. As you can see, Listing #B is a Form with a button that initiates another AJAX request which displays the results of Listing #C below, including the Form field "txt1" contents.
Listing #C: PHP Form request handler, also displayed by AJAX
AJAX & JavaScript Limitations
There is also a clear limitation on how you can use JavaScript within the dynamically updated (AJAX generated) content of a page. You can do as we did in our example above in file Listing #B where we had an onClick event that calls a JavaScript function (which is actually defined in the main HTML page file which is Listing #A).
You can also do the following (note the following is an edited version of Listing #B above; it is displayed as dynamic AJAX content on a page). The following changes the "txt1" field element content with an onClick event that uses a standard JavaScript method, referencing the "txt1" field by ID as you notice on the highlighted line below.
Warning: You cannot do the following, which has a user-defined JavaScript function in the AJAX dynamic content. JavaScript functions defined within dynamic AJAX content simply will not function.
Come back soon to read the next article in our series on Ajax and PHP!
|