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

时间:2014-12-01 11:27:20

标签: php html contact-form

HI感谢任何有关此问题的答案。即使在反复检查变量之后,我也无法让我的php表单工作。

这是表格标签的开头 -

<form name="sentMessage" action="contact.php" method="post" id="contactForm">

我的PhP如下 -

<?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
" ;

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)
{
?>

[HTML BODY HERE}

<?php
exit();
}
?>

请大家,任何帮助都很有用。这已经上传,即使在现场直播也无法正常工作......

这是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" 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" 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" 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" 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" class="btn btn-success btn-lg">Send</button>
                            </div>
                        </div>
                    </form>

更新----

HI GUYS,感谢您的回答...删除了PHP中的重新验证,它似乎重新指向感谢页面... 但我收到的电子邮件是空白的(没有$ name,$ phone等...)

新的PHP代码 -

$name = check_input($_REQUEST['name']);
$email = check_input($_REQUEST['email']);
$phone = check_input($_REQUEST['phone']);
$refemail = check_input($_REQUEST['refemail']);

我删除了以下代码。

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

更新

linil回答

2 个答案:

答案 0 :(得分:1)

您的代码似乎没问题。

您应首先尝试在条件中包装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>

您的新PHP

<?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();
  }
?>

希望有所帮助。

更新:

尝试用此替换$header

$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();
  }

答案 1 :(得分:0)

试试这个

<?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
}