无法设置基本的php联系人表单

无法设置基本的php联系人表单,php,html,contact-form,Php,Html,Contact Form,您好,非常感谢您的回答。我无法让我的php表单工作,即使在反复检查变量之后 这是表单标签的开头- <form name="sentMessage" action="contact.php" method="post" id="contactForm"> 我已经删除了下面的代码 if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email)) { show_error("E-mail address not valid

您好,非常感谢您的回答。我无法让我的php表单工作,即使在反复检查变量之后

这是表单标签的开头-

<form name="sentMessage" action="contact.php" method="post" id="contactForm">
我已经删除了下面的代码

if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
    {
    show_error("E-mail address not valid");
    }
更新

linil回答说你的代码似乎很好

您应该首先尝试将
mail()
和以下
exit()
语句包装在一个条件中。现在,您正试图在加载HTML以便客户端填写联系信息之前发送邮件并退出

见下文:

你的新HTML

<form name="sentMessage" action="contact.php" method="post" id="contactForm">
  <div class="row control-group">
    <div>
      <label style="margin-right:85px; font-size:16px;">Name</label>
      <input type="text" class="form-control" placeholder="Name" id="name" name="name" required data-validation-required-message="Please enter your name.">
      <p class="help-block text-danger"></p>
    </div>
  </div>
  <div class="row control-group">
    <div>
      <label style="margin-right:20px; font-size:16px;">Email Address</label>
      <input type="email" class="form-control" placeholder="Email Address" id="email" name="email" required data-validation-required-message="Please enter your email address.">
      <p class="help-block text-danger"></p>
    </div>
  </div>
  <div class="row control-group">
    <div>
      <label style="margin-right:15px; font-size:16px;">Phone Number</label>
      <input type="tel" class="form-control" placeholder="Phone Number (+974)" id="phone" name="phone" required data-validation-required-message="Please enter your phone number.">
      <p class="help-block text-danger"></p>
    </div>
  </div>
  <br>
  <p>If you would like to refer someone else or if you have multiple e-mail IDs, enter them below</p>
  <div class="row control-group">
    <div>
      <label style="margin-right:40px; font-size:16px;">References</label>
      <input type="tel" class="form-control" placeholder="Email 1; Email 2; Email 3" id="refemail" name="refemail" required data-validation-required-message="Please the E-mail IDs.">
      <p class="help-block text-danger"></p>
    </div>
  </div>
  <br>
  <div id="success"></div>
  <div class="row" style="display:inline-block; text-align:center;">
    <div class="form-group col-xs-12">
      <button type="submit" name="submit" class="btn btn-success btn-lg">Send</button>
    </div>
  </div>
</form>
此外,请替换以下内容,以便我们了解您收到空白邮件的原因:

if ($name && $email)
  { //add a condition that validates the email for sending
    print_r($message); exit; 
    /* 
         if Name, Email, Contact and References are not set. 
         It mean their name value are not still set in your HTML code
         Note: while print_r is in effect. this page won't redirect. 
         we are debugging. 
         Remove the print_r line after you start getting expected values
         and redirect will work fine.
    */
    mail($myemail, $subject, $message, $headers);
    header('Location: thankyou.html');
    exit();
  }
试试这个

<?php
$myemail  = 'nikhilnayak.in@gmail.com'; 
if(isset($_POST['name'] && $_POST['email'] && $_POST['phone'] && $_POST['refemail']){
      $name = check_input($_POST['name'], "Enter your name");
      $email = check_input($_POST['email'], "Enter your E-mail ID");
      $phone = check_input($_POST['phone'], "Enter your Contact #");
      $refemail = check_input($_POST['refemail']);

    if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
    {
    show_error("E-mail address not valid");
    }
    $subject = 'New Website Lead' ;

    $headers = "From: $name";

    $message = "
    Name: $name
    E-mail: $email
    Contact #: $phone

    References:
    $refemail
    " ;

    mail($myemail, $subject, $message, $headers);

    header('Location: thankyou.html');
    exit();
}


function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}

function show_error($myError)
{    
 //try to echo the html here instead
} 

在执行以下任何退出功能之前退出粘贴表单html以及您面临的错误?在不显示任何错误的情况下,它加载URL contact.php并显示其中的html,即contact.html页面而不是Thankyu.html页面。这就是为什么我知道它不起作用。不,不起作用。。。同样的结果。。。在浏览器中加载contact.php不感谢page@masmrdrr你能告诉我,邮件已经寄出了吗?您没有在输入字段中添加名称标记。请参阅新编辑修复了代码(在我的问题中编辑),页面重定向到thankyou.html,但收到空白电子邮件(请参阅更新的问题-最后一部分)@masmrdrr将此
打印($message);退出
在您的
邮件()之前(
并让我知道您看到了什么它会在浏览器窗口上打印姓名:电子邮件:联系人#:参考:无电子邮件。不会重定向到thankyou.html
<form name="sentMessage" action="contact.php" method="post" id="contactForm">
  <div class="row control-group">
    <div>
      <label style="margin-right:85px; font-size:16px;">Name</label>
      <input type="text" class="form-control" placeholder="Name" id="name" name="name" required data-validation-required-message="Please enter your name.">
      <p class="help-block text-danger"></p>
    </div>
  </div>
  <div class="row control-group">
    <div>
      <label style="margin-right:20px; font-size:16px;">Email Address</label>
      <input type="email" class="form-control" placeholder="Email Address" id="email" name="email" required data-validation-required-message="Please enter your email address.">
      <p class="help-block text-danger"></p>
    </div>
  </div>
  <div class="row control-group">
    <div>
      <label style="margin-right:15px; font-size:16px;">Phone Number</label>
      <input type="tel" class="form-control" placeholder="Phone Number (+974)" id="phone" name="phone" required data-validation-required-message="Please enter your phone number.">
      <p class="help-block text-danger"></p>
    </div>
  </div>
  <br>
  <p>If you would like to refer someone else or if you have multiple e-mail IDs, enter them below</p>
  <div class="row control-group">
    <div>
      <label style="margin-right:40px; font-size:16px;">References</label>
      <input type="tel" class="form-control" placeholder="Email 1; Email 2; Email 3" id="refemail" name="refemail" required data-validation-required-message="Please the E-mail IDs.">
      <p class="help-block text-danger"></p>
    </div>
  </div>
  <br>
  <div id="success"></div>
  <div class="row" style="display:inline-block; text-align:center;">
    <div class="form-group col-xs-12">
      <button type="submit" name="submit" class="btn btn-success btn-lg">Send</button>
    </div>
  </div>
</form>
<?php
  $myemail = 'nikhilnayak.in@gmail.com';

  $name = check_input($_POST['name'], "Enter your name");
  $email = check_input($_POST['email'], "Enter your E-mail ID");
  $phone = check_input($_POST['phone'], "Enter your Contact #");
  $refemail = check_input($_POST['refemail']);

  if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
  {
    show_error("E-mail address not valid");
  }
  $subject = 'New Website Lead';

  $headers = "From: $name";

  $message = "
Name: $name
E-mail: $email
Contact #: $phone

References:
$refemail
";
  if ($name && $email)
  { //add a condition that validates the email for sending

    mail($myemail, $subject, $message, $headers);
    header('Location: thankyou.html');
    exit();
  }

  function check_input($data, $problem = '')
  {
    echo $data;
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    if ($problem && strlen($data) == 0)
    {
      show_error($problem);
    }
    return $data;
  }

  function show_error($myError)
  {
    ?>

    [HTML BODY HERE}

    <?php
    exit();
  }
?>
$headers = "From: $name <nobody@".$_SERVER['SERVER_NAME'].">";
if ($name && $email)
  { //add a condition that validates the email for sending
    print_r($message); exit; 
    /* 
         if Name, Email, Contact and References are not set. 
         It mean their name value are not still set in your HTML code
         Note: while print_r is in effect. this page won't redirect. 
         we are debugging. 
         Remove the print_r line after you start getting expected values
         and redirect will work fine.
    */
    mail($myemail, $subject, $message, $headers);
    header('Location: thankyou.html');
    exit();
  }
<?php
$myemail  = 'nikhilnayak.in@gmail.com'; 
if(isset($_POST['name'] && $_POST['email'] && $_POST['phone'] && $_POST['refemail']){
      $name = check_input($_POST['name'], "Enter your name");
      $email = check_input($_POST['email'], "Enter your E-mail ID");
      $phone = check_input($_POST['phone'], "Enter your Contact #");
      $refemail = check_input($_POST['refemail']);

    if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
    {
    show_error("E-mail address not valid");
    }
    $subject = 'New Website Lead' ;

    $headers = "From: $name";

    $message = "
    Name: $name
    E-mail: $email
    Contact #: $phone

    References:
    $refemail
    " ;

    mail($myemail, $subject, $message, $headers);

    header('Location: thankyou.html');
    exit();
}


function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}

function show_error($myError)
{    
 //try to echo the html here instead
}