我与jquery和php的联系表格不起作用,我找不到错误

时间:2015-01-27 12:16:11

标签: php jquery html

这是我的联系表单代码,我无法开始工作。有人可以帮助我,因为我现在不真的PHP。我还添加了CSS。我是从教程中得到的,但出于某种原因,我的代码无效。

<?php

$hasError = false;
$sent = false;

if(isset($_POST['submitform'])) {
    $name = trim(htmlspecialchars($_POST['name'], ENT_QUOTES));
    $email = trim($_POST['email']);
    $message = trim(htmlspecialchars($_POST['message'], ENT_QUOTES));

    $fieldsArray = array(
        'name' => $name,
        'email' => $email,
        'message' => $message
    );

    $errorArray = array();

    foreach($fieldsArray as $key => $val) {
        switch ($key) {
            case 'name':
            case 'message':
                if (empty($val)) {
                    $hasError = true;
                    $errorArray[$key] = ucfirst($key) . " field was left empty.";
                }
                break;
            case 'email':
                if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
                    $hasError = true;
                    $errorArray[$key] = "Invalid email address";
                } else {
                    $email = filter_var($email, FILTER_SANITIZE_EMAIL);
                }
                break;
        }
    }

    if($hasError !== true) {
        $to = "lodewicus@gmail.com";
        $subject = "Message from website"
        $msgcontents = "Name: $name<br/>Email: $email<br/>Message: $message";
        $headers = "MIME-Version: 1.0 \r\n";
        $headers .= "Content-type: text/html; charset=iso-8859-1 \r\n";
        $headers .= "From: $name <$email> \r\n";
        $mailsent = mail($to, $subject, $msgcontents, $headers);
        if($mailsent) {
            $sent = true;
            unset($name);
            unset($email);
            unset($message);
        }
    }
}

?>
<!DOCTYPE HTML>
<html>
<head>
    <meta charset="UTF-8">
    <title>Contact From</title>
    <link rel="stylesheet" type="text/css" href="index.css">
    <script type="text/javascript" src="//code.jquery.com/jquery-1.10.2.min.js"></script>
    <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script>
    <script type="text/javascript">
    jQuery(document).ready(function($) {
        $("#contactform").validate({
            rules: {
                name: {
                    required: true,
                    minlength: 2
                },
                email: {
                    required: true,
                    email: true
                },
                message: {
                    required: true,
                    minlength: 20
                }
            },
            messages: {
                name: {
                    required: "Please enter your name",
                    minlength: "Your name must be at least 2 characters!"
                },
                email: {
                    required: "Please enter your email address",
                    email: "Please enter a valid email address"
                },
                message: {
                    required: "Please enter your message",
                    minlength: "Your message must be at least 20 characters"
                }
            }
        });
    });
    </script>
</head>
<body>
    <div class="container">
        <h1>Contact Form</h1>
        <form id="contactform" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" novalidate>
            <?php
                if($sent === true) {
                    echo "<h2 class='success'>Thanks, your message has been sent successfully</h2>";
                } elseif($hasError === true) {
                    echo '<ul class="errorlist">';
                    foreach($errorArray as $key => $val) {
                        echo "<li>" . ucfirst($key) . " field error - $val</li>";
                    }
                    echo '</ul>;'
                }
            ?>
            <input type="text" name="name" value="<?php echo (isset($name) ? $name : ""); ?>" placeholder="Your Name">
            <input type="email" name="email" value="<?php echo (isset($email) ? $email : ""); ?>" placeholder="Your Email">
            <textarea name="message" placeholder="Your Message"><?php echo (isset($message) ? $message : ""); ?></textarea>
            <input type="submit" name="submitform" value="Send">
        </form>
    </div>
</body>
</html>




    *, *:before, *:after {
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
}

*, html {
    margin: 0;
    padding: 0;
    outline: none;
}

html {
    font-size: 62.5%;
}

::-ms-clear {
    display: none;
}

::-webkit-search-cancel-button {
    -webkit-appearance: none;
}

body {
    font-family: Arial, sans-serif;
    font-size: 14px;
    font-weight: normal;
    background-color: #fff;
    color: #222;
}

.container {
    position: relative;
    width: 600px;
    margin: 0 auto;
}

.container:first-of-type {
    top: 100px;
}

h1 {
    text-align: center;
    font-size: 42px;
    font-size: 4.2rem;
    margin-bottom: 10px;
    color: #ff3300;
}

#contactform {
    padding: 20px;
    border: 1px solid #eee;
    -webkit-box-shadow: 0px 2px 3px #ddd;
    -moz-box-shadow: 0px 2px 3px #ddd;
    box-shadow: 0px 2px 3px #ddd;
    -webkit-border-radius: 5px;
    -moz-border-radius: 5px;
    border-radius: 5px;
}

#contactform input[type=text],
#contactform input[type=email],
#contactform input[type=submit],
#contactform textarea {
    width: 100%;
    height: 34px;
    display: block;
    padding: 5px 10px;
    border: 1px solid #ddd;
    color: #222;
    margin-bottom: 10px;
}

#contactform textarea {
    height: 100px;
    font-family: Arial, sans-serif;
}

#contactform input[type=submit] {
    width: 200px;
    margin-bottom: 0;
    background-color: #fff;
    -webkit-border-radius: 2px;
    -moz-border-radius: 2px;
    border-radius: 2px;
    text-transform: uppercase;
    cursor: pointer;
    -webkit-transition: all .3s ease-in-out;
    -moz-transition: all .3s ease-in-out;
    transition: all .3s ease-in-out;
}

#contactform input[type=submit]:hover {
    background-color: #ff3300;
    color: #fff;
    border-color: #dd2c00;
}

h2.success {
    color: #27ae60;
    margin-bottom: 20px;
}

.errorlist {
    list-style: none;
}

label.error, 
.errorlist li {
    margin-bottom: 10px;
    color: #bb0000;
    display: block;
}

.errorlis li:last-child {
    margin-bottom: 20px;
}

3 个答案:

答案 0 :(得分:1)

更改结束ul
 echo '</ul>;'

 echo '</ul>';

答案 1 :(得分:1)

替换以下两行

行号:112 替换echo '</ul>';代表echo '</ul>;'

第二是否:42 完成分号码41号线以下代码显示错误错误

    $subject = "Message from website" ****** This Line Has No Semicolon
    $msgcontents = "Name: $name<br/>Email: $email<br/>Message: $message";

使用此代码进行复制

    $subject = "Message from website";
    $msgcontentss = "Name: $name<br/>Email: $email<br/>Message: $message";

答案 2 :(得分:0)

将此作为答案添加,并在评论中确认为问题。

您确定自己的网页是.php而不是.html吗?