转发后联系表格空白

时间:2011-12-30 21:11:43

标签: php html forms

我的联系表格有问题。发送数据时,我希望再次将这些字段留空。我已经尝试了很多,但没有任何作用。

以下是发送邮件的脚本:

<?php
error_reporting(0);
session_start();


if ("post" === strtolower($_SERVER['REQUEST_METHOD'])) {

$name = $_POST['name'];
$email = $_POST['email'];
$adres = $_POST['adres'];
$postcode = $_POST['postcode'];
$woonplaats = $_POST['woonplaats'];
$telefoon = $_POST['telefoon'];
$dag = $_POST['dag'];
$dag_nummer = $_POST['dag_nummer'];
$maand = $_POST['maand'];
$tijd = $_POST['tijd'];
$opmerking = $_POST['opmerking'];

$_SESSION['name'] = $name;
$_SESSION['email'] = $email;
$_SESSION['adres'] = $adres;
$_SESSION['postcode'] = $postcode;
$_SESSION['woonplaats'] = $woonplaats;
$_SESSION['telefoon'] = $telefoon;
$_SESSION['dag'] = $dag;
$_SESSION['dag_nummer'] = $dag_nummer;
$_SESSION['maand'] = $maand;
$_SESSION['tijd'] = $tijd;
$_SESSION['opmerking'] = $opmerking;

if (empty($name) || empty($email) || empty($telefoon) || empty($tijd)) {

    if (empty($name)) {
        $errors[] = "name";
    }

    if (empty($email)) {
        $errors[] = "email";
    }

    if (empty($telefoon)) {
        $errors[] = "telefoon";
    }

    if (empty($tijd)) {
        $errors[] = "tijd";
    }

    header("Location: afspraak.php?error[]=" .implode('&error[]=', $errors));
    exit;
} else {

    // send mail if (post) parameters are correct
    mail(
        'tim-luijten@hotmail.com', 
        'Afsprakenformulier o-vita.nl', 
        "Er is contact opgenomen via het afsprakenformulier. Zie onderstaande gegevens: \n\n
        Naam: {$name} \n
        E-mailadres: {$email} \n
        Adres: {$adres} \n
        Postcode: {$postcode} \n
        Woonplaats: {$woonplaats} \n
        Telefoonnummer: {$telefoon} \n
        Datum: {$dag} - {$dag_nummer} - {$maand} \n
        Tijd: {$tijd} \n
        Opmerking: {$opmerking} \n"         
                    ,
        "From: {$email} \r\n"
    );

    header("Location: bedankt_afspraak.php");
    exit;
}

}
header("Location: afspraak.php");
?>

表格:

              <form action="afspraak_verstuur.php" method="post" name="contact_form">
                <table width="300" border="0">
                        <tr>
                        <td class="tabform">Naam*:</td>
                        <td>&nbsp;</td>
                        </tr>
                        <tr>
                        <td colspan="2">
                                <input name="name" type="text" class="text_veld" value="<?php echo (isset($_SESSION['name'])) ? $_SESSION['name'] : ""; ?>" /></td>
                </tr>
                <?php if (in_array('name', $_GET['error'])) { ?>
                <tr>
                        <td colspan="2">
                                <font color="red">Er is geen naam ingevuld</font>
                        </td>
                </tr>
                <?php } ?>


                        <tr>
                        <td class="tabform">E-mail*:</td>
                                <td>&nbsp;</td>
                        </tr>
                        <tr>
                                <td colspan="2">
                                        <input name="email" type="text" class="text_veld" value="<?php echo (isset($_SESSION['email'])) ? $_SESSION['email'] : ""; ?>" />
                                </td>
                        </tr>
                        <?php if (in_array('email', $_GET['error'])) { ?>
                <tr>
                        <td colspan="2">
                                <font color="red">Er is geen e-mailadres ingevuld</font>
                        </td>
                </tr>
                <?php } ?>
                        <tr>


                        <tr>
                        <td class="tabform">Adres:</td>
                                <td>&nbsp;</td>
                        </tr>
                        <tr>
                                <td colspan="2">
                                        <input name="adres" type="text" class="text_veld" value="<?php echo (isset($_SESSION['adres'])) ? $_SESSION['adres'] : ""; ?>" />
                                </td>
                        </tr>
                        <?php if (in_array('adres', $_GET['error'])) { ?>
                <tr>
                        <td colspan="2">
                                <font color="red">Er is geen adres ingevuld</font>
                        </td>
                </tr>
                <?php } ?>
                        <tr>


                                <tr>
                        <td class="tabform">Postcode:</td>
                                <td>&nbsp;</td>
                        </tr>
                        <tr>
                                <td colspan="2">
                                        <input name="postcode" type="text" class="text_veld" value="<?php echo (isset($_SESSION['postcode'])) ? $_SESSION['postcode'] : ""; ?>" />
                                </td>
                        </tr>
                        <?php if (in_array('postcode', $_GET['error'])) { ?>
                <tr>
                        <td colspan="2">
                                <font color="red">Er is geen postcode ingevuld</font>
                        </td>
                </tr>
                <?php } ?>
                        <tr>


            <tr>
                        <td class="tabform">Woonplaats:</td>
                                <td>&nbsp;</td>
                        </tr>
                        <tr>
                                <td colspan="2">
                                        <input name="woonplaats" type="text" class="text_veld" value="<?php echo (isset($_SESSION['woonplaats'])) ? $_SESSION['woonplaats'] : ""; ?>" />
                                </td>
                        </tr>
                        <?php if (in_array('woonplaats', $_GET['error'])) { ?>
                <tr>
                        <td colspan="2">
                                <font color="red">Er is geen woonplaats ingevuld</font>
                        </td>
                </tr>
                <?php } ?>
                        <tr>


                        <tr>
                        <td class="tabform">Telefoon*:</td>
                                <td>&nbsp;</td>
                        </tr>
                        <tr>
                                <td colspan="2">
                                        <input name="telefoon" type="text" class="text_veld" value="<?php echo (isset($_SESSION['telefoon'])) ? $_SESSION['telefoon'] : ""; ?>" />
                                </td>
                        </tr>
                        <?php if (in_array('telefoon', $_GET['error'])) { ?>
                <tr>
                        <td colspan="2">
                                <font color="red">Er is geen telefoonnummer ingevuld</font>
                        </td>
                </tr>
                <?php } ?>
                        <tr>

            <tr>
                        <td class="tabform">Datum*:</td>
                                <td>&nbsp;</td>
                        </tr>
                        <tr>
                                <td colspan="2">
                                        <select name="dag" type="text" class="text_dag" value="Selecteer een dag" />
                    <option value="maandag">maandag</option>
                    <option value="dinsdag">dinsdag</option>
                    <option value="woensdag">woensdag</option>
                    <option value="donderdag">donderdag</option>
                    <option value="vrijdag">vrijdag</option>
                    <option value="zaterdag">zaterdag</option>
                    </select>
    <?php echo (isset($_SESSION['dag'])) ? $_SESSION['dag'] : ""; ?>

                    <select name="dag_nummer" type="text" class="text_dag_nummer" value="1" />
                    <option value="1">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                    <option value="4">4</option>
                    <option value="5">5</option>
                    <option value="6">6</option> 
                    <option value="7">7</option>
                    <option value="8">8</option>
                    <option value="9">9</option>
                    <option value="10">10</option>
                    <option value="11">11</option>
                    <option value="12">12</option>
                    <option value="13">13</option>
                    <option value="14">14</option>
                    <option value="15">15</option>
                    <option value="16">16</option>
                    <option value="17">17</option>
                    <option value="18">18</option> 
                    <option value="19">19</option>
                    <option value="20">20</option>
                    <option value="21">21</option>
                    <option value="22">22</option>
                    <option value="23">23</option>
                    <option value="24">24</option>
                    <option value="25">25</option>
                    <option value="26">26</option>
                    <option value="27">27</option>
                    <option value="28">28</option>
                    <option value="29">29</option>
                    <option value="30">30</option>
                    <option value="31">31</option>
                    </select>
    <?php echo (isset($_SESSION['dag_nummer'])) ? $_SESSION['dag_nummer'] : ""; ?>

                    <select name="maand" type="text" class="text_maand" value="Januari" />
                    <option value="januari">januari</option>
                    <option value="februari">februari</option>
                    <option value="maart">maart</option>
                    <option value="april">april</option>
                    <option value="mei">mei</option>
                    <option value="juni">juni</option>
                    <option value="juli">juli</option>
                    <option value="augustus">augustus</option>
                    <option value="september">september</option>
                    <option value="oktober">oktober</option>
                    <option value="november">november</option>
                    <option value="december">december</option>
                    </select>
      <?php echo (isset($_SESSION['maand'])) ? $_SESSION['maand'] : ""; ?>
                                </td>
                        </tr>
                        <?php if (in_array('datum', $_GET['error'])) { ?>
                <tr>
                        <td colspan="2">
                                <font color="red">Er is geen datum ingevuld</font>
                        </td>
                </tr>
                <?php } ?>
                        <tr>


            <tr>
                        <td class="tabform">Tijd*:</td>
                                <td>&nbsp;</td>
                        </tr>
                        <tr>
                                <td colspan="2">
                                        <input name="tijd" type="text" class="text_veld" value="<?php echo (isset($_SESSION['tijd'])) ? $_SESSION['tijd'] : ""; ?>" />
                                </td>
                        </tr>
                        <?php if (in_array('tijd', $_GET['error'])) { ?>
                <tr>
                        <td colspan="2">
                                <font color="red">Er is geen tijd ingevuld</font>
                        </td>
                </tr>
                <?php } ?>
                        <tr>


            <tr>
                        <td class="tabform">Opmerking:</td>
                                <td>&nbsp;</td>
                        </tr>
                        <tr>
                                <td height="60" colspan="2">
                                        <textarea name="opmerking" type="text" class="text_comments" rows="4" value="" /><?php echo (isset($_SESSION['opmerking'])) ? $_SESSION['opmerking'] : ""; ?></textarea>
                                </td>
                        </tr>
                        <?php if (in_array('opmerking', $_GET['error'])) { ?>
                <tr>
                        <td colspan="2">
                                <font color="red">Er is geen opmerking ingevuld</font>
                        </td>
                </tr>
                <?php } ?>
                        <tr>


                        <tr>
                                <td colspan="2" class="verzenden">
                                        <input name="verzenden" type="image" id="verzenden" src="img/verzenden.png" alt="Verzenden" title="Verzenden" />
                                </td>
                        </tr>
                </table>
        </form>

1 个答案:

答案 0 :(得分:1)

由于您使用会话来设置表单输入中的值,因此您可以在发送后将这些会话值设置为空。

} else {

// send mail if (post) parameters are correct
if (mail(
    'tim-luijten@hotmail.com', 
    'Afsprakenformulier o-vita.nl', 
    "Er is contact opgenomen via het afsprakenformulier. Zie onderstaande gegevens: \n\n
    Naam: {$name} \n
    E-mailadres: {$email} \n
    Adres: {$adres} \n
    Postcode: {$postcode} \n
    Woonplaats: {$woonplaats} \n
    Telefoonnummer: {$telefoon} \n
    Datum: {$dag} - {$dag_nummer} - {$maand} \n
    Tijd: {$tijd} \n
    Opmerking: {$opmerking} \n"         
                ,
    "From: {$email} \r\n"
  ))
 {
     $_SESSION['name'] = "";
     $_SESSION['email'] = "";
     $_SESSION['adres'] = "";
     $_SESSION['postcode'] = "";
     $_SESSION['woonplaats'] = "";
     $_SESSION['telefoon'] = "";
     $_SESSION['dag'] = "";
     $_SESSION['dag_nummer'] = "";
     $_SESSION['maand'] = "";
     $_SESSION['tijd'] = "";
     $_SESSION['opmerking'] = "";

     header("Location: bedankt_afspraak.php");

     exit;
   } else {
    //display a message that the message failed to send.
   }
}