首页 > 解决方案 > 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();
}
}
}
}

有谁知道如何解决这一问题?提前致谢!

标签: php

解决方案


推荐阅读