Submit button name breaks jQuery

Submit button name breaks jQuery

by Paul P. -
Number of replies: 3

I'm not sure if anyone had this issue, but when using jquery to submit a questionnaire form, the post dies because the submit button is named name="submit". From what I understand, it's never a good idea to name elements with reserved words.

I think the changes to make it work are pretty simple.  I tested it in v1.9.10 and it worked.

// CHANGE IN ./locallib.php
//FROM THIS (line 228)
if (isset($viewform->submit) && isset($viewform->submittype) &&
//TO THIS
if (isset($viewform->qsubmit) && isset($viewform->submittype) &&
//FROM THIS (line 509)
if(!empty($formdata->submit)) {
//TO THIS
if(!empty($formdata->qsubmit)) {
//FROM THIS (line 643)
<input type="submit" name="submit" value="'.get_string('submitsurvey', 'questionnaire').'" /></div>';
//TO THIS
<input type="submit" name="qsubmit" value="'.get_string('submitsurvey', 'questionnaire').'" /></div>';




// CHANGE IN ./report.php
//FROM THIS (line )
echo "<input type=\"submit\" name=\"submit\" value=\"".get_string('download', 'questionnaire')."\" />\n";
//TO THIS
echo "<input type=\"submit\" name=\"qsubmit\" value=\"".get_string('download', 'questionnaire')."\" />\n";

Average of ratings: -
In reply to Paul P.

Re: Submit button name breaks jQuery

by Joseph Rézeau -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers Picture of Translators

Hi Paul,

I do not understand what you mean by "... when using jquery to submit a questionnaire form, the post dies..."

Joseph

In reply to Joseph Rézeau

Re: Submit button name breaks jQuery

by Paul P. -

Hi Joseph,

To clarify, it's not exactly a jQuery issue, it is a javascript issue (thus affecting jQuery as well).  These two links explain it better than I can:

http://thedesignspace.net/MT2archives/000292.html
https://issues.apache.org/jira/browse/TAP5-947

Basically, since "submit" is already a method of the form object, naming the submit button "submit" overrides the method "submit" thus making any calls to form.submit() void.

If you are wondering what I am trying to do with this, basically I am putting a questionnaire form in an HTML block in the front page, and using javascript/jquery to validate the form before sending it off to mod/questionnaire/view.php (this is solely for design purposes - my client wants the questionnaire on the front page).  So I basically hide the submit button and add a regular button that triggers a event when clicked.  In the event function, the form is processed and (theoretically) submitted via javascript to mod/questionnaire/view.php.  This, however, doesn't happen since the submit button is named "submit".

What does happen, is that the button doesn't trigger the event.  If I rename the button to "qsubmit", then the event is triggered, but even if all of the fields are filled out correctly, the form is still rejected and I am presented with the standard questionnaire field screen with all the fields filled out from the screen.

What I think is happening is that somewhere in the validation, the script is checking to see if there is an object named "submit" in the post data.  Thus, we just have to find that check and rename it to "qsubmit" or the like.

Best,

Paul

In reply to Paul P.

Re: Submit button name breaks jQuery

by Joseph Rézeau -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers Picture of Translators

Hi Paul,

Thanks for the detailed explanation. I'm afraid yours is a "special" request, due to the special way you are using the Questionnaire module. I hope you will find the relevant solution for your special use.

ATB

Joseph