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

Posted

技术标签:

【中文标题】我的 jquery 和 php 联系表不起作用,我找不到错误【英文标题】:my contact form with jquery and php is not working, i cant find the error 【发布时间】:2015-03-26 01:24:46 【问题描述】:

这是我的联系表格代码,我无法上班。有人可以帮助我,因为我现在真的不是 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;

【问题讨论】:

您能否给出一些故障或故障征兆以帮助我们诊断? 如果我们确切地知道“不工作”是什么意思,那么为您提供帮助会容易得多。 问题出在这 118 行 css 中 当我尝试在 chrome 中运行它时,它会在左上角和联系表单内出现一堆我的 php 代码! 我想添加图片,但不允许! 【参考方案1】:

将其添加为答案,已确认为 cmets 中的问题。

您是否确定您的页面是 .php 而不是 .html?

【讨论】:

【参考方案2】:

替换以下两行

行号:112 替换echo '&lt;/ul&gt;';代表echo '&lt;/ul&gt;;'

第二个是没有: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";

【讨论】:

【参考方案3】:

更改关闭 ul 来自

 echo '</ul>;'

 echo '</ul>';

【讨论】:

谢谢,确实进行了更改,但仍然无法正常工作!

以上是关于我的 jquery 和 php 联系表不起作用,我找不到错误的主要内容,如果未能解决你的问题,请参考以下文章

带有 Ajax + 数据表的 CRUD 表不起作用

精灵表不起作用

插入日期选择器后 Jquery 表单验证不起作用

联系表格 PHP 重定向不起作用

插入蜂巢表不起作用

使用jquery sql插入多行并且php不起作用