Hello, I've written down a small JS function 'checkForm()' to validate the input in a form and alerting, before actually sending data to the PHP server-side script.
In my form, in addition to common input text fields I've got two buttons: the first called "ADD" to submit data and "CANCEL" to abort the insertion, close the form page and come back to the caller.

I've inserted the checkForm() call to the form declaration this way:
<form ... onsubmit="return checkForm()" ... >

Problems comes out when the user doesn't want to finish the form compilation and clicks the "CANCEL" button: void form fields will cause checkForm() to return false, so the submit won't be performed and the page won't be closed, asking the user for useless missing data requirements.

Is there any way to activate checkForm() if and only if the "ADD" button is pressed?
Or some way to detect inside checkForm() which submit button was pressed, so that I can add in the function code a 'return true statement' if I notice the script was called by the "CANCEL" button?

In HTML for 'Cancel' button remove change the property type='submit' to type='button'. This will no cause to call 'checkForm()' method when 'Cancel' button is pressed.

In HTML for 'Cancel' button remove change the property type='submit' to type='button'. This will no cause to call 'checkForm()' method when 'Cancel' button is pressed.

Changing "CANCEL" button type to 'button' instead of 'submit' will cause the page not to refresh when I click "CANCEL".
I need the page to refresh, in order to pass some $_POST values...

Changing "CANCEL" button type to 'button' instead of 'submit' will cause the page not to refresh when I click "CANCEL".
I need the page to refresh, in order to pass some $_POST values...

What Luckychap means could be that you change the type of "CANCEL" to button and add an event to "onclick" to your own customized JavaScript. The script could be to reload the page again or redirect to another page instead of submit the form and go through the server.

What Luckychap means could be that you change the type of "CANCEL" to button and add an event to "onclick" to your own customized JavaScript. The script could be to reload the page again or redirect to another page instead of submit the form and go through the server.

Ok, understood now.
The question then is how can I reload the page and pass a $_POST value using the onclick button feature.
I know how to call inside the onclick a JavaScript function but I've got no idea of how can I pass a $_POST value to the destination page, in my case the same page.

use Ajax

use Ajax

Well, it's not so easy to me... maybe in the future I'll come across some Ajax learning...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.