How to do contact form validation?

Mac

I have no knowledge of PHP but somehow with the help of Uncle Google and you dear Stack Overflow users I managed already with contact form on my website. One thing I have no idea how to do still:

How to change my php to not send a form (and let know the user to correct something) if some of the form will be empty or fails validation.

  1. Don't send if there's at least one empty field to input.
  2. Don't send if e-mail address has a wrong form.
  3. Don't send if user didn't mark the 'terms' checkbox

My PHP:

    <?php
error_reporting(-1);
ini_set('display_errors', 'On');
if(isset($_POST['email'])) {

// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "[email protected]";
$email_subject = "POZIOM 1";
$email_from = "[email protected]";

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

My 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>
Mohammad Tomaraei

Use conditions and isset to check for the presence of form fields.

   <?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 = "[email protected]";
$email_subject = "POZIOM 1";
$email_from = "[email protected]";

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related