发送带有空字段的 PHP 表单时出现错误消息

发布于 2024-12-20 12:56:16 字数 19452 浏览 0 评论 0原文

我已经尝试了很多方法,但是我无法获得干净的 HTML 或 PHP 代码来阻止用户提交带有空字段的表单(我不知道是否可以做到)。我不认为我可以执行 JavaScript,因为 HTML 文件的主机会自动删除 HEAD... 截至目前,PHP 操作将包含表单结果的电子邮件发送到普通的 @ 地址。 这是表单,后面是 PHP 操作(对于我刚开始编写 PHP 代码时出现的错误,我深表歉意):


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Untitled Form</title>
<link rel="stylesheet" type="text/css" href="http://www.puzzles-et-jeux.com/themes/puzzles-et-jeux/css/view.css" media="all">
<script type="text/javascript" src="view.js"></script>

</head>
<body id="main_body" >

        <div id="form_container">

        <h1><a>Page contact</a></h1>
        <form action="http://www.lesparisettes.com/emailphp/email.php" method="post">
                    <div class="form_description">
            <p><font color="#cc398b"><strong>Besoin d'une information?<br />
      Vous souhaitez recevoir l'un des catalogues de notre collection?<br />
      Le formulaire ci-dessous est a votre disposition pour nous faire part de vos demandes.<br />
      <u>Merci de bien remplir tous les champs de ce formulaire.</u>
        </div>                      
            <ul >

                    <li id="li_7" >
        <label class="description" for="sujet">Sujet de votre message </label>
        <div>
        <select class="element select medium" id="sujet" name="sujet"> 
            <option value="" selected="selected"></option>
<option value="Catalogue Puzzle Adultes" >Catalogue Puzzles Adultes</option>
<option value="Catalogue Puzzle Enfants" >Catalogue Puzzles Enfants</option>
<option value="Photopuzzle" >Photopuzzle - votre puzzle personnalisé</option>
<option value="Professionnel" >Professionnel</option>
<option value="Autre demande" >Autre demande</option>

        </select>
        </div> 
        </li>       <li id="li_1" >
        <label class="description" for="email">Adresse e-mail </label>
        <div>
            <input id="email" name="email" class="element text medium" type="text" maxlength="255" value=""/> 
        </div> 
        </li>       <li id="li_2" >
        <label class="description" for="element_2">Nom et prénom </label>
        <span>
            <input id="prenom" name= "prenom" class="element text" maxlength="255" size="8" value=""/>
            <label>Prenom</label>
        </span>
        <span>
            <input id="nom" name= "nom" class="element text" maxlength="255" size="14" value=""/>
            <label>Nom</label>
        </span> 
        </li>       <li id="li_3" >
        <label class="description" for="numeroclient">Numéro de client </label>
        <div>
            <input id="numeroclient" name="numeroclient" class="element text medium" type="text" maxlength="255" value=""/> 
        </div> 
        </li>       <li id="li_4" >
        <label class="description" for="element_4">Adresse complète </label>

        <div>
            <input id="rue" name="rue" class="element text large" value="" type="text">
            <label for="rue">Rue / Avenue / Voie</label>
        </div>

        <div>
            <input id="adresse2" name="adresse2" class="element text large" value="" type="text">
            <label for="adresse2">Adresse ligne 2</label>
        </div>

        <div class="left">
            <input id="ville" name="ville" class="element text medium" value="" type="text">
            <label for="ville">Ville</label>
        </div>

        <div class="left">
            <input id="region" name="region" class="element text medium" value="" type="text">
            <label for="region">Province / Region</label>
        </div>

        <div class="left">
            <input id="codepostal" name="codepostal" class="element text medium" maxlength="15" value="" type="text">
            <label for="codepostal">Code postal</label>
        </div>

        <div class="left">
            <select class="element select medium" id="pays" name="pays"> 
            <option value="" selected="selected"></option>
<option value="Afghanistan" >Afghanistan</option>
<option value="Albania" >Albania</option>
<option value="Algeria" >Algeria</option>
<option value="Andorra" >Andorra</option>
<option value="Antigua and Barbuda" >Antigua and Barbuda</option>
<option value="Argentina" >Argentina</option>
<option value="Armenia" >Armenia</option>
<option value="Australia" >Australia</option>
<option value="Austria" >Austria</option>
<option value="Azerbaijan" >Azerbaijan</option>
<option value="Bahamas" >Bahamas</option>
<option value="Bahrain" >Bahrain</option>
<option value="Bangladesh" >Bangladesh</option>
<option value="Barbados" >Barbados</option>
<option value="Belarus" >Belarus</option>
<option value="Belgium" >Belgium</option>
<option value="Belize" >Belize</option>
<option value="Benin" >Benin</option>
<option value="Bhutan" >Bhutan</option>
<option value="Bolivia" >Bolivia</option>
<option value="Bosnia and Herzegovina" >Bosnia and Herzegovina</option>
<option value="Botswana" >Botswana</option>
<option value="Brazil" >Brazil</option>
<option value="Brunei" >Brunei</option>
<option value="Bulgaria" >Bulgaria</option>
<option value="Burkina Faso" >Burkina Faso</option>
<option value="Burundi" >Burundi</option>
<option value="Cambodia" >Cambodia</option>
<option value="Cameroon" >Cameroon</option>
<option value="Canada" >Canada</option>
<option value="Cape Verde" >Cape Verde</option>
<option value="Central African Republic" >Central African Republic</option>
<option value="Chad" >Chad</option>
<option value="Chile" >Chile</option>
<option value="China" >China</option>
<option value="Colombia" >Colombia</option>
<option value="Comoros" >Comoros</option>
<option value="Congo" >Congo</option>
<option value="Costa Rica" >Costa Rica</option>
<option value="Côte d'Ivoire" >Côte d'Ivoire</option>
<option value="Croatia" >Croatia</option>
<option value="Cuba" >Cuba</option>
<option value="Cyprus" >Cyprus</option>
<option value="Czech Republic" >Czech Republic</option>
<option value="Denmark" >Denmark</option>
<option value="Djibouti" >Djibouti</option>
<option value="Dominica" >Dominica</option>
<option value="Dominican Republic" >Dominican Republic</option>
<option value="East Timor" >East Timor</option>
<option value="Ecuador" >Ecuador</option>
<option value="Egypt" >Egypt</option>
<option value="El Salvador" >El Salvador</option>
<option value="Equatorial Guinea" >Equatorial Guinea</option>
<option value="Eritrea" >Eritrea</option>
<option value="Estonia" >Estonia</option>
<option value="Ethiopia" >Ethiopia</option>
<option value="Fiji" >Fiji</option>
<option value="Finland" >Finland</option>
<option value="France" >France</option>
<option value="Gabon" >Gabon</option>
<option value="Gambia" >Gambia</option>
<option value="Georgia" >Georgia</option>
<option value="Germany" >Germany</option>
<option value="Ghana" >Ghana</option>
<option value="Greece" >Greece</option>
<option value="Grenada" >Grenada</option>
<option value="Guatemala" >Guatemala</option>
<option value="Guinea" >Guinea</option>
<option value="Guinea-Bissau" >Guinea-Bissau</option>
<option value="Guyana" >Guyana</option>
<option value="Haiti" >Haiti</option>
<option value="Honduras" >Honduras</option>
<option value="Hong Kong" >Hong Kong</option>
<option value="Hungary" >Hungary</option>
<option value="Iceland" >Iceland</option>
<option value="India" >India</option>
<option value="Indonesia" >Indonesia</option>
<option value="Iran" >Iran</option>
<option value="Iraq" >Iraq</option>
<option value="Ireland" >Ireland</option>
<option value="Israel" >Israel</option>
<option value="Italy" >Italy</option>
<option value="Jamaica" >Jamaica</option>
<option value="Japan" >Japan</option>
<option value="Jordan" >Jordan</option>
<option value="Kazakhstan" >Kazakhstan</option>
<option value="Kenya" >Kenya</option>
<option value="Kiribati" >Kiribati</option>
<option value="North Korea" >North Korea</option>
<option value="South Korea" >South Korea</option>
<option value="Kuwait" >Kuwait</option>
<option value="Kyrgyzstan" >Kyrgyzstan</option>
<option value="Laos" >Laos</option>
<option value="Latvia" >Latvia</option>
<option value="Lebanon" >Lebanon</option>
<option value="Lesotho" >Lesotho</option>
<option value="Liberia" >Liberia</option>
<option value="Libya" >Libya</option>
<option value="Liechtenstein" >Liechtenstein</option>
<option value="Lithuania" >Lithuania</option>
<option value="Luxembourg" >Luxembourg</option>
<option value="Macedonia" >Macedonia</option>
<option value="Madagascar" >Madagascar</option>
<option value="Malawi" >Malawi</option>
<option value="Malaysia" >Malaysia</option>
<option value="Maldives" >Maldives</option>
<option value="Mali" >Mali</option>
<option value="Malta" >Malta</option>
<option value="Marshall Islands" >Marshall Islands</option>
<option value="Mauritania" >Mauritania</option>
<option value="Mauritius" >Mauritius</option>
<option value="Mexico" >Mexico</option>
<option value="Micronesia" >Micronesia</option>
<option value="Moldova" >Moldova</option>
<option value="Monaco" >Monaco</option>
<option value="Mongolia" >Mongolia</option>
<option value="Montenegro" >Montenegro</option>
<option value="Morocco" >Morocco</option>
<option value="Mozambique" >Mozambique</option>
<option value="Myanmar" >Myanmar</option>
<option value="Namibia" >Namibia</option>
<option value="Nauru" >Nauru</option>
<option value="Nepal" >Nepal</option>
<option value="Netherlands" >Netherlands</option>
<option value="New Zealand" >New Zealand</option>
<option value="Nicaragua" >Nicaragua</option>
<option value="Niger" >Niger</option>
<option value="Nigeria" >Nigeria</option>
<option value="Norway" >Norway</option>
<option value="Oman" >Oman</option>
<option value="Pakistan" >Pakistan</option>
<option value="Palau" >Palau</option>
<option value="Panama" >Panama</option>
<option value="Papua New Guinea" >Papua New Guinea</option>
<option value="Paraguay" >Paraguay</option>
<option value="Peru" >Peru</option>
<option value="Philippines" >Philippines</option>
<option value="Poland" >Poland</option>
<option value="Portugal" >Portugal</option>
<option value="Puerto Rico" >Puerto Rico</option>
<option value="Qatar" >Qatar</option>
<option value="Romania" >Romania</option>
<option value="Russia" >Russia</option>
<option value="Rwanda" >Rwanda</option>
<option value="Saint Kitts and Nevis" >Saint Kitts and Nevis</option>
<option value="Saint Lucia" >Saint Lucia</option>
<option value="Saint Vincent and the Grenadines" >Saint Vincent and the Grenadines</option>
<option value="Samoa" >Samoa</option>
<option value="San Marino" >San Marino</option>
<option value="Sao Tome and Principe" >Sao Tome and Principe</option>
<option value="Saudi Arabia" >Saudi Arabia</option>
<option value="Senegal" >Senegal</option>
<option value="Serbia and Montenegro" >Serbia and Montenegro</option>
<option value="Seychelles" >Seychelles</option>
<option value="Sierra Leone" >Sierra Leone</option>
<option value="Singapore" >Singapore</option>
<option value="Slovakia" >Slovakia</option>
<option value="Slovenia" >Slovenia</option>
<option value="Solomon Islands" >Solomon Islands</option>
<option value="Somalia" >Somalia</option>
<option value="South Africa" >South Africa</option>
<option value="Spain" >Spain</option>
<option value="Sri Lanka" >Sri Lanka</option>
<option value="Sudan" >Sudan</option>
<option value="Suriname" >Suriname</option>
<option value="Swaziland" >Swaziland</option>
<option value="Sweden" >Sweden</option>
<option value="Switzerland" >Switzerland</option>
<option value="Syria" >Syria</option>
<option value="Taiwan" >Taiwan</option>
<option value="Tajikistan" >Tajikistan</option>
<option value="Tanzania" >Tanzania</option>
<option value="Thailand" >Thailand</option>
<option value="Togo" >Togo</option>
<option value="Tonga" >Tonga</option>
<option value="Trinidad and Tobago" >Trinidad and Tobago</option>
<option value="Tunisia" >Tunisia</option>
<option value="Turkey" >Turkey</option>
<option value="Turkmenistan" >Turkmenistan</option>
<option value="Tuvalu" >Tuvalu</option>
<option value="Uganda" >Uganda</option>
<option value="Ukraine" >Ukraine</option>
<option value="United Arab Emirates" >United Arab Emirates</option>
<option value="United Kingdom" >United Kingdom</option>
<option value="United States" >United States</option>
<option value="Uruguay" >Uruguay</option>
<option value="Uzbekistan" >Uzbekistan</option>
<option value="Vanuatu" >Vanuatu</option>
<option value="Vatican City" >Vatican City</option>
<option value="Venezuela" >Venezuela</option>
<option value="Vietnam" >Vietnam</option>
<option value="Yemen" >Yemen</option>
<option value="Zambia" >Zambia</option>
<option value="Zimbabwe" >Zimbabwe</option>

            </select>
        <label for="pays">Pays</label>
    </div> 
        </li>       <li id="li_5" >
        <label class="description" for="telephone">Téléphone </label>
        <div>
            <input id="telephone" name="telephone" class="element text medium" type="text" maxlength="255" value=""/> 
        </div> 
        </li>       <li id="li_6" >
        <label class="description" for="message">Votre message </label>
        <div>
            <textarea id="message" name="message" class="element textarea medium"></textarea> 
        </div> 
        </li>

                    <li class="buttons">
                <input type="hidden" name="form_id" value="306225" />

                <input id="saveForm" class="button_text" type="submit" name="submit" value="Envoyer" />
        </li>
            </ul>
        </form> 
        <div id="footer">
            Generated by <a href="http://www.phpform.org">pForm</a>
        </div>
    </div>
    </body>
</html>

    <?php
$required = array('sujet', 'email', 'prenom', 'nom', 'numeroclient', 'rue', 'adresse2', 'ville', 'region', 'codepostal', 'pays', 'telephone', 'message');

$error = false;
foreach($required as $field) {
  if (empty($_POST[$field])) {
    $error = true;
     }
}

    $sujet = $_POST['sujet'];
    $email = $_POST['email'];
    $prenom = $_POST['prenom'];
    $nom = $_POST['nom'];
    $numeroclient = $_POST['numeroclient'];
    $rue = $_POST['rue'];
    $adresse2 = $_POST['adresse2'];
    $ville = $_POST['ville'];
    $region = $_POST['region'];
    $codepostal = $_POST['codepostal'];
    $pays = $_POST['pays'];
    $telephone = $_POST['telephone'];
    $message = $_POST['message'];

    $msg = "sujet: \t $sujet \n";
    $msg .= "email: \t $email \n";
    $msg .= "prenom:\t $prenom \n";
    $msg .= "nom:\t $nom \n";
    $msg .= "numeroclient:\t $numeroclient \n";
    $msg .= "rue:\t $rue \n";
    $msg .= "adresse2:\t $adresse2 \n";
    $msg .= "ville:\t $ville \n";
    $msg .= "region:\t $region \n";
    $msg .= "codepostal:\t $codepostal \n";
    $msg .= "pays:\t $pays \n";
    $msg .= "telephone:\t $telephone \n";
    $msg .= "message:\t $message \n";

    $recipient = "[email protected]";
    $subject = "$sujet \t $prenom \t $nom";

    $mailheaders = "From: Site internet PMW <[email protected]> \n";
    $mailheaders .= "Reply-To: $email \n\n";

    mail($recipient, $subject, $msg, $mailheaders);

    echo "<HTML><HEAD>";
    echo "<title>Merci</title>";
    echo "</head>";
    echo "<body leftmargin=\"0\" topmargin=\"0\" marginwidth=\"0\" marginheight=\"0\">";
    echo "<table width=\"650\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\">";
    echo "<tr>";
    echo "</tr>";
    echo "<tr>";
    echo "</tr>";
    echo "<tr>";
    echo "</tr>";
    echo "<td align=\"center\" valign=\"top\"></br></br>Merci pour votre message, nous l'avons pris en compte. <a href=\"http://www.puzzles-et-jeux.com/\">Revenir a PMW</a></td>";
    echo "</table>";
    echo "</body>";
    echo "</html>";
    ?>

I have tried many things, but I cannot get a clean HTML or PHP code to prevent a user from submitting a form with empty fields (I don't know if it can be done). I don't think I can do a javascript, because the host of the HTML files strips the HEAD automatically... As of now, the PHP action sends an email with the form results to a normal @ address.
Here is the form , followed by the PHP action (sorry for the bugs I am new to coding PHP):


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Untitled Form</title>
<link rel="stylesheet" type="text/css" href="http://www.puzzles-et-jeux.com/themes/puzzles-et-jeux/css/view.css" media="all">
<script type="text/javascript" src="view.js"></script>

</head>
<body id="main_body" >

        <div id="form_container">

        <h1><a>Page contact</a></h1>
        <form action="http://www.lesparisettes.com/emailphp/email.php" method="post">
                    <div class="form_description">
            <p><font color="#cc398b"><strong>Besoin d'une information?<br />
      Vous souhaitez recevoir l'un des catalogues de notre collection?<br />
      Le formulaire ci-dessous est a votre disposition pour nous faire part de vos demandes.<br />
      <u>Merci de bien remplir tous les champs de ce formulaire.</u>
        </div>                      
            <ul >

                    <li id="li_7" >
        <label class="description" for="sujet">Sujet de votre message </label>
        <div>
        <select class="element select medium" id="sujet" name="sujet"> 
            <option value="" selected="selected"></option>
<option value="Catalogue Puzzle Adultes" >Catalogue Puzzles Adultes</option>
<option value="Catalogue Puzzle Enfants" >Catalogue Puzzles Enfants</option>
<option value="Photopuzzle" >Photopuzzle - votre puzzle personnalisé</option>
<option value="Professionnel" >Professionnel</option>
<option value="Autre demande" >Autre demande</option>

        </select>
        </div> 
        </li>       <li id="li_1" >
        <label class="description" for="email">Adresse e-mail </label>
        <div>
            <input id="email" name="email" class="element text medium" type="text" maxlength="255" value=""/> 
        </div> 
        </li>       <li id="li_2" >
        <label class="description" for="element_2">Nom et prénom </label>
        <span>
            <input id="prenom" name= "prenom" class="element text" maxlength="255" size="8" value=""/>
            <label>Prenom</label>
        </span>
        <span>
            <input id="nom" name= "nom" class="element text" maxlength="255" size="14" value=""/>
            <label>Nom</label>
        </span> 
        </li>       <li id="li_3" >
        <label class="description" for="numeroclient">Numéro de client </label>
        <div>
            <input id="numeroclient" name="numeroclient" class="element text medium" type="text" maxlength="255" value=""/> 
        </div> 
        </li>       <li id="li_4" >
        <label class="description" for="element_4">Adresse complète </label>

        <div>
            <input id="rue" name="rue" class="element text large" value="" type="text">
            <label for="rue">Rue / Avenue / Voie</label>
        </div>

        <div>
            <input id="adresse2" name="adresse2" class="element text large" value="" type="text">
            <label for="adresse2">Adresse ligne 2</label>
        </div>

        <div class="left">
            <input id="ville" name="ville" class="element text medium" value="" type="text">
            <label for="ville">Ville</label>
        </div>

        <div class="left">
            <input id="region" name="region" class="element text medium" value="" type="text">
            <label for="region">Province / Region</label>
        </div>

        <div class="left">
            <input id="codepostal" name="codepostal" class="element text medium" maxlength="15" value="" type="text">
            <label for="codepostal">Code postal</label>
        </div>

        <div class="left">
            <select class="element select medium" id="pays" name="pays"> 
            <option value="" selected="selected"></option>
<option value="Afghanistan" >Afghanistan</option>
<option value="Albania" >Albania</option>
<option value="Algeria" >Algeria</option>
<option value="Andorra" >Andorra</option>
<option value="Antigua and Barbuda" >Antigua and Barbuda</option>
<option value="Argentina" >Argentina</option>
<option value="Armenia" >Armenia</option>
<option value="Australia" >Australia</option>
<option value="Austria" >Austria</option>
<option value="Azerbaijan" >Azerbaijan</option>
<option value="Bahamas" >Bahamas</option>
<option value="Bahrain" >Bahrain</option>
<option value="Bangladesh" >Bangladesh</option>
<option value="Barbados" >Barbados</option>
<option value="Belarus" >Belarus</option>
<option value="Belgium" >Belgium</option>
<option value="Belize" >Belize</option>
<option value="Benin" >Benin</option>
<option value="Bhutan" >Bhutan</option>
<option value="Bolivia" >Bolivia</option>
<option value="Bosnia and Herzegovina" >Bosnia and Herzegovina</option>
<option value="Botswana" >Botswana</option>
<option value="Brazil" >Brazil</option>
<option value="Brunei" >Brunei</option>
<option value="Bulgaria" >Bulgaria</option>
<option value="Burkina Faso" >Burkina Faso</option>
<option value="Burundi" >Burundi</option>
<option value="Cambodia" >Cambodia</option>
<option value="Cameroon" >Cameroon</option>
<option value="Canada" >Canada</option>
<option value="Cape Verde" >Cape Verde</option>
<option value="Central African Republic" >Central African Republic</option>
<option value="Chad" >Chad</option>
<option value="Chile" >Chile</option>
<option value="China" >China</option>
<option value="Colombia" >Colombia</option>
<option value="Comoros" >Comoros</option>
<option value="Congo" >Congo</option>
<option value="Costa Rica" >Costa Rica</option>
<option value="Côte d'Ivoire" >Côte d'Ivoire</option>
<option value="Croatia" >Croatia</option>
<option value="Cuba" >Cuba</option>
<option value="Cyprus" >Cyprus</option>
<option value="Czech Republic" >Czech Republic</option>
<option value="Denmark" >Denmark</option>
<option value="Djibouti" >Djibouti</option>
<option value="Dominica" >Dominica</option>
<option value="Dominican Republic" >Dominican Republic</option>
<option value="East Timor" >East Timor</option>
<option value="Ecuador" >Ecuador</option>
<option value="Egypt" >Egypt</option>
<option value="El Salvador" >El Salvador</option>
<option value="Equatorial Guinea" >Equatorial Guinea</option>
<option value="Eritrea" >Eritrea</option>
<option value="Estonia" >Estonia</option>
<option value="Ethiopia" >Ethiopia</option>
<option value="Fiji" >Fiji</option>
<option value="Finland" >Finland</option>
<option value="France" >France</option>
<option value="Gabon" >Gabon</option>
<option value="Gambia" >Gambia</option>
<option value="Georgia" >Georgia</option>
<option value="Germany" >Germany</option>
<option value="Ghana" >Ghana</option>
<option value="Greece" >Greece</option>
<option value="Grenada" >Grenada</option>
<option value="Guatemala" >Guatemala</option>
<option value="Guinea" >Guinea</option>
<option value="Guinea-Bissau" >Guinea-Bissau</option>
<option value="Guyana" >Guyana</option>
<option value="Haiti" >Haiti</option>
<option value="Honduras" >Honduras</option>
<option value="Hong Kong" >Hong Kong</option>
<option value="Hungary" >Hungary</option>
<option value="Iceland" >Iceland</option>
<option value="India" >India</option>
<option value="Indonesia" >Indonesia</option>
<option value="Iran" >Iran</option>
<option value="Iraq" >Iraq</option>
<option value="Ireland" >Ireland</option>
<option value="Israel" >Israel</option>
<option value="Italy" >Italy</option>
<option value="Jamaica" >Jamaica</option>
<option value="Japan" >Japan</option>
<option value="Jordan" >Jordan</option>
<option value="Kazakhstan" >Kazakhstan</option>
<option value="Kenya" >Kenya</option>
<option value="Kiribati" >Kiribati</option>
<option value="North Korea" >North Korea</option>
<option value="South Korea" >South Korea</option>
<option value="Kuwait" >Kuwait</option>
<option value="Kyrgyzstan" >Kyrgyzstan</option>
<option value="Laos" >Laos</option>
<option value="Latvia" >Latvia</option>
<option value="Lebanon" >Lebanon</option>
<option value="Lesotho" >Lesotho</option>
<option value="Liberia" >Liberia</option>
<option value="Libya" >Libya</option>
<option value="Liechtenstein" >Liechtenstein</option>
<option value="Lithuania" >Lithuania</option>
<option value="Luxembourg" >Luxembourg</option>
<option value="Macedonia" >Macedonia</option>
<option value="Madagascar" >Madagascar</option>
<option value="Malawi" >Malawi</option>
<option value="Malaysia" >Malaysia</option>
<option value="Maldives" >Maldives</option>
<option value="Mali" >Mali</option>
<option value="Malta" >Malta</option>
<option value="Marshall Islands" >Marshall Islands</option>
<option value="Mauritania" >Mauritania</option>
<option value="Mauritius" >Mauritius</option>
<option value="Mexico" >Mexico</option>
<option value="Micronesia" >Micronesia</option>
<option value="Moldova" >Moldova</option>
<option value="Monaco" >Monaco</option>
<option value="Mongolia" >Mongolia</option>
<option value="Montenegro" >Montenegro</option>
<option value="Morocco" >Morocco</option>
<option value="Mozambique" >Mozambique</option>
<option value="Myanmar" >Myanmar</option>
<option value="Namibia" >Namibia</option>
<option value="Nauru" >Nauru</option>
<option value="Nepal" >Nepal</option>
<option value="Netherlands" >Netherlands</option>
<option value="New Zealand" >New Zealand</option>
<option value="Nicaragua" >Nicaragua</option>
<option value="Niger" >Niger</option>
<option value="Nigeria" >Nigeria</option>
<option value="Norway" >Norway</option>
<option value="Oman" >Oman</option>
<option value="Pakistan" >Pakistan</option>
<option value="Palau" >Palau</option>
<option value="Panama" >Panama</option>
<option value="Papua New Guinea" >Papua New Guinea</option>
<option value="Paraguay" >Paraguay</option>
<option value="Peru" >Peru</option>
<option value="Philippines" >Philippines</option>
<option value="Poland" >Poland</option>
<option value="Portugal" >Portugal</option>
<option value="Puerto Rico" >Puerto Rico</option>
<option value="Qatar" >Qatar</option>
<option value="Romania" >Romania</option>
<option value="Russia" >Russia</option>
<option value="Rwanda" >Rwanda</option>
<option value="Saint Kitts and Nevis" >Saint Kitts and Nevis</option>
<option value="Saint Lucia" >Saint Lucia</option>
<option value="Saint Vincent and the Grenadines" >Saint Vincent and the Grenadines</option>
<option value="Samoa" >Samoa</option>
<option value="San Marino" >San Marino</option>
<option value="Sao Tome and Principe" >Sao Tome and Principe</option>
<option value="Saudi Arabia" >Saudi Arabia</option>
<option value="Senegal" >Senegal</option>
<option value="Serbia and Montenegro" >Serbia and Montenegro</option>
<option value="Seychelles" >Seychelles</option>
<option value="Sierra Leone" >Sierra Leone</option>
<option value="Singapore" >Singapore</option>
<option value="Slovakia" >Slovakia</option>
<option value="Slovenia" >Slovenia</option>
<option value="Solomon Islands" >Solomon Islands</option>
<option value="Somalia" >Somalia</option>
<option value="South Africa" >South Africa</option>
<option value="Spain" >Spain</option>
<option value="Sri Lanka" >Sri Lanka</option>
<option value="Sudan" >Sudan</option>
<option value="Suriname" >Suriname</option>
<option value="Swaziland" >Swaziland</option>
<option value="Sweden" >Sweden</option>
<option value="Switzerland" >Switzerland</option>
<option value="Syria" >Syria</option>
<option value="Taiwan" >Taiwan</option>
<option value="Tajikistan" >Tajikistan</option>
<option value="Tanzania" >Tanzania</option>
<option value="Thailand" >Thailand</option>
<option value="Togo" >Togo</option>
<option value="Tonga" >Tonga</option>
<option value="Trinidad and Tobago" >Trinidad and Tobago</option>
<option value="Tunisia" >Tunisia</option>
<option value="Turkey" >Turkey</option>
<option value="Turkmenistan" >Turkmenistan</option>
<option value="Tuvalu" >Tuvalu</option>
<option value="Uganda" >Uganda</option>
<option value="Ukraine" >Ukraine</option>
<option value="United Arab Emirates" >United Arab Emirates</option>
<option value="United Kingdom" >United Kingdom</option>
<option value="United States" >United States</option>
<option value="Uruguay" >Uruguay</option>
<option value="Uzbekistan" >Uzbekistan</option>
<option value="Vanuatu" >Vanuatu</option>
<option value="Vatican City" >Vatican City</option>
<option value="Venezuela" >Venezuela</option>
<option value="Vietnam" >Vietnam</option>
<option value="Yemen" >Yemen</option>
<option value="Zambia" >Zambia</option>
<option value="Zimbabwe" >Zimbabwe</option>

            </select>
        <label for="pays">Pays</label>
    </div> 
        </li>       <li id="li_5" >
        <label class="description" for="telephone">Téléphone </label>
        <div>
            <input id="telephone" name="telephone" class="element text medium" type="text" maxlength="255" value=""/> 
        </div> 
        </li>       <li id="li_6" >
        <label class="description" for="message">Votre message </label>
        <div>
            <textarea id="message" name="message" class="element textarea medium"></textarea> 
        </div> 
        </li>

                    <li class="buttons">
                <input type="hidden" name="form_id" value="306225" />

                <input id="saveForm" class="button_text" type="submit" name="submit" value="Envoyer" />
        </li>
            </ul>
        </form> 
        <div id="footer">
            Generated by <a href="http://www.phpform.org">pForm</a>
        </div>
    </div>
    </body>
</html>

    <?php
$required = array('sujet', 'email', 'prenom', 'nom', 'numeroclient', 'rue', 'adresse2', 'ville', 'region', 'codepostal', 'pays', 'telephone', 'message');

$error = false;
foreach($required as $field) {
  if (empty($_POST[$field])) {
    $error = true;
     }
}

    $sujet = $_POST['sujet'];
    $email = $_POST['email'];
    $prenom = $_POST['prenom'];
    $nom = $_POST['nom'];
    $numeroclient = $_POST['numeroclient'];
    $rue = $_POST['rue'];
    $adresse2 = $_POST['adresse2'];
    $ville = $_POST['ville'];
    $region = $_POST['region'];
    $codepostal = $_POST['codepostal'];
    $pays = $_POST['pays'];
    $telephone = $_POST['telephone'];
    $message = $_POST['message'];

    $msg = "sujet: \t $sujet \n";
    $msg .= "email: \t $email \n";
    $msg .= "prenom:\t $prenom \n";
    $msg .= "nom:\t $nom \n";
    $msg .= "numeroclient:\t $numeroclient \n";
    $msg .= "rue:\t $rue \n";
    $msg .= "adresse2:\t $adresse2 \n";
    $msg .= "ville:\t $ville \n";
    $msg .= "region:\t $region \n";
    $msg .= "codepostal:\t $codepostal \n";
    $msg .= "pays:\t $pays \n";
    $msg .= "telephone:\t $telephone \n";
    $msg .= "message:\t $message \n";

    $recipient = "[email protected]";
    $subject = "$sujet \t $prenom \t $nom";

    $mailheaders = "From: Site internet PMW <[email protected]> \n";
    $mailheaders .= "Reply-To: $email \n\n";

    mail($recipient, $subject, $msg, $mailheaders);

    echo "<HTML><HEAD>";
    echo "<title>Merci</title>";
    echo "</head>";
    echo "<body leftmargin=\"0\" topmargin=\"0\" marginwidth=\"0\" marginheight=\"0\">";
    echo "<table width=\"650\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\">";
    echo "<tr>";
    echo "</tr>";
    echo "<tr>";
    echo "</tr>";
    echo "<tr>";
    echo "</tr>";
    echo "<td align=\"center\" valign=\"top\"></br></br>Merci pour votre message, nous l'avons pris en compte. <a href=\"http://www.puzzles-et-jeux.com/\">Revenir a PMW</a></td>";
    echo "</table>";
    echo "</body>";
    echo "</html>";
    ?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦回旧景 2024-12-27 12:56:16

我看到你正在创建一个 $error 标志,但看起来你并没有用它做任何事情。您应该测试此 $error 并显示不同的 HTML 结果(并放弃通过电子邮件发送结果)(如果存在)。

I see you're creating an $error flag, but it doesn't look as if you're doing anything with it. You should test this $error and show a different HTML result (and forego emailing the results) if it exists.

ぇ气 2024-12-27 12:56:16

问题出在哪里?你可以这样做:

$error = false;
foreach(...)
{
  // set $error = true if empty (I'd also check for invalid values if I were you)
}
if($error)
{
  echo 'error message';
  // display form again using value="$_POST['fieldname']"
}
else
{
  //...
  mail(...);
  echo 'thank you message';
}

Where is the problem? You can just do this:

$error = false;
foreach(...)
{
  // set $error = true if empty (I'd also check for invalid values if I were you)
}
if($error)
{
  echo 'error message';
  // display form again using value="$_POST['fieldname']"
}
else
{
  //...
  mail(...);
  echo 'thank you message';
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文