【问题标题】:contact email form in php [duplicate]php中的联系电子邮件表格[重复]
【发布时间】:2015-11-14 08:17:14
【问题描述】:

我正在制作一个网站,我想设置一个联系电子邮件表格。像这样的东西:contact email form image

我明白这段代码会这样做吗? 但是当我单击提交按钮时,什么也没有发生。 我以前从未使用过php,所以我不知道我做错了什么。

<?php 
if(isset($_POST['submit'])){
    $to = "mecinoart@hotmail.com"; // this is your Email address
    $from = $_POST['email']; // this is the sender's Email address
    $first_name = $_POST['first_name'];
    $subject = "Form submission";
    $subject2 = "Copy of your form submission";
    $message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['message'];
    $message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message'];

    $headers = "From:" . $from;
    $headers2 = "From:" . $to;
    mail($to,$subject,$message,$headers);
    mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
    }
?>

<!DOCTYPE html>
<head>
<title>Form submission</title>
</head>
<style>
#from{
	font-weight: bold;
	font-family: Trajan Pro;
	font-size: 14px;
	color: #66441c;
	}
.input{
	outline: none;
	margin: 5px 0 10px 0;
	padding-left: 10px;
	width: 230px;
	height: 35px;
	font-family: Adobe Garamond Pro;
	font-size: 14px;
	color: #A4A4A4;
	border-radius: 5px;
	border: 1px solid #a37f4f;
	}
textarea {
	padding-top: 10px;
	}	
#message{
	outline: none;
	margin: 5px 0 10px 0;
	padding-left: 10px;
	width: 230px;
	height: 60px;
	font-family: Adobe Garamond Pro;
	font-size: 14px;
	color: #A4A4A4;
	border-radius: 5px;
	border: 1px solid #a37f4f;
	}	
#button{
	margin-top: -10px;
	border: none;
	outline: none;
	border-radius: 5px;
	width: 230px;
	height: 35px;
	font-family: Trajan Pro;
	font-size: 14px;
	color: #fff;
	background-color: #a37f4f;
	}
#button:hover {
  background-color: #401b0f;
  color: #a37f4f;
}		
</style>
<body>

<form id="from" action="" method="post">
Name <input class="input" type="text" name="first_name"><br>
Email <input class="input" type="text" name="email"><br>
Message <br><textarea id="message" rows="5" name="message" cols="30"></textarea><br>
<input id="button" type="submit" name="submit" value="Submit">
</form>

</body>
</html> 

【问题讨论】:

  • 没有显示成功消息,也没有错误检查或报告,因此如果失败,您将不会收到错误消息。确认的唯一方法是您是否收到了电子邮件。缩小范围后,您可以尝试多种方法。

标签: php forms email contact


【解决方案1】:

表单的actiononsubmit 属性为空,因此表单信息永远不会在任何地方提交。您需要指定将表单内容发送到哪个页面(使用action="...")或指定要执行的javascript函数(使用onsubmit="..."),否则什么都不会当您单击 Submit 按钮时发生。

【讨论】:

    【解决方案2】:

    我有类似的问题

    我用在form Tag

    action="<?php echo $_SERVER['PHP_SELF']; ?>" 
    

    【讨论】:

      【解决方案3】:

      尝试检查邮件是否发送。

      <? php
      if (isset($_POST['submit'])) {
        $to = "mecinoart@hotmail.com"; // this is your Email address
        $from = $_POST['email']; // this is the sender's Email address
        $first_name = $_POST['first_name'];
        $subject = "Form submission";
        $subject2 = "Copy of your form submission";
        $message = $first_name.
        " ".$last_name.
        " wrote the following:".
        "\n\n".$_POST['message'];
        $message2 = "Here is a copy of your message ".$first_name.
        "\n\n".$_POST['message'];
      
        $headers = "From:".$from;
        $headers2 = "From:".$to;
        $email1 = mail($to, $subject, $message, $headers);
        $email2 = mail($from, $subject2, $message2, $headers2); // sends a copy of the message to the sender
      }
      if ($email1) {// checking if email 1 is sent
        echo "Email 1 is sent";
      } else {
        echo "Email 1 is not sent";
      }
      if ($email2) {//checking if email 2 is sent
        echo "Email 2 is sent";
      } else {
        echo "Email 2 is not sent";
      } ?>
      

      将此添加到您的表单属性中。 action="&lt;?php echo $_SERVER['PHP_SELF']; ?&gt;" 例如:&lt;form method="POST" action="&lt;?php echo $_SERVER['PHP_SELF']; ?&gt;"&gt;

      【讨论】:

        【解决方案4】:

        你为什么使用$last_name?删除它或添加一个新的姓氏文本框。

        【讨论】:

          猜你喜欢
          • 2013-11-02
          • 1970-01-01
          • 1970-01-01
          • 2021-10-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多