Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Form submit to run javascript and then post to php script

Tags:

javascript

php

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.

like image 409
Boardy Avatar asked Feb 26 '12 14:02

Boardy


People also ask

Can JavaScript send data to PHP?

The way to pass a JavaScript variable to PHP is through a request. This type of URL is only visible if we use the GET action, the POST action hides the information in the URL. Server Side(PHP): On the server side PHP page, we request for the data submitted by the form and display the result. $result = $_GET [ 'data' ];

How get data from form submit in PHP?

How to retrieve form data sent via GET. When you submit a form through the GET method, PHP provides a superglobal variable, called $_GET. PHP uses this $_GET variable to create an associative array with keys to access all the sent information ( form data ). The keys is created using the element's name attribute values.

How can I use JavaScript and PHP together?

JavaScript is used as client side to check and verify client details and PHP is server side used to interact with database. In PHP, HTML is used as a string in the code. In order to render it to the browser, we produce JavaScript code as a string in the PHP code.


1 Answers

Set 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

like image 166
Tchoupi Avatar answered Oct 26 '22 23:10

Tchoupi