Forums

Form tag attachment: something like onSubmit() doesn't work

V@lentine 16 Jul, 2010
Hello I need to add a javascript code on my form's submission.
like this :
<form name="Liste1" method="post" action="test.php" onSubmit="PostSelect(this.name)">

I put it there : Form tag attachment: something like onSubmit()
but when I display code it does'nt appear :
<form name="ChronoContact_choixAction" id="ChronoContact_choixAction" method="post" action="http://localhost/SerfaApplications/index.php?option=com_chronocontact&task=send&chronoformname=choixAction&Itemid=45" >

How can I do that ?



Another suggestion : what about adding a means to sort forms by name in Forms Manager in a next version, I now have 3 pages (and I'm not finished) and I'm starting to spend time searching for the form I want to work on !
😛
nml375 16 Jul, 2010
Hi,
The onSubmit event does not work well with the event-handler used by both MooTools and LiveValidation. You'll have to use a piece of javascript-code somewhat like this instead (add the code to the Form HTML):

<?php
/* Fetch a reference to the JDocument object:
 * this allows us to inject the script in the document head rather than using inline scripting
 */
$doc =& JFactory::getDocument();

/* Now create the actual script:
 * Caution with quotes and ticks (" ') since we're doing this in php..
 */

$script = "/* Use the DOMReady event, so that we may be certain any and all form elements are available */
window.addEvent('domready', function() {
/* This uses the MooTools $() function to get a reference to the form, and adds an onSubmit-event.. */

  $('ChronoContact_choixAction').addEvent('submit', function(event) {
/* event is the event that triggered the code.
 * We'll need this as the 'this' keyword isn't available:
 */

    var e = new Event(event);
    PostSelect(e.target.name);
  });
});";

/* Javascript done, now we'll have to add it to the document.. */
$doc->addScriptDeclaration($script);
?>


This code is written from memory, but should work well. Be aware that the 'this' keyword can not be used with the addEvent() method. Instead, the function takes one argument being 'event' (which contains contains all the information on the triggered event, including the target - the equivalent of 'this').

The DOMReady event is only used to wait until the page is fully loaded before adding the onSubmit-event to the form. I use the $() operator (works similar like getElementByID()) from MooTools to get a reference to the form element, though this could be hardcoded.

/Fredrik
V@lentine 23 Aug, 2010
Thank you, it's solved !
This topic is locked and no more replies can be posted.