doubi7306 2014-12-01 11:27
浏览 18
已采纳

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

HI Appreciate any answers regarding this. I'm unable to get my php form to work even after double checking the variable over and again.

This is the form tag beginning -

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

My PhP below -

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

Please guys, any help is useful. This has been uploaded and its not working even when live...

This is the HTML form -

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

UPDATE ----

HI GUYS, thanks for your answers... removed the re-validation in the PHP and it seems to re-direct to the thank you page... but the email i get is blank (without the $name, $phone, etc...)

new PHP code -

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

and i have removed the below code.

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

UPDATE

Answered by linil

  • 写回答

2条回答 默认 最新

  • doumi1944 2014-12-01 11:47
    关注

    Your code seems fine.

    You should first try wrapping the mail() and the following exit() statements in a condition. As it is now, you are attempting to mail and exiting before the HTML get loaded for the client to fill in contact information.

    See below:

    Your New 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>
    

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

    Hope that helps.

    UPDATE:

    try replacing your $header with this:

    $headers = "From: $name <nobody@".$_SERVER['SERVER_NAME'].">";
    

    Also, replace the following so we can see why you may be getting blank mails:

    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条)

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line