PHP form not displaying errors, just goes blank

It sends the form but when there’s an error it does not filter or show validation errors.

This php is not on the same page as form…it is separate…

<form method="post" action="http://www.domain.com/submit/send2.php">
  <?php
//let's start the session
session_start();

        $errFirstName ="";
        $errLastName ="";
        $errCellPhone ="";
        $errEmail ="";
        
    if ($_POST["submit"]) {
        $FirstName = filter_var($_POST['FirstName'], FILTER_SANITIZE_STRING);        
                $LastName = filter_var($_POST['LastName'], FILTER_SANITIZE_STRING);
                $Email = filter_var($_POST['Email'], FILTER_SANITIZE_EMAIL);
        $Amount = filter_var($_POST['Amount'], FILTER_SANITIZE_NUMBER_INT);
        $CellPhone = $_POST['CellPhone'];
                $to = 'yourdomain@gmail.com';
        $cc_tomail = 'user@domain.com';
        $from = 'From: User<admin@domain.com>';
        $host = "mail.domain.com";
                $username = "admin@domain.com";
                $password = "hissyfit";        
                $subject = 'Main subject';
        $headers = "From: $from \r\n";
                $headers .= "Reply-To: $Email \r\n"; 
        
        $body = "From: $FirstName $LastName\n E-Mail: $Email\n Cell Phone: $CellPhone\n Phone: $HomePhone\n Amount: $Amount\n;
        
        
    $autoResponse = true; //if set to true auto response email will be sent, if you don't want autoresponse set it to false
    $autoResponseSubject = "Your request from User **Not Spam **"; 
    $autoResponseMessage = "Hello, thank you for sending your request. We will contact you within 24 to 48 hours.";
    $autoResponseHeaders = "From: noreply@domain.com"; 
 
        // Check if name has been entered
        if ($_POST['FirstName'] != "") {
    $_POST['FirstName'] = filter_var($_POST['FirstName'], FILTER_SANITIZE_STRING);
    if ($_POST['FirstName'] == "") {
        $errors .= 'Please enter a valid first name.<br/><br/>';
    }
} else {
    $errors .= 'Please enter your first name.<br/>';
}
        
        // Check if last name has been entered
        if ($_POST['LastName'] != "") {
    $_POST['LastName'] = filter_var($_POST['LastName'], FILTER_SANITIZE_STRING);
    if ($_POST['LastName'] == "") {
        $errors .= 'Please enter a valid last name.<br/><br/>';
    }
} else {
    $errors .= 'Please enter your last name.<br/>';
}

// Check if last name has been entered
        if ($_POST['Amount'] != "") {
    $_POST['Amount'] = filter_var($_POST['Amount'], FILTER_SANITIZE_NUMBER_INT);
    if ($_POST['Amount'] == "") {
        $errors .= 'Please enter the amount.<br/><br/>';
    }
} else {
    $errors .= 'Please enter your amount.<br/>';
}
        
        
        
        // Check if email has been entered and is valid
        if (!$_POST['Email'] || !filter_var($_POST['Email'], FILTER_VALIDATE_EMAIL)) {
            $errEmail = 'Please enter a valid email address';
        }
        
        if (!empty($_POST['CellPhone'])) {
  $CellPhone = $_POST['CellPhone'];
  $pattern = "/^[0-9\_]{7,20}/";
  if (preg_match($pattern,$CellPhone)){ $CellPhone = $_POST['CellPhone'];}
  else{ $errors[] = 'Your Cell Phone number can only be numbers.';}
  }

// If there are no errors, send the email
if (!$errName && !$errEmail && !$errCellPhone) {
    if (mail ($to, $subject, $body, $from)) {
     header( 'Location: http://www.domain.com/thank-you' ) ;
     //   $result='<div class="alert alert-success">Thank You! You will be contacted within 24 hours, allow 48 hours on weekends</div>';
    } else {
        $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again </div>';
    }
}
    }
    
ob_end_flush();
?>

There’s some weird things going on. In some areas you build a string called $errors and append the message onto it, for example:

$errors .= 'Please enter a valid last name.<br/><br/>';

but then a bit later, you start treating it as an array:

$errors[] = 'Your Cell Phone number can only be numbers.';

But then when you’re deciding whether to send the mail, you check an entirely different set of variables to see if there was an error:

// If there are no errors, send the email
if (!$errName && !$errEmail && !$errCellPhone) {

anything else weird going on? I corrected that.but same problem. - Thans

If you’ve not already done so crank up PHP’s error reporting to maximum. My guess is that you’ve got a parser error somewhere or possibly something has triggered the headers to have already been sent by the time session_start() is called

well, the form is continued from another page. 1st page goes throgh and then 2nd part of form is on second page

This doesn’t look right:

        $from = 'From: User<admin@domain.com>';
...
...
        $headers = "From: $from \r\n";

So your header will start “From: From: User admin@domain.com” - would that cause trouble?

And why do you assign $firstname, $lastname and so on at the top of the script, but then validate them later on after you’ve built up the mail body? How far through the php code does it get, and will the spaces before the opening php tag cause trouble with your header redirect?

I have moved the validation code up befre posting and still no change. It does not validate The header creates no problem at all.

Is this line:

     //   $result='<div class="alert alert-success">Thank You! You will be contacted within 24 hours, allow 48 hours on weekends</div>';

meant to be commented out?

How far through the code does it get? That is, if you add echo()s into the code to break it down, what’s the last line it executes, if any? Is it even starting to run?

It goes all the way through each time. Problem is it doesn’t check the phone number at all, and if anythng is missing it just goes blank… I picked up this form here – https://bootstrapbay.com/blog/working-bootstrap-contact-form/ but it has problems I guess.

I don’t have the php on the same page as the html

You would save a lot of wasted time installing xdebug and stepping through the code yourself to find the issue(s). Speculation of what it *could or couldn’t be is SUCH a waste of time when you could just install a smart debugger and figure it out by stepping through the code.

Doesn’t check the phone number, or does check it but doesn’t spot the error you expect it to?

That form doesn’t have a Cellphone field, are you sure that part is correct? Can you post your html form and the current version of the php now you’ve updated it?

@oddz , Xdebug sounds interesting, but don’t you have to add calls to that as well? Or will it trace through the code with no code changes? I had a brief look at something similar when it was mentioned, but I can’t remember if it was that specific product or not.

No code changes are necessary to use xdebug. Once installed you need an IDE that has xdebug integration. Eclipse, Netbeans, and PHPStorm all provide integrations with xdebug.

That form was used as a template only and wanted to edit fields as necessary.

All I can say really - hard to suggest any more without seeing the form and the current version of the code.

@oddz - thanks, I may have a look at this. So, no integration with Wordpad then? :slight_smile:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.