如何使用PHP验证我的联系表格

时间:2018-12-13 17:42:30

标签: php html forms process

我是这个网站和PHP的新手,但是我有一个简单的HTML联系人表格,当用户提交该表格时,该表格仅重定向到此页面。www.example.co不起作用。英国目前无法处理此请求。 HTTP错误500。

我一直在研究和研究,按照我能找到的所有教程并阅读了可以在PHP上找到的所有内容,但是似乎没有任何效果。我做错了什么吗?

HTML

<div class="contact">

        <div class="container">
            <h2>Contact US</h2>
        <div class="contact-form">

            <div class="col-md-8 contact-grid">
                <form id="test" action="form-process.php" method="POST">
                    <input name="name" type="text" placeholder="Name" onfocus="this.value='';" onblur="if (this.value == '') {this.value ='name';}" required>
                    <br>
                    <input name="email" type="text" placeholder="Email" onfocus="this.value='';" onblur="if (this.value == '') {this.value ='email';}" required>
                    <br>
                    <select id="contact" name="dropdown" onchange="handleOption(this)" required>
                        <option value="" disabled selected>Crew/Gang</option>
                        <option value="NC Maphia">NC Maphia</option>
                        <option value="deadeye/index">Dead Eye</option>
                    </select><br>
                    <select id="contact" name="dropdown2" required>
                        <option value="" disabled selected>Gang</option>
                        <option value="Grand Theft Auto V">Grand Theft Auto V</option>
                        <option value="Red Dead Redemption II">Red Dead Redemption II</option>
                    </select><br>
                    <select id="contact" name="dropdown3" required>
                        <option value="" disabled selected>Platform</option>
                        <option value="Xbox One">Xbox One</option>
                        <option value="PS4">PS4</option>
                    </select><br>
                    <select id="contact" name="dropdown4" required>
                        <option value="" disabled selected>Reason For Contacting Us</option>
                        <option value="Joining NC Maphia">Joining NC Maphia</option>
                        <option value="Recruitment Requirments">Recruitment Requirments</option>
                        <option value="Events">Events</option>
                        <option value="Report A Member">Report A Member</option>
                        <option value="Webmaster">Webmaster</option>
                        <option value="General Inquiry">General Inquiry</option>
                    </select><br>
                    <input name="subject" type="text" placeholder="Subject" onfocus="this.value='';" onblur="if (this.value == '') {this.value ='subject';}">
                    <br>
                    <textarea name="message" cols="77" rows="6" placeholder="Message" onfocus="this.value='';" onblur="if (this.value == '') {this.value = 'message';}" required></textarea>
                    <div class="send">
                        <input type="submit" name="submit" value="Send" >
                    </div>
                </form>
            </div>
            <div class="col-md-4 contact-in">

                    <div class="address-more">
                    <h4>Address</h4>
                        <p>Maze Bank Tower,</p>
                        <p>Los Santos,</p>
                        <p>America </p>
                    </div>
                    <div class="address-more">
                    <h4>Chat With Us</h4>
                        <p>PSN: Kyle-The-Gunman9</p>
                        <p>PSN: NC_Maphia</p>
                        <p>Email:<a href="mailto:info@ncmaphia.co.uk"> info[at]ncmaphia.co.uk</a></p>
                    </div>

            </div>
            <div class="clearfix"> </div>
        </div>
    </div>
    <script type="text/javascript">
        function handleOption(elm) {
        if(elm.value == "deadeye/index") { // Check if Dead Eye was selected
            console.log("Dead Eye selected, redirecting");
            window.location = elm.value + ".html";
        }
    }
    </script>

PHP

<?php
error_reporting(E_ALL | E_NOTICE);
ini_set('display_errors', '1');

if(isset)($_POST["submit"])) //If the form is submitted
{
$notification="";  //Used for catching all your messages
//Retrieve form data. 
//GET - user submitted data using AJAX
//POST - in case user does not support javascript, we'll use POST instead
$name = $_POST['name'];
$email = $_POST['email'];
$dropdown = $_POST['dropdown'];
$dropdown2 = $_POST['dropdown2'];
$dropdown3 = $_POST['dropdown3'];
$dropdown4 = $_POST['dropdown4'];
$subject = $_POST['subject'];
$message = $_POST['message'];

//flag to indicate which method it uses. If POST set it to 1
if ($_POST) $post=1;
//Simple server side validation for POST data, of course, 
//you should validate the email
if (!$personname) $errors[count($errors)] = 'Please enter your name.';
if (!$email) $errors[count($errors)] = 'Please enter your email.'; 
if (!$dropdown) $errors[count($errors)] = 'Please enter which crew/gang you are attempting to contact.'; 
if (!$dropdown2) $errors[count($errors)] = 'Please enter which game you are referring too.'; 
if (!$dropdown3) $errors[count($errors)] = 'Please enter which platform you are currently playing on.'; 
if (!$dropdown4) $errors[count($errors)] = 'We need too know the reason for your message.';
if (!$message) $errors[count($errors)] = 'We need more detail on why you are contacting us.'; 
//if the errors array is empty, send the mail
if (!$errors) {
//recipient - change this to your name and email
$to = 'MyEmail@example.co.uk';    
//sender
$from = $email;

//subject and the html message
$subject = 'Hello from ' . $name;    
$message = '
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head></head>
<body>
<p>You have recieved a message from $name using your contact form at www.example.co.uk
<table>
    <tr><td>Name: </td><td>' . $name . '</td></tr>
    <tr><td>Email: </td><td>' . $email . '</td></tr>
    <tr><td>Crew/Gang: </td><td>' . $dropdown . '</td></tr>
    <tr><td>Game: </td><td>' . $dropdown2 . '</td></tr>
    <tr><td>Platform: </td><td>' . $dropdown3 . '</td></tr>
    <tr><td>Reason For Contacting Us: </td><td>' . $dropdown4 . '</td></tr>
    <tr><td>Subject: </td><td>' . $subject . '</td></tr>
    <tr><td>Message: </td><td>' . $message . '</td></tr>
</table>
</body>
</html>';
//send the mail
$result = sendmail($to, $subject, $message, $from);

//if POST was used, display the message straight away
if ($_POST) {
    if ($result) echo 'Thank you! We have received your message.';
    else $notification.= 'Sorry, unexpected error. Please try again later';

//else if GET was used, return the boolean value so that 
//ajax script can react accordingly
//1 means success, 0 means failed
} else {
    $notification.= $result;    
}
//if the errors array has values
} else {
//display the errors message
for ($i=0; $i<count($errors); $i++) echo $errors[$i] . '<br/>';
$notification.= '<a href="contact.html">Back</a>';
exit;
}
//Simple mail function with HTML header
function sendmail($to, $subject, $message, $from) {
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
    $headers .= 'From: ' . $from . "\r\n";
    $result = mail($to,$subject,$message,$headers);
    if ($result) return 1;
    else return 0;
}
} //First If loop
?>

我们将非常有帮助,在此先感谢您

1 个答案:

答案 0 :(得分:0)

您的代码中有很多错误。

从第7行开始

if(isset)($ _ POST [“ submit”]))

将此更改为 isset($ _ POST [“ submit”]);

未声明$ errors数组

不是对数组添加错误,而是对数组进行计数。

如果我今天有时间,我将尝试完善您的代码。

同时,我建议您学习更多PHP。我也非常怀疑,当您说您搜索了整个互联网时,这些东西就是基本的php。