[英]don't send the form message if input is empty
我的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
}
?>
有什么想法,如何解决这个小问题? :(
我认为,如果您的目标是防止某人发送空白消息/联系人,请在输入标签的末尾使用“ required”,如下所示:
<input type="text" name="something" required/>
或者您的情况应该是这样的:
<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。
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
检查是否使用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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.