【问题标题】:don't send the form message if input is empty如果输入为空,则不发送表单消息
【发布时间】:2016-03-31 13:31:22
【问题描述】:

我的contact.php确实有点问题:(

我的页面需要一个联系表格(非常简单,只需姓名、电子邮件和消息)。 它工作得很好,但即使输入为空,它也会发送消息。我的输入确实有“必需”,但它似乎不起作用。我的php技能不是那么好。但是你肯定明白问题出在哪里。

如果有空字段,表单不应发送消息并显示警报。

我当前的代码:

HTML

    <form method="post" id="myform" action="contact.php">
        <div class="field half first">
            <label for="name">Ihr Name</label>
            <input type="text" name="name" id="name" required="required">
        </div>
        <div class="field half">
            <label for="email">Ihre E-Mail</label>
            <input type="text" name="email" id="email" required="required">
        </div>
        <div class="field">
            <label for="message">Ihre Nachricht</label>
            <textarea name="message" id="message" rows="5" required="required"></textarea>
        </div>
        <ul class="actions">
            <li>
                <a href="" class="button submit">Senden</a>
            </li>
        </ul>
    </form>

contact.php

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

$mail_to = 'mail@gmail.com';
$subject = 'Nachricht von '.$field_name;

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

$headers = 'Von: '.$field_email."\r\n";
$headers .= 'Antworte an: '.$field_email."\r\n";

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

if ($mail_status) { ?>
    <script language="javascript" type="text/javascript">
        alert('Ich bedanke mich für Ihre Nachricht. Bald werde ich Sie kontaktieren./ Thank you for your message.');
    window.location = 'http://';
    </script>
    <?php
     }
else { ?>
     <script language="javascript" type="text/javascript">
        alert('Senden fehlgeschlagen./ Could not send the message');
    window.location = 'http://';
     </script>
<?php
}
?>

任何想法,如何解决这个小问题? :(

【问题讨论】:

标签: php forms validation contact-form


【解决方案1】:

我认为如果您的目标是防止某人发送空白消息/联系人,只需在输入标签的末尾使用“必需”,如下所示:

&lt;input type="text" name="something" required/&gt;

或者在你的情况下,应该是这样的:

<form method="post" id="myform" action="contact.php">
        <div class="field half first">
            <label for="name">Ihr Name</label>
            <input type="text" name="name" id="name" required>
        </div>
        <div class="field half">
            <label for="email">Ihre E-Mail</label>
            <input type="text" name="email" id="email" required>
        </div>
        <div class="field">
            <label for="message">Ihre Nachricht</label>
            <textarea name="message" id="message" rows="5" required></textarea>
        </div>
        <ul class="actions">
            <li>
                <a href="" class="button submit">Senden</a>
            </li>
        </ul>
    </form>

“必需”是一个简单的 html5 标记属性,可帮助您防止有人发送空白表单。我希望它对你有用,确保你使用的是 html5。

【讨论】:

  • 试过了,它显示“谢谢你的提醒”消息,然后我必须点击确定,它仍然发送文本,输入邮件为空白
【解决方案2】:

PHP 中有一个名为empty() 的函数。您可以将其与 $_POST 函数结合使用,如下例所示:

<?php
// The form is submitted.
if(isset($_POST["submit_button"])) {
    // Now check if the posted input element is empty, if empty stop by echo a error message
    // Otherwhise continue executing script
    if(empty($_POST["form_name"])) {
        echo "You forgot to fill in this form-element.";

    }else{
        // Continue
    }
}
?>

阅读有关此功能的更多信息:http://php.net/manual/en/function.empty.php

【讨论】:

    【解决方案3】:

    使用isset()函数检查表单是否提交:

    isset — 确定变量是否已设置且不为 NULL

    然后,使用empty()函数检查字段是否填写:

    empty — 判断一个变量是否为空

    // this will return true if the Submit Button is clicked
    if(isset($_POST["submit_button"])) {
    
        if(empty($_POST['name']) {
            echo "Name is not filled";
        } else if (empty($_POST['email']) {
            echo "Email is not filled";
        } else if (empty($_POST['message']) {
            echo "Message is not filled";
        } else {
            // Your original code
        }
    

    【讨论】:

    • 我试过了,我的原始代码在 else 中,但是它打开了我的 contact.php (if(isset($_POST["submit_button"]))...) 并且消息仍然发送。
    猜你喜欢
    • 2013-10-16
    • 1970-01-01
    • 2013-11-14
    • 1970-01-01
    • 2017-09-28
    • 2019-12-04
    • 2017-12-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多