PHP代码向我发送了来自服务器的电子邮件,但用户输入为空或“空白”。我只收到:“发件人:\电子邮件:\主题:\消息:”仅此而已。
如何修复我的PHP和/或HTML代码以接收来自表单的用户输入?这是我现有的HTML和PHP代码,不会从表单发送任何“用户输入”。
感谢任何可以提供帮助的人!
答案 0 :(得分:-1)
检查所有变量为空还是不像下面的代码一样,也使用pregmatch验证电子邮件地址
<?php
$errors = '';
$myemail = 'yourname@website.com';//<—–Put Your email address here. if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['message']))
{
$errors .= "\n Error: all fields are required”;
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$message = $_POST['message'];
if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i”, $email_address))
{
$errors .= "\n Error: Invalid email address”;
}
if( empty($errors))
{
$to = $myemail;
$email_subject = "Contact form submission: $name";
$email_body = "You have received a new message. ".
" Here are the details:\n Name: $name \n ".
"Email: $email_address\n Message \n $message";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
header('Location: contact-form-thank-you.html');
}
我希望这对您有用。
答案 1 :(得分:-1)
考虑这是您的表格
<form action="yourpage.php" method="post">
<input type="text" name="name" placeholder="enter name" required>
<input type="submit" name="submit" placeholder="enter name">
确保<form action=""
链接到正确的页面,并且method="post"
应该在此处。
如果当前页面上有表单,即我的情况是yourpage.php,则将表单操作留空。另外,我建议在输入字段的末尾添加必填项,例如
<input type="text" name="name" placeholder="enter your name" required>
因为它迫使用户在其中写入一些值,所以您不会得到空值
接下来将是您的php文件,只需在第一行中添加代码
if(isset($_POST['submit'])) {
上面的代码验证用户单击了“提交”按钮,从而使总体代码看起来像
<?php
if(isset($_POST['submit'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phoneT'];
$call = $_POST['call'];
$website = $_POST['website'];
$priority = $_POST['priority'];
$type = $_POST[itypel];
$message = $_POSTUmessagefl;
$formcontent = " From:$name \n Phone: $phone \n CallBack: $call \n Website: $website \n Priority: $priority \n Type: $type \n Message: $message";
$recipient = "YOUREMAIL@HERE.COM";
$subject - "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!" . " -" . "<a href=iform.htmll
style='text-decoration:none;color:#ff0099;'> Return
Home</a>";
}
?>