Copy link to clipboard
Copied
i have a registration form, which checks for errors before the form is submitted. i was told then to use an if else statement to submit the form on the same page, but i would prefer to have the user go to a confirmation page instead. i have the form submitting to itself to check for errors. any ideas as to how i can do this?
Copy link to clipboard
Copied
Should be simple enough.
<?php
if(isset($_POST['submit'])) // when your submit button has name="submit"
{
// code for your confirmation page
}
else
{
// code for your page with the form
}
?>
Nick
Copy link to clipboard
Copied
ok, so here is how i modified it...
<?php
if(isset($_POST['register']) && empty($errors)) // when your submit button has name="submit"
{
echo "the form will have been submitted!";
}
else
{
echo "registration form displayed here...";
}
?>
i have an error array that prints out what the user needs to fill in yet. i have to make sure that it is empty before the form is submitted. am i doing this right? when i run it with empty fields, it prints out, " the form will have been submitted"...
Copy link to clipboard
Copied
here is how i modified the code:
<?php
if(isset($_POST['register']) && empty($errors)) // when your submit button has name="submit"
{
echo "the form will have been submitted!";
}
else
{
echo "registration form displayed here...";
}
?>
i want to make sure that the user did not leave any empty fields. but i get " the form will have been submitted" when i submit it with empty fields....
Copy link to clipboard
Copied
I would recommend condensing the code to something like this:
<?php
if(isset($_POST['register']) && !empty($_POST['field1']) && !empty($_POST['field2']) ... ) // when your submit button has name="submit"
{
echo "the form will have been submitted!";
}
else
{
echo "registration form displayed here...";}
?>
Where the registration form code would look something like this:
<form method="post" action="self.php">
<input type="text" name="field1" size="20"><?php if(isset($_POST['resubmit']) && empty($_POST['field1'])) echo "This field must be filled out."; ?>
<input type="text" name="field2" size="20"><?php if(isset($_POST['resubmit']) && empty($_POST['field2'])) echo "This field must be filled out."; ?>
...
<input type="hidden" name="resubmit">
<input type="submit" name="register" value="Register">
</form>
Obviously, you would want to add in any formatting things (like tables, divs, etc), but this should work code-wise.
Nick
Copy link to clipboard
Copied
but since i have about 25 fields, wouldn't that be a little tedious? can't i just check if the errors array is empty, and if so, submit the form?
i currently have:
$to = 'email@yahoo.com';
$subject = 'registration form';
$mesage = 'name: $FName $LName';
if (empty($errors)){ mail($to, $subject, $message);?>
<div class="green">Thanks for the submission</div>
<?php
unset($errors);
i would prefer to take them to a confirmation page to do all of the sending there. (i would not prefer to use javascript for checking the fields, since they may not have it installed, or just may have it blocked).
Copy link to clipboard
Copied
Where is the code for filling up the errors array?
Nick
Copy link to clipboard
Copied
i think i actually just put the required fields into an array, and checked if they were in there, and if they were, and that field was empty, an error message was thrown.
Copy link to clipboard
Copied
Ahh, so the problem is that you are not checking whether each field is filled, but whether atleast one field is filled. Here's another solution:
<?php
$valid = true;
foreach ($errors as &$value)
{
if ($value == ""){
$valid = false;
}
}if(isset($_POST['register']) && $valid)
{
echo "the form will have been submitted!";
}
else
{
echo "registration form displayed here...";
}
?>
Nick
Copy link to clipboard
Copied
ok, so here is how i modified the code:
<?php
$valid = true;
foreach ($errors as $value)
{
if ($value == "")
{
$valid = false;
}
}
if(isset($_POST['register']) && $valid)
{
echo "the form will have been submitted";
}
else
{
echo "registration form with errors displayed here...";
}
?>
it did not like the & sign before the $value. when i submit the form with empty fields, i still get " the form will have been submitted"
here is what i have for the error-checking array:
if (array_key_exists('register',$_POST)){
// Fields that are on form
$expected = array('FName', 'LName', 'phone', 'cell', 'street-address', 'state', 'zip', 'email', 'emergecnycontactname', 'pphonenumber', 'relationship', 'aphonenumber');
// Set required fields
$required = array('FName', 'LName', 'phone', 'cell', 'street-address', 'state', 'zip', 'email', 'emergencycontactname', 'pphonenumber', 'relationship');
// Initialize array for errors
$errors = array();
foreach ($_POST as $field => $value){
// Assign to $temp and trim spaces if not array
$temp = is_array($value) ? $value : trim($value);
// If field is empty and required, tag onto $errors array
if (empty($temp) && in_array($field, $required)) {
array_push($errors, $field);
}
}
Find more inspiration, events, and resources on the new Adobe Community
Explore Now