【问题标题】:Can't find error PHP找不到错误PHP
【发布时间】:2018-03-28 20:24:09
【问题描述】:

所以,这是我第一次尝试 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 的代码并稍作修改。

【问题讨论】:

  • 是的,你稍微编辑了一下。你在代码里把first_name改成了fname等,你在表单上改了吗???
  • 使用error_reporting(E_ALL); ini_set('display_errors', '1');,它会告诉你。
  • @AbraCadaver 我有自己的表格,这就是为什么我更改了这些变量并且名称正确对应我可以向您保证
  • 你的表格method="post"?
  • @AbraCadaver 是的

标签: php email


【解决方案1】:

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

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; ?>

现在,开始测试。

【讨论】:

    猜你喜欢
    • 2014-02-21
    • 2011-04-16
    • 1970-01-01
    • 2015-03-20
    • 2018-06-06
    • 2023-03-29
    • 2018-12-28
    • 2014-09-07
    • 1970-01-01
    相关资源
    最近更新 更多