无法找到错误PHP

时间:2018-03-28 20:24:10

标签: php email

所以,这是我在PHP中的第一次尝试,因此可能很容易修复。我正在尝试制作一份可以提交的表格并发送给管理部门的电子邮件。

我正在使用MAMP运行服务器,在线填写表单,提交它并收到错误:

We are very sorry, but there were error(s) found with the form you submitted. These errors appear below.

We are sorry, but there appears to be a problem with the form you submitted.

Please go back and fix these errors.

完整的PHP代码:

<?php
if(isset($_POST['email'])) {

    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to = "I HAVE CORRECT EMAIL HERE 4 SURE";
    $email_subject = "eShop Contact Form";

    function died($error) {
        // your error code can go here
        echo "We are very sorry, but there were error(s) found with the form you submitted. ";
        echo "These errors appear below.<br /><br />";
        echo $error."<br /><br />";
        echo "Please go back and fix these errors.<br /><br />";
        die();
    }


    // validation expected data exists
    if(!isset($_POST['fname']) ||
        !isset($_POST['lname']) ||
        !isset($_POST['email']) ||
        !isset($_POST['subject'])) {
        died('We are sorry, but there appears to be a problem with the form you submitted.');
    }



    $first_name = $_POST['fname']; // required
    $last_name = $_POST['lname']; // required
    $email_from = $_POST['email']; // required
    //$country = $_POST['country']; // not required
    $comments = $_POST['subject']; // required

    $error_message = "";
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

    if(!preg_match($email_exp,$email_from)) {
        $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
    }

    $string_exp = "/^[A-Za-z .'-]+$/";

    if(!preg_match($string_exp,$first_name)) {
        $error_message .= 'The First Name you entered does not appear to be valid.<br />';
    }

    if(!preg_match($string_exp,$last_name)) {
        $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
    }

    if(strlen($comments) < 2) {
        $error_message .= 'The Comments you entered do not appear to be valid.<br />';
    }

    if(strlen($error_message) > 0) {
        died($error_message);
    }

    $email_message = "Form details below.\n\n";


    function clean_string($string) {
        $bad = array("content-type","bcc:","to:","cc:","href");
        return str_replace($bad,"",$string);
    }



    $email_message .= "First Name: ".clean_string($first_name)."\n";
    $email_message .= "Last Name: ".clean_string($last_name)."\n";
    $email_message .= "Email: ".clean_string($email_from)."\n";
//    $email_message .= "Country: ".clean_string($country)."\n";
    $email_message .= "Subject: ".clean_string($comments)."\n";

// create email headers
    $headers = 'From: '.$email_from."\r\n".
        'Reply-To: '.$email_from."\r\n" .
        'X-Mailer: PHP/' . phpversion();
    @mail($email_to, $email_subject, $email_message, $headers);
    ?>

    <!-- include your own success html here -->

    Thank you for contacting us. We will be in touch with you very soon.

    <?php

}
?>

这是应该有问题的部分:

    // validation expected data exists
    if(!isset($_POST['fname']) ||
        !isset($_POST['lname']) ||
        !isset($_POST['email']) ||
        !isset($_POST['subject'])) {
        died('We are sorry, but there appears to be a problem with the form you submitted.');
    }

P.S我在这里复制了http://www.freecontactform.com/email_form.php的代码,并对其进行了一些编辑。

1 个答案:

答案 0 :(得分:0)

好吧,现在我将从这里开始。 我将重新编辑您的代码

<强> SUBMIT.PHP

<?php
if(isset($_POST['formid'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "yourmail.com.id@maisetting.com";
$email_subject = "eShop Contact Form";
//
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$mail = $_POST['email'];
$subject = $_POST['subject'];
$comments = $_POST['comm'];
// I was replace this line
// validation expected data exists
if(empty($fname) or empty($lname) or empty($mail) or empty($subject) or empty($comments)){
$err_msg = 'We are sorry, but there appears to be a problem with the form you submitted.';
} else {
// check mail registered
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$mail)) {
$err_msg = 'The Email Address you entered does not appear to be valid.<br />';
} else {
// check name
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$fname)) {
$err_msg = 'The First Name you entered does not appear to be valid.<br />';
} else {
// check Last name
if(!preg_match($string_exp,$lname)) {
$err_msg = 'The Last Name you entered does not appear to be valid.<br />';
} else {
// Check comment
if($comments<2 or $comments=='') {
$err_msg = 'The Comments you entered do not appear to be valid.<br />';
} else {
// SEND msg from the visitor
$err_msg = 'Your Messages Was Send. ThankYou!';
$email_message = "Form details below.\n\n";

function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}

$email_message .= "First Name: ".clean_string($fname)."\n";
$email_message .= "Last Name: ".clean_string($lname)."\n";
$email_message .= "Email: ".clean_string($email)."\n";
//    $email_message .= "Country: ".clean_string($country)."\n";
$email_message .= "Subject: ".clean_string($comments)."\n";

// create email headers
$headers = 'From: '.$email."\r\n".
'Reply-To: '.$email."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);

}}}}} } else { $err_msg = 'Add Your comment here'; }
?>

<强>的index.php

<?php require('SUBMIT.PHP'); ?>
<form action="#" method="post" />
<p><label>First Name</label>
<input type="text" placeholder="First name here" name="fname" />
</p>
<p><label>Last Name</label>
<input type="text" placeholder="Last name here" name="lname" />
</p>
<p><label>Mail</label>
<input type="text" placeholder="Add yourmail here" name="mail" />
</p>
<p><label>Subject</label>
<input type="text" placeholder="Subject here" name="subject" />
</p>
<p><label>Your Comment</label>
<textarea name="comm"></textarea>
</p>
<p><input type="submit" name="formid" value="Send"  /></p>
</form>
<?php echo $err_msg; ?>

现在,做并测试。