表格的PHP代码不起作用。代码陈述

时间:2016-10-04 12:03:12

标签: php html web

表格的代码是: -

  <form action="mail.php">
  <input class="input-text" type="text" name="name" value="Your Name *" onFocus="if(this.value==this.defaultValue)this.value='';" onBlur="if(this.value=='')this.value=this.defaultValue;">
  <input class="input-text" type="text" name="email" value="Your E-mail *" onFocus="if(this.value==this.defaultValue)this.value='';" onBlur="if(this.value=='')this.value=this.defaultValue;">
  <textarea class="input-text text-area" name="message" cols="0" rows="0" onFocus="if(this.value==this.defaultValue)this.value='';" onBlur="if(this.value=='')this.value=this.defaultValue;">Your Message *</textarea>
  <input class="input-btn" type="submit" value="send message">
  </form>

我托管了网页,但表单无效。我还需要做些什么来使其发挥作用。

这是php代码: -

<html>
<body>
<?php 
$errors = '';
$myemail = 'xyz@abc.com.com';
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 'indes' page
header('Location: index.html');
}
?>

我已经完成了php部分并将其上传到网站,但它仍然没有从联系表单发送任何邮件。即根本没有回应。我现在可以得到任何帮助吗?

1 个答案:

答案 0 :(得分:1)

您需要有一种在发送时收集提交的方法。阅读本网站,将为您提供更多信息。

基本上你需要使用类似PHP的东西来收集信息并对其进行处理,将其存储到数据库中或通过电子邮件发送给自己。

HTML & PHP Contact Form

mail.php

<?php
   $name    = $_POST['name'];
   $email   = $_POST['email'];
   $message = $_POST['message'];

   $to      = 'nobody@example.com';
   $subject = 'the subject';
   $headers = 'From: webmaster@example.com' . "\r\n" .
              'Reply-To: webmaster@example.com' . "\r\n" .
              'X-Mailer: PHP/' . phpversion();

   mail($to, $subject, $message, $headers);
?> 

   // Either insert into database or email to yourself.  You would want to look at sanitizing the inputs a bit and checking for valid email addresses. 

PHP mail()