php - PHP联系表单不向用户显示错误
问题描述
所以我正在尝试制作这个 PHP 联系表单,如果某些内容留空或输入无效字符,我希望它们显示错误。
我有两个文件:contactform.php 和contact.php。contact.php 包含脚本,contactform.php 包含显示错误的脚本。
联系表格.php:
<html>
<!DOCTYPE html>
<head>
</head>
<div class="container">
<center class="contacttext"><h3>Contact opnemen</h3></center>
<form class"contact-form" action="contact.php" method="POST">
<input type="text" name="naam" placeholder="naam">
<input type="text" name="mail" placeholder="E-mailadres">
<input type="text" name="onderwerp" placeholder="Onderwerp">
<textarea name="bericht" placeholder="bericht"></textarea>
<input type="Submit" value="verstuur"></input>
<?php
$fullUrl = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
if (strpos($fullUrl, "submit=empty") == true) {
echo "<center><p class='error'> Je hebt niet alle velden ingevuld!</p></center>";
exit();
}
elseif (strpos($fullUrl, "submit=char") == true) {
echo "<center><p class='error'> Je hebt ongeldige karakters gebruikt!</p></center>";
exit();
}
elseif (strpos($fullUrl, "submit=email") == true) {
echo "<center><p class='error'> Je hebt een ongeldig e-mailadres gebruikt!</p></center>";
exit();
}
elseif (strpos($fullUrl, "submit=succes") == true) {
echo "<center><p class='error'> Heb bericht is succesvol verstuurd!</p></center>";
exit();
}
?>
</form>
</div>
</body>
</html>
联系方式.php:
<?php
if (isset($_POST['Submit'])) {
$naam = $_POST['naam'];
$onderwerp = $_POST['onderwerp'];
$mailVan = $_POST['mail'];
$bericht = $_POST['bericht'];
$mailNaar = "name@domain.com";
$headers = "From: ".$mailVan;
$txt = "Je hebt een e-mail gekregen van ".$naam.".\n\n".$bericht;
mail($mailNaar, $onderwerp, $txt, $headers);
header("Location: index.php?mailsend");
if (empty($naam) || empty($onderwerp) || empty($mailVan) || empty($bericht)) {
header("Location: index.html?submit=empty");
exit();
} else {
if (!filter_var($mailVan, FILTER_VALIDATE_EMAIL)) {
header("Location: index.html?submit=invalidemail");
exit();
} else {
if (!preg_match("/^[a-zA-Z]*$/", $naam) || !preg_match("/^[a-zA-Z]*$", $onderwerp)) {
header("Location: index.html?submit=char");
exit();
} else {
header("Location: index.html?submit=succes");
exit();
}
}
}
}
有谁知道如何解决这一问题?提前致谢!
解决方案
推荐阅读
- python - 运行提升的 cmd 提示符命令 python
- excel - 将元素插入数组
- php - 使用正则表达式查找元标记的内容?
- python - 如何使用 h5py 为每个 HDF5 列定义单独的数据类型
- flutter - 如何在 Flutter 中隐藏 DataTable 的列标题?
- mysql - 在多列中查找最频繁的数字
- laravel - 无法通过 php artisan migrate 命令在 laravel 中进行迁移
- python - Django REST Framework - 对添加订单的用户的引用
- javascript - 简单的 add 函数返回 NaN
- web - 本网站使用哪些技术来展示 3D 产品?