PHP and JavaScript Form Help

PHP and JavaScript Form Help

Posted by: Joseph Witchard
Posted on: 2008-07-01 08:20:00

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.

Re: PHP and JavaScript Form Help

Posted by: scjessey
Posted on: 2008-07-01 09:19:00

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:

<html>
<head>
<script type="text/javascript">
var error = use your form validation to assign this as true or false
function formSubmit(error) {
if(error == false) {
document.getElementById("myform").submit()
}
}
</script>
</head>
<body>
<form id="myform" action="script.php" method="post">
Your form controls here
<input type="button" onclick="formSubmit()" value="Submit">
</form>
</body>
</html>

-- si-blog --

Re: PHP and JavaScript Form Help

Posted by: Joseph Witchard
Posted on: 2008-07-01 12:31:00

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?

Re: PHP and JavaScript Form Help

Posted by: scjessey
Posted on: 2008-07-02 06:28:00

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).

-- si-blog --

Re: PHP and JavaScript Form Help

Posted by: Joseph Witchard
Posted on: 2008-07-02 14:54:00

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.

Re: PHP and JavaScript Form Help

Posted by: Atropos7
Posted on: 2008-07-02 20:42:00

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 openvein.org -//-

Tags: email formarray