PHP必填字段

时间:2013-02-21 09:51:57

标签: php html css forms fill

我在PHP中获得了以下代码:

<?php
$field_name = $_POST['name'];
$field_email = $_POST['email'];
$field_message = $_POST['message'];

$mail_to = 'info@remydesigns.nl';
$subject = 'Bericht van '.$field_name;

$body_message = 'Van: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Bericht: '.$field_message;

$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";

$mail_status = mail($mail_to, $subject, $body_message, $headers);

else { ?>
    <script language="javascript" type="text/javascript">
        alert('Bericht mislukt, probeer het nog een keer.');
        window.location = 'index.htm';
    </script>
<?php
}

if ($mail_status) { ?>
    <script language="javascript" type="text/javascript">
        alert('Bedankt voor je bericht.');
        window.location = 'index.htm';
    </script>
<?php
}
?>

即使没有填写字段,每当我点击提交时,它都会发送一封电子邮件。 所以我不断收到人们的空邮件。

如果字段为空,我无法检查字段。我的代码出了什么问题?

由于

编辑:

<?php
if(isset($_POST['name']) && isset($_POST['email']) && isset($_POST['message'])) {
    $field_name = $_POST['name'];
    $field_email = $_POST['email'];
    $field_message = $_POST['message'];

    $mail_to = 'info@remydesigns.nl';
    $subject = 'Bericht van '.$field_name;

    $body_message = 'Van: '.$field_name."\n";
    $body_message .= 'E-mail: '.$field_email."\n";
    $body_message .= 'Bericht: '.$field_message;

    $headers = 'From: '.$field_email."\r\n";
    $headers .= 'Reply-To: '.$field_email."\r\n";

    $mail_status = mail($mail_to, $subject, $body_message, $headers);
}
if ($mail_status) { ?>
    <script language="javascript" type="text/javascript">
        alert('Bedankt voor je bericht.');
        window.location = 'index.htm';
    </script>
<?php
}
else { ?>
    <script language="javascript" type="text/javascript">
        alert('Bericht mislukt, probeer het nog een keer.');
        window.location = 'index.htm';
    </script>
<?php
}
?>

HTML:

<form action="contact.php" method="post">
    <span>Naam</span> <input class="name" name="name" type="text" /> 
    <span>Email</span> <input class="email" name="email" type="text" /> 
    <span>Bericht</span><textarea class="message" name="message"></textarea> 
        <input class="submit" type="submit" value="Submit" />&nbsp;
</form>

3 个答案:

答案 0 :(得分:2)

使用isset

if(isset($_POST['name']) && isset($_POST['email']) && isset($_POST['message'])) {
     $field_name = $_POST['name'];
     $field_email = $_POST['email'];
     $field_message = $_POST['message'];

     $mail_to = 'info@remydesigns.nl';
     $subject = 'Bericht van '.$field_name;

     $body_message = 'Van: '.$field_name."\n";
     $body_message .= 'E-mail: '.$field_email."\n";
     $body_message .= 'Bericht: '.$field_message;

     $headers = 'From: '.$field_email."\r\n";
     $headers .= 'Reply-To: '.$field_email."\r\n";

     $mail_status = mail($mail_to, $subject, $body_message, $headers);

     if ($mail_status) { ?>
         <script language="javascript" type="text/javascript">
             alert('Bedankt voor je bericht.');
             window.location = 'index.htm';
         </script>
     <?php
     }
     else { ?>
         <script language="javascript" type="text/javascript">
             alert('Bericht mislukt, probeer het nog een keer.');
             window.location = 'index.htm';
         </script>
     <?php
     }
}

答案 1 :(得分:0)

使用类似的内容检查它是否为空还检查isset

<?php

if(empty($_POST['name']) || empty($_POST['email']) || empty($_POST['message']))
    die();
if(isset($_POST['name']) && isset($_POST['email']) && isset($_POST['message'])){
now do something
}

答案 2 :(得分:0)

尝试将代码置于此 -

if (isset($_POST['submit'])) {
// send email
}

if(!empty($_POST)){
// send email
}