0

我在 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>
4

3 回答 3

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
     }
}
于 2013-02-21T09:55:42.610 回答
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
}
于 2013-02-21T09:55:19.913 回答
0

尝试将代码放在其中 -

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

或者

if(!empty($_POST)){
// send email
}
于 2013-02-21T09:56:05.830 回答