PHP表单(使用POST)不提交或发送电子邮件,只显示空白页面

PHP表单(使用POST)不提交或发送电子邮件,只显示空白页面

问题描述:

I have the following HTML:

<form method="post" action="https://www.domain.co.uk/v2/contact.php" id="contactform">
              <p><label for="name"><span class="required">*</span> Your Name</label><br>
              <input name="name" type="text" id="name" size="30" value="" class="input" required> </p>
              <p><label for="email"><span class="required">*</span> Email</label><br>
              <input name="email" type="email" id="email" size="30" value="" class="input" required></p>
              <p><label for="phone">Phone</label><br>
              <input name="phone" type="tel" id="phone" size="30" value="" class="input"></p>
              <p><label for="subject">Subject</label><br>
              <select name="subject" id="subject" class="input">
              <option value="Not sure" selected="selected">Not sure</option>
                <option value="this">this</option>
                <option value="that">that</option>
              </select></p>              
              <p><label for="comments"><span class="required">*</span> Message</label><br>
              <textarea name="comments" cols="40" rows="3" id="comments" class="input" required></textarea></p>
              <p><input type="submit" class="submit" id="submit" value="Submit"></p>
              </form>

and the following in contact.php

<?php

    if ($_POST['name'] != "") {
    $_POST['name'] = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
    if ($_POST['name'] == "") {
        $errors .= 'Please enter a valid name.<br/><br/>';
    }
    } else {
        $errors .= 'Please enter your name.<br/>';
    }

    if ($_POST['email'] != "") {
    $email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $errors .= "$email is <strong>NOT</strong> a valid email address.<br/><br/>";
    }
    } else {
        $errors .= 'Please enter your email address.<br/>';
    }

    if ($_POST['phone'] != "") {
    $_POST['phone'] = filter_var($_POST['phone'], FILTER_SANITIZE_STRING);
    if ($_POST['phone'] == "") {
        $errors .= 'Please enter your phone number.<br/>';
    }
    } else {
        $errors .= 'Please enter your phone number.<br/>';
    }

    if ($_POST['subject'] != "") {
    $_POST['subject'] = filter_var($_POST['subject'], FILTER_SANITIZE_STRING);
    if ($_POST['subject'] == "") {
        $errors .= 'Please choose a subject.<br/>';
    }
    } else {
        $errors .= 'Please choose a subject.<br/>';
    }

    if ($_POST['comments'] != "") {
    $_POST['comments'] = filter_var($_POST['comments'], FILTER_SANITIZE_STRING);
    if ($_POST['comments'] == "") {
        $errors .= 'Please enter a message.<br/>';
    }
    } else {
        $errors .= 'Please enter a message.<br/>';
    }


    if (!$errors) {
            $mail_to = 'info@myemails.co.uk';
            $subject = 'Enquiry';
            $message .= 'Regarding: ' . $_POST['subject'] . "

";
            $message .= 'Name: ' . $_POST['name'] . "

";
            $message .= 'Email: ' . $_POST['email'] . "

";
            $message .= 'Phone: ' . $_POST['phone'] . "

";
            $message .= 'Message ' . $_POST['comments'] . "

";

            $success = mail($mail_to, $subject, $message, "From: <$email>");

            if ($success){
              print "sent";
            }
            else{
              print "failed";
            }
    }

?>

no matter what I change or try I end up on a blank white page for contact.php instead of seeing the sent or failed message (having removed my javascript validation incase I was causing issue there), likewise there is nothing in the error log and despite having gone back over the code I can't spot the issue? Unsure if I have stared at it for too long and missing something obvious or there is a deeper problem?

Any pointers appreciated.

var_dump shows it is getting the information:

array(5) { ["name"]=> string(10) "Joe Bloggs" ["email"]=> string(16) "joe@anyemail.com" ["phone"]=> string(11) "07123456789" ["subject"]=> string(8) "Not sure" ["comments"]=> string(17) "test message here" }

You used string concatenation, but you didn't defined your variables before that, if you change your code, like this:

<?php

        $errors = '';

        if ($_POST['name'] != "") {
        $_POST['name'] = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
        if ($_POST['name'] == "") {
                $errors .= 'Please enter a valid name.<br/><br/>';
        }
        } else {
                $errors .= 'Please enter your name.<br/>';
        }

        if ($_POST['email'] != "") {
        $email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
                $errors .= "$email is <strong>NOT</strong> a valid email address.<br/><br/>";
        }
        } else {
                $errors .= 'Please enter your email address.<br/>';
        }

        if ($_POST['phone'] != "") {
        $_POST['phone'] = filter_var($_POST['phone'], FILTER_SANITIZE_STRING);
        if ($_POST['phone'] == "") {
                $errors .= 'Please enter your phone number.<br/>';
        }
        } else {
                $errors .= 'Please enter your phone number.<br/>';
        }

        if ($_POST['subject'] != "") {
        $_POST['subject'] = filter_var($_POST['subject'], FILTER_SANITIZE_STRING);
        if ($_POST['subject'] == "") {
                $errors .= 'Please choose a subject.<br/>';
        }
        } else {
                $errors .= 'Please choose a subject.<br/>';
        }

        if ($_POST['comments'] != "") {
        $_POST['comments'] = filter_var($_POST['comments'], FILTER_SANITIZE_STRING);
        if ($_POST['comments'] == "") {
                $errors .= 'Please enter a message.<br/>';
        }
        } else {
                $errors .= 'Please enter a message.<br/>';
        }

        $message = '';
        if (empty($errors)) {
                        $mail_to = 'info@myemails.co.uk';
                        $subject = 'Enquiry';
                        $message .= 'Regarding: ' . $_POST['subject'] . "

";
                        $message .= 'Name: ' . $_POST['name'] . "

";
                        $message .= 'Email: ' . $_POST['email'] . "

";
                        $message .= 'Phone: ' . $_POST['phone'] . "

";
                        $message .= 'Message ' . $_POST['comments'] . "

";

                        $success = mail($mail_to, $subject, $message, "From: <$email>");

                        if ($success){
                            print "sent";
                        }
                        else{
                            print "failed";
                        }
        } else {
            echo $errors;
        }

?>

It works. You said that you are at live site, so probably errors are not show up, if you want you can add this lines:

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

at the top of your php file, to see errors, but clients also would see these errors. You can add IP Check for your IP around them.