Why does AJAX stop working when I use <form>...</form>


'recently been playing with AJAX (without jQuery) and surprised that AJAX does not like <form> ... </form>


Sure it does, you just have to attach an onsubmit handler that prevents default behaviour.


I like to NOT use a submit button, in favor of a plain button and use Javascript to perform the actual submit via AJaX.


^ _ ^


I use a submit button and make the form work when Javascript is disabled / borked for whatever reason too - graceful degradation.


We have to fail closed for security reasons. If Javascript is disabled (or otherwise borked), we protect the systems from potential malicious activities. We would rather prevent the information from being submitted than risk being compromised, in any fashion.


^ _ ^


I must not understand. A form is a form. Whether it is submitted via AJAX or a plain old post shouldn’t matter anything, unless the Javascript is doing something very fancy that a regular form cannot?


I work for USG DoD - think paranoia multiplied by 1,000.


^ _ ^


I still do not understand how to add form.

I have in the following example three AJAX form input calls and when form is added the AJAX stops working.



I would do it a bit different.


I would give the inputs name and id attributes, and instead of having onclick in the elements

  • get the input elements
  • add event listeners
  • prevent the default form submit
  • do the XHR
  • if XHR Fails trigger submit event


Many thanks, I look forward to trying your suggestions when on the desktop.


All changes implemented apart from the last item which I will have to investigate later.

The following FireFox JavaScript Toggler was used for testing


The latest version has a JavaScript modal alert() box which pops up only when JavaScript is enabled. - Is it possible to detect when JavaScript is not enabled?

When JavaScript is disabled the form freezes until all the URL results have been retrieved :frowning: The “simple” test takes about 15 seconds to finally render about 20 URLs and 25 seconds for “verbose” to render!

Delighted to say that with JavaScript/Ajax enabled the rendering is progressive and the first result is virtually instant. The total rendering result is also faster because PHP Curl() is used and does not have to wait until the previous process is complete.


Well there’s the <noscript> tag, but it rather depends on what you’re trying to do with that information.

The default structure design would be to code the HTML page as though it has no javascript, and then use javascript to overwrite the HTML with the javascript-enabled versions, because a javascript-enabled client… can actually process said scripts.


I unsuccessfully tried <noscript> when trying to display a message in ajaxOutput div:

 document.getElementById('ajaxOutput').innerHTML += return_data;


It would be more along the lines of:

<form id='myForm' action='noscript.php'>
function doTheAjax(e) { 



Many thanks, I hope to try it early tomorrow before going away for a week to get some sand between my toes :slight_smile:


Unless I’m missing something, the <noscript> tag is used when Javascript is turned off, so any Javascript code within won’t work.


^ _ ^