PHP and JavaScript Form Help

software development


So, I’ve got an email form that uses PHP to process it. I also have JavaScript ready to catch invalid fields as well as PHP script. However, using JavaScript, when someone makes an error, an alert window pops up. After you click OK in the alert, though, the PHP still sends the form, which then results in PHP also telling the user that their input is invalid.

Is there any way to cancel the $_POST array with JavaScript like you do with PHP code? I’ve never used JavaScript and PHP together before this, so I wouldn’t know.


Make sure your form has an “id” attribute (I will assume you called it “myform”). Use an input type of “button” instead of “submit” for the form submission, and then use JavaScript to test whether or not to submit the form:


[i]Your form controls here[/i] [/code]-- [url=][b]si-blog[/b][/url] -- [b]Max[/b] discount on [i]any[/i] plan with promocode [url=|SCJESSEYTOTAL][b]SCJESSEYTOTAL[/b][/url]


So if an alert is displayed with onClick, the form will not submit until the errors have been corrected, thus setting the error variable to false? Also, will it still submit using PHP if the user has JavaScript turned off in their browser?


The example I gave will not work on its own (obviously), but it demonstrates what you need to do in order to stop the form from submitting until the errors have been cleared. You will need to use more advanced DOM techniques to get around the problem of disabled JavaScript (ie. use JavaScript to disable a “submit” control).

Max discount on any plan with promocode SCJESSEYTOTAL


But what I’m saying is with my current form script, I’m able to get an error alert when the user submits the form (of course, it’s using input-submit instead of input-button), but after you close out of the alert, PHP still tries to send the form, which then results in an error message being displayed from my PHP validation script.

After the error alert pops up, I just want the form to know not to submit itself after the user clicks “ok” in the JavaScript alert. Once he corrected the errors, obviously, the alert wouldn’t pop up.


You need to capture the onsubmit event (of the form itself, not of a control such as input) instead. If the function returns false, the form will not be submitted.

:cool: -//- One-time [color=#6600CC]$50.00 discount[/color] on [color=#0000CC]DreamHost[/color] plans: Use ATROPOS7