douliangli6407 2015-01-27 12:16
浏览 61

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

Here is my code for a contact form, i cant get to work. Can someone please help me since i dont really now php. i also added the css. i got it from a tutorial but for some reason my code is not working.

<?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 
";
        $headers .= "Content-type: text/html; charset=iso-8859-1 
";
        $headers .= "From: $name <$email> 
";
        $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条回答 默认 最新

  • doumeng3188 2015-01-27 12:19
    关注

    Change closing ul from

     echo '</ul>;'
    

    TO

     echo '</ul>';
    
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)