Login Register

Anyone have a working example of a form with buttons and AJAX?

I would like to have a form with form data and several buttons. When one of those buttons is pushed, I would like the form data **and the button that was pushed** sent back to the server via AJAX.

I have spent the past six hours trying the various examples I've found on this site with no luck. My AJAX code is cut-n-pasted from this site (http://dojotoolkit.org/book/dojo-book-0-9/part-3-programmatic-dijit-and-...) . My form is three elements; two button and an input field. I click the button and nothing happens. However, when I reload the page, the AJAX function fires off!

I have been able to get it to partially work by adding an onClick handler to the button and changing my AJAX function to appear more like this example (http://dojotoolkit.org/book/dojo-book-0-9/part-3-programmatic-dijit-and-...), but the value of the button is not passed with the rest of the form data.
I then used dojo.connect to connect the button to the function. That didn't work either.

This does work, doesn't it?

Anyone have a working example of a form with buttons and AJAX?

After reading the source, I found out that what I want to do is impossible since form buttons are explicitly NOT read by the dojo.formToObj.

But you CAN do this:

// in your form:

Output Files

//and in your header

function sendFNDdata(pressedBtn) {
     var vargetFNDdata = {
         url:"cgi-bin/PortSim/getFNDdata.pl",
         handleAs: "text",

         // Here's the FM!  
         content: { value: pressedBtn }, //<------!!!!!!!!
         form:dojo.byId("fndParms"),     // yes they can be combined
         
         timeout: 5000, // Time in milliseconds
         load: function(response, ioArgs) {...},
         error: function(response, ioArgs) {...}
    };

 dojo.xhrPost(vargetFNDdata);
};

Also, note that the xhrPost is wrapped in together with everything else into one function; that wasn't clear from the docs (at least to me).

I hope this saves someone else six hours of frustration!

--

faber