如何进行联系表格验证?

时间:2013-12-14 10:29:22

标签: php html contact-form

我不知道PHP,但是在谷歌叔叔的帮助下,你亲爱的Stack Overflow用户,我已经在我的网站上用联系表格管理过了。有一件事我不知道怎么做:

如果某些表单为空或验证失败,如何将我的php更改为不发送表单(并告知用户更正某些内容)。

  1. 如果要输入至少一个空字段,请不要发送。
  2. 如果电子邮件地址格式错误,请勿发送。
  3. 如果用户未标记“条款”复选框
  4. ,请勿发送

    我的PHP:

        <?php
    error_reporting(-1);
    ini_set('display_errors', 'On');
    if(isset($_POST['email'])) {
    
    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to = "biuro@natropiegra.pl";
    $email_subject = "POZIOM 1";
    $email_from = "rezerwacja@natropiegra.pl";
    
    $email_message = "Form details below.\n\n";
    
    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }
    
    $email_message .= "Imię i nazwisko: ".clean_string($_POST["imie"])."\n";
    $email_message .= "Email: ".clean_string($_POST["email"])."\n";
    $email_message .= "Telefon: ".clean_string($_POST["telefon"])."\n";
    $email_message .= "Płatność: ".implode(" ", $_POST['platnosc'])."\n";
    $email_message .= "Miesiąc: ".clean_string($_POST["miesiac"])."\n";
    $email_message .= "Dzień: ".clean_string($_POST["dzien"])."\n";
    
    
    // create email headers
    $headers = 'From: '.$email_from."\r\n".
    'Reply-To: '.$email_from."\r\n" .
    'X-Mailer: PHP/' . phpversion();
    mail($email_to, $email_subject, $email_message, $headers); 
    ?>
    
    <!-- include your own success html here -->
    <center>
    <img src="image/natropie2.png" />
      <br>
      <br>
      Thank you for contacting us.  We will be in touch.<br>
    <br>
     <a href="index.html">HOME</a></center>
    
    <?php
    }
    ?>
    

    我的HTML:

        <div id="form" class="java">
    </div>
    
    <div id="x" class="java">
    </div>
    
    
    <div id="formphp" class="java">
    
    <div id="formtytul">
    <span class="txt3">
    POZIOM 1 <span class="txt1">Cena -<span class="txt3"> 79zł
    </span>
    </div>
    
    <form name="htmlform" method="post" action="email.php">
    <table width="561">
    <tr>
     <td width="212" align="right" valign="top">
      <label for="imie"> </label>
     </td>
     <td width="337" valign="top">
        <input type="text" input size="20" name="imie" placeholder="Imię i Nazwisko">
     </td>
    </tr>
    <tr>
     <td valign="top" align="right">
      <label for="nazwisko"></label>
     </td>
     <td valign="top">
      <input type="text" input size="20" name="telefon" placeholder="Telefon">
     </td>
    </tr>
    <tr>
     <td valign="top" align="right">
      <label for="email"></label>
     </td>
     <td valign="top">
      <input type="text" input size="20" name="email" placeholder="E-mail">
     </td>
     </tr>
    
    
    <div id="formmiesiac">
    
    <select name="miesiac">
        <option selected="selected">Grudzień</option>
        <option>Styczeń</option>
            <option>Luty</option>
            <option>Marzec</option>
    </select>
    </div>
    
    <div id="formdzien">
    
    <select name="dzien"> 
        <option selected="selected">1</option>
        <option>2</option>
            <option>3</option>
            <option>4</option>
            <option>5</option>
            <option>6</option>
            <option>7</option>
            <option>8</option>
            <option>9</option>
            <option>10</option>
            <option>11</option>
            <option>12</option>
            <option>13</option>
            <option>14</option>
            <option>15</option>
            <option>16</option>
            <option>17</option>
            <option>18</option>
            <option>19</option>
            <option>20</option>
            <option>21</option>
            <option>22</option>
            <option>23</option>
            <option>24</option>
            <option>25</option>
            <option>26</option>
            <option>27</option>
            <option>28</option>
            <option>29</option>
            <option>30</option>
            <option>31</option>
    </select>
    </div>
    
    <div id="formgodz">
    
    <select name="godz"> 
        <option selected="selected">9:00</option>
        <option>10:00</option>
            <option>11:00</option>
            <option>12:00</option>
            <option>13:00</option>
            <option>14:00</option>
            <option>15:00</option>
            <option>16:00</option>
            <option>17:00</option>
            <option>18:00</option>
            <option>19:00</option>
            <option>20:00</option>
            <option>21:00</option>
    </select>
    </div>
    
    <div id="formplatnosc">
    <span class="txt1">Rodzaj płatności:</span><br>
    <span class="txt9">
    <input type="checkbox" name="platnosc[]" value="gotowka" />&nbsp;Gotówka
    
    <input type="checkbox" name="platnosc[]" value="voucher" />&nbsp;Voucher
    </span>
    </div>
    
    <div id="formregulamin">
    <span class="txt9">
    <input type="checkbox" name="regulamin[]" value="tak" />&nbsp;Akcpetuję
    <a href="http://www.natropiegra.pl/regulamin.html" target="_blank"> regulamin</a></span>
    </div>
    
    
    <div id="formwyslij">
    <span class="txt7">
    <input type="submit" value="Rezerwuj">
    </span>
    </div>
    
    
    </table>
    </form>
    
    </div>
    

1 个答案:

答案 0 :(得分:0)

使用条件和isset来检查表单字段是否存在。

   <?php
error_reporting(-1);
ini_set('display_errors', 'On');

// Check if all the fields are entered
if(isset($_POST['imie']) && isset($_POST['telefon']) && isset($_POST['email']) && isset($_POST['platnosc'])){

// Check if rules are agreed.
if(!isset($_POST['regulamin'])){
echo "Please accept our terms of services.";
die();
}

//Check if the email is valid
if(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)){
//Kill the page if it's invalid
echo "Please enter a proper email address.";
die();
}

// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "biuro@natropiegra.pl";
$email_subject = "POZIOM 1";
$email_from = "rezerwacja@natropiegra.pl";

$email_message = "Form details below.\n\n";

function clean_string($string) {
  $bad = array("content-type","bcc:","to:","cc:","href");
  return str_replace($bad,"",$string);
}

$email_message .= "Imię i nazwisko: ".clean_string($_POST["imie"])."\n";
$email_message .= "Email: ".clean_string($_POST["email"])."\n";
$email_message .= "Telefon: ".clean_string($_POST["telefon"])."\n";
$email_message .= "Płatność: ".implode(" ", $_POST['platnosc'])."\n";
$email_message .= "Miesiąc: ".clean_string($_POST["miesiac"])."\n";
$email_message .= "Dzień: ".clean_string($_POST["dzien"])."\n";


// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($email_to, $email_subject, $email_message, $headers); 
?>

<!-- include your own success html here -->
<center>
<img src="image/natropie2.png" />
  <br>
  <br>
  Thank you for contacting us.  We will be in touch.<br>
<br>
 <a href="index.html">HOME</a></center>

<?php
}else{
echo "Please fill all the fields properly.";
die();
}
?>