Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

form action with javascript

I have a form that must execute a javascript function on submit, the function then posts data to my php send mail file and the mail is sent. But it only works in fire fox. The form action does not seem to be doing anything in IE, my question is: Is this the correct way to call a function from an external file from the form action:

action="javascript:simpleCart.checkout()" 

simpleCart is the .js file and checkout() is the function.

Tips appreciated, struggling to understand why it would work in firefox but not IE, chrome or safari.

<form name=form onsubmit="return validateFormOnSubmit(this)" enctype="multipart/form-data" action="javascript:simpleCart.checkout()" method="post"> 
like image 575
whispering_Jack Avatar asked May 09 '12 17:05

whispering_Jack


People also ask

Can you use JavaScript for forms?

With JavaScript at your side, you can process simple forms without invoking the server. And when submitting the form to a CGI program is necessary, you can have JavaScript take care of all the preliminary requirements, such as validating input to ensure that the user has dotted every i.

What is form action?

The HTML form action attribute defines where to send the form data when a form is submitted in an HTML document.

What is form attr (' action ')?

The HTML form action attribute defines what should happen to data when a form is submitted on a web page. The value of the action attribute should be the URL of the web resource that will process the contents of the form.


1 Answers

A form action set to a JavaScript function is not widely supported, I'm surprised it works in FireFox.

The best is to just set form action to your PHP script; if you need to do anything before submission you can just add to onsubmit

Edit turned out you didn't need any extra function, just a small change here:

function validateFormOnSubmit(theForm) {     var reason = "";     reason += validateName(theForm.name);     reason += validatePhone(theForm.phone);     reason += validateEmail(theForm.emaile);      if (reason != "") {         alert("Some fields need correction:\n" + reason);     } else {         simpleCart.checkout();     }     return false; } 

Then in your form:

<form action="#" onsubmit="return validateFormOnSubmit(this);"> 
like image 86
Ja͢ck Avatar answered Oct 14 '22 16:10

Ja͢ck