I am currently working on a php/html/javascript project. I have a form where when the user presses the submit button, it should run a javascript function and then afterwards post to a php page to process the data that was submitted in the form.
I have the form run a javascript method as below
<form class="form" id="addImageForm" name="addImageForm" action="javascript:validateAddImage();" method="post">
The method validates the form, calls another method and then it submits the form using document.myForm.submit();
How do I get it to submit the form to another php page to process the data including upload selected files.
Thanks for any help you can provide.
I am currently working on a php/html/javascript project. I have a form where when the user presses the submit button, it should run a javascript function and then afterwards post to a php page to process the data that was submitted in the form.
I have the form run a javascript method as below
<form class="form" id="addImageForm" name="addImageForm" action="javascript:validateAddImage();" method="post">
The method validates the form, calls another method and then it submits the form using document.myForm.submit();
How do I get it to submit the form to another php page to process the data including upload selected files.
Thanks for any help you can provide.
Share Improve this question asked Feb 26, 2012 at 14:55 BoardyBoardy 36.2k108 gold badges271 silver badges457 bronze badges 1- Just pass your page name into action tag and call javascript function on onsubmit event of your form. <form class="form" id="addImageForm" name="addImageForm" action="yourpage.php" method="post" onsubmit="return validateAddImage();"> – Milap Commented Feb 26, 2012 at 15:07
4 Answers
Reset to default 8Set yout 'action' parameter to your PHP script, and do any javascript procesing in a javascript event.
<form class="form" id="addImageForm" name="addImageForm" action="processing.php" method="post" onsubmit="return validateAddImage();">
Then in your js validation, return 'true' if everything's fine, or 'false' if not. Returning 'true' means 'continue with the submit process and send over data to processing.php
What you need is onsubmit.
<form class="form" id="addImageForm" name="addImageForm" action="addImage.php" onsubmit="return validateAddImage();" method="post">
Replace the action
attribute with the name of your PHP page, and have the validateAddImage
method as an onsubmit
event.
Change the action attribute to the address of the php script you're posting to. Call the javascript function in an onsubmit attribute instead of action. But you'll have to prefix it with a return statement.
<form class="form" method="post" action="your/script.php" id="addImageForm" name="addImageForm" onsubmit=return javascript:validateAddImage()>
That way if validateAddImage() returns false, the form won't be submitted. But if it returns true it will.