【问题标题】:PHP file is not retrieving values from HTML websitePHP 文件未从 HTML 网站检索值
【发布时间】:2016-12-23 11:29:14
【问题描述】:

我的 PHP 文件没有将值从网站传递到我的电子邮件。

index.html 中的表单代码块:

<!--Contact Form-->

<form id="contact" action="mail.php" method="post" enctype="text/plain">

<input name="name" placeholder="Your name" type="text" tabindex="1" required autofocus>

<input name="email" placeholder="Your Email Address" type="email" tabindex="2" required>

<input name="contact" placeholder="Your Phone Number" type="tel" tabindex="3" required>

<textarea name="message" placeholder="Your Message...." tabindex="4" required></textarea><br>

<input id="submit" name="submit" type="submit" value="Submit"/>
<input id="reset" name="reset" type="reset" value="Reset">

CSS:

#contact input[type="text"],
#contact input[type="email"],
#contact input[type="tel"],
#contact textarea {
  width:75%;
  box-shadow:inset 0 1px 2px #DDD, 0 1px 0 #FFF;
  -webkit-box-shadow:inset 0 1px 2px #DDD, 0 1px 0 #FFF;
  -moz-box-shadow:inset 0 1px 2px #DDD, 0 1px 0 #FFF;
  border:1px solid #CCC;
  background:#FFF;
  border-radius:5px;
  padding:10px 10px;
  font-family:Arial, Helvetica, sans-serif;
  font-size:14px;
}
fieldset {
border:0;
}
#contact button [type="submit"],
#contact button [type="reset"]
{
width:100%;
height:AUTO;
}

PHP 文件:

<?php
//collect data
$name = $_POST['name'];//Your Name
$email = $_POST['email'];//Email ID
$contact = $_POST['contact'];//Contact number
$message = $_POST['message'];//Message

//declare data
$to = "vertika7march@gmail.com";//Recipient Email ID
$subject = "Message from center2enter.com";//Inbox Subject
$body = "Message from-\n Name: $name\n Email: $email\n Contact: $contact\n Message: $message";
mail($to,$subject,$body);//Mail sent

//redirect
echo "<div align='center' style ='font:30px Arial,Helvetica,sans-serif; color:blue'>Thank you for your message. I will be in touch with you very soon!</div>";//Thank you Screen
?>

我收到的邮件输出:

'来自-的消息

姓名:

电子邮件:

联系方式:

消息:'

[未获取在我的网站“center2enter.com”上提交的变量的值]

我试过了:

  • $body = "发件人:($_POST['name'])\n 电子邮件:($_POST['email'])\n 联系人:($_POST['contact'])\n $message = ( $_POST['message'])";

  • $body = ("From:" .$name "\nEmail:" .$email "\nContact:" .$contact "\nMessage:" .message);

我必须使用 PHPMailer 还是可以不用它来发送邮件?

【问题讨论】:

  • 您似乎未验证表单已提交或值有效
  • @JohnConde 它是 enctype ;-)

标签: php html


【解决方案1】:

简单;从您的表单标签中删除enctype="text/plain";使用 POST 数组时,它不是有效的 enctype。

另外,我没有在您的代码中看到结束 &lt;/form&gt; 标记,因此请确保它确实存在。

您还应该使用正确的标头,否则如果没有有效的From: 电子邮件地址,您的代码可能会被误认为垃圾邮件。

如果有人不使用兼容 HTML5 的浏览器,还建议您检查任何空输入; 它可能发生。

我必须使用 PhpMailer 还是不用它也可以发送邮件?

即使您使用 PHPMailer 或任何其他邮件程序,您仍然需要从表单中删除 enctype。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多