- Oct 9, 2002
- 28,298
- 1,236
- 136
This is a simplified version of what I'm working on:
The actual form has MANY more fields and validates required fields with nested conditions. I deliberately designed it so fields aren't cleared when the user submits. This allows the user to print the page or make a quick correction and re-submit. The "Reset" button works fine, but only when it's used before submitting the form. After the form has been submitted, the "Reset" button generates a prompt, but clicking "OK" doesn't clear anything.
After analyzing the code, I know WHY this happens. I believe I can change the reset event to run a javascript function instead of just the prompt, and the function can manually clear each variable.
I'm not trained in HTML, PHP, or web development. I've just been forced to re-write some things. You PHP gurus have been a huge help in the past. Maybe one of you can tell me if there's a better / simpler approach to accomplish the same thing I'm doing here.
Thanks!
PHP:
<html>
<body>
<?php
$field1 = $_REQUEST['field1'];
$submitted = $_REQUEST['submitted'];
?>
<form method='post' action='index.php' onReset="return confirm('Do you really want to clear the form?')">
<label for='field1'>Field 1:<br>
<input name='field1' id='field1' type='text' value='<?php echo $field1; ?>'></label><input id='submit' name='submit' type='submit' value='Submit'> <input id='clear' name='clear' type='reset'>
<input id='submitted' name='submitted' type='hidden' value='<?php echo "$submitted"; ?>'>
<?php
if ($submitted == "yes")
{
// The value of "$submitted" was set, so this is not the first time the page has loaded
// The user has clicked the Submit button
if ($field1=="")
{
// Notify user that required fields must be completed
echo '<script type=\'text/javascript\'>alert(\'Ensure that required fields are completed.\r\n\r\n'.$validation.'\');</script>';
}
else
{
// Send email
$emailto = "fake@recipient.address";
$subject = "[Web Submission]";
$message = "Field 1:\r\n$field1\r\n\r\n";
$headers = "From: No Reply <noreply@nowhere.com>\r\n";
mail($emailto,$subject,$message,$headers);
echo "<script>alert('Form submitted.\\r\\n\\r\\nAn email message has been dispatched to $emailto')</script>";
}
}
else
{
// The value of "$submitted" was not set, so this is the first time the page has loaded
// The user has not clicked the Submit button
echo '<script type=\'text/javascript\'>document.getElementById("submitted").value="yes";</script>';
}
?>
</form>
</body>
</html>
The actual form has MANY more fields and validates required fields with nested conditions. I deliberately designed it so fields aren't cleared when the user submits. This allows the user to print the page or make a quick correction and re-submit. The "Reset" button works fine, but only when it's used before submitting the form. After the form has been submitted, the "Reset" button generates a prompt, but clicking "OK" doesn't clear anything.
After analyzing the code, I know WHY this happens. I believe I can change the reset event to run a javascript function instead of just the prompt, and the function can manually clear each variable.
I'm not trained in HTML, PHP, or web development. I've just been forced to re-write some things. You PHP gurus have been a huge help in the past. Maybe one of you can tell me if there's a better / simpler approach to accomplish the same thing I'm doing here.
Thanks!
Last edited:
