15 Mar, 2008
Submit is Not a Function (and getting links to submit all forms in CubeCart)
Posted by: Jennifer In: cubecart|Javascript Related
"Why am I getting that Javascript error?? WTH is it talking about – submit IS a function!!"
So here's the deal – if you have a form and an element in the form is named "submit" – if you try to call document.myform.submit() – you'll end up getting the "submit is not a function" javascript error. (Because to javascript – "submit" is now that object element in your form – not a function)
So the simple solution is if you plan on using the javascript function submit() – do not name any of your form elements "submit".
That's all well and good except if you're working on code that isn't completely yours – and if the PHP code to process the form is specifically looking for $_POST['submit'] like so:
if (isset($_POST['submit'])) { // process form }
then you now have another problem.
This was the case I ran into with CubeCart recently. Most of the forms do not require a submit element to be in the form in order to process it – but a handful did. The design I was working on needed all the buttons designed and to look the same. So my options were:
1) Just use the regular input type="submit" button on those forms. (Ok – but then the site is inconsistent)
2) search for all instances of (isset($_POST['submit']) in the code and change it to some other element I can add to the page… ie:
<input type="hidden" name="formsubmitted" />
and then in the code:
if (isset($_POST['formsubmitted'])) { // process form }
(Obviously this is not recommended in the case of CubeCart as it will make it really annoying to maintain/upgrade the cart!)
3) add that other "formsubmitted" element I noted above to the pages that need it – then towards the top of the MAIN index.php page (which is called with all pages on the store) add the following:
if (isset($_POST['formsubmitted'])) $_POST['submit'] = 1;
Thereby setting the value of $_POST['submit'] so it will process the form…
Another tip with using css-styled links for buttons that will submit forms in CubeCart – you don't need to use document.FORMNAME.submit() – from any form you can use their "submitDoc('FORMNAME')" function like so:
<a href="javascript:submitDoc('FORMNAME');" class="myButtonStyle">Send Form</a>
Just make sure the form has a name (some of them don't).