簡體   English   中英

無法設置基本的php聯系表單

[英]Unable Setting up a basic php 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>

更新----

嗨,大家好,謝謝您的回答...刪除了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");
    }

UPDATE

由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>

您的新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();
  }

嘗試這個

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM