从验证码清理我的php联系表单

时间:2014-03-14 00:27:06

标签: javascript php html5 contact

我在清理和填写联系表格方面遇到了一些麻烦。

我使用的是具有验证码验证的模板,我不想要它。我删除了一个"网站输入。

我是php的新手,所以我不知道我要删除的内容,而且我不想创建新的php / js文件

我的contact-send.php文件位于php文件夹中。所以我假设html中的动作调用很好。

我还有另外两个文件(captcha_page.php和image.php,但我不会在这里使用,会删除它,所以我不会发布代码)

<form class="contact-form" method="post" action="php/contact-send.php">

<p class="input-block">
    <input type="text" name="name" id="name" placeholder="Name *" />
 </p>

 <p class="input-block">
    <input type="email" name="email" id="email" placeholder="Email *" />
 </p>

 <p class="input-block">
    <textarea name="message" id="message" placeholder="Message *"></textarea>   
 </p>


 <p class="input-block">
    <button class="button turquoise submit" type="submit" id="submit"><i class="icon-paper-plane"></i></button>
 </p>

 </form><!--/ .contact-form-->


<?php

if (isset($_REQUEST['action'])) {

    if ($_REQUEST['action'] == "contact_form_request") {

        $ourMail = "xxx.xxxx@gmail.com";

        $required_fields = array("name", "email", "message");
        $pre_messagebody_info = "";

        $errors = array();
        $data = array();

        parse_str($_REQUEST['values'], $data);

        //check for required and assemble message

        if (!empty($data)) {
            foreach ($data as $key => $value) {
                $name = strtolower(trim($key));
                if (in_array($name, $required_fields)) {
                    if (empty($value)) {

                        if ($name == "name") {
                            $errors[$name] = "Please enter your Name before sending the message";
                        }

                        if ($name == "message") {
                            $errors[$name] = "Please enter your message ";
                        }

                        if ($name == "email") {
                            if (!isValidEmail($value)) {
                                $errors[$name] = "Please enter correct Email address";
                            }           
                        }

                    }
                }
            }
        }

        session_start();
        $verify = $_SESSION['verify'];
        if ($verify != md5($data['verify'])) {
            $errors["verify"] = "Please enter correctly captcha";
        }

        $result = array (
            "is_errors" => 0,
            "info" => ""
        );

        if (!empty($errors)) {
            $result['is_errors'] = 1;
            $result['info'] = $errors;
            echo json_encode($result);
            exit;
        }

        $pre_messagebody_info .= "<strong>Name</strong>" . ": " . $data['name'] . "<br />";
        $pre_messagebody_info .= "<strong>E-mail</strong>" . ": " . $data['email'] . "<br />";
        $pre_messagebody_info .= "<strong>Website</strong>" . ": " . $data['website'] . "<br />";

        $headers = 'MIME-Version: 1.0' . "\r\n";
        $headers.= 'Content-type: text/html; charset=UTF-8' . "\r\n";
        $headers.= "From: " . $data['email'] . "\r\n";

        $after_message = "\r\n<br />--------------------------------------------------------------------------------------------------\r\n<br /> This mail was sent via contact form";

        if (mail($ourMail, "Email from contact form", $pre_messagebody_info .="<strong>Message</strong>" . ": " . nl2br($data['message']) . $after_message, $headers)) {
            $result["info"] = "success";
        } else {
            $result["info"] = "server_fail";
        }

        echo json_encode($result);
        exit;
    }
}

function isValidEmail($email) {
    return filter_var($email, FILTER_VALIDATE_EMAIL);
}

?>


    if ($('.contact-form').length) {

        var $form = $('.contact-form'),
            $loader = '<span>Loader...</span>';
            $form.append('<div class="hide contact-form-responce" />');

        $form.each(function () {

            var $this = $(this), 
                $response = $('.contact-form-responce', $this).append('<p></p>');

            $this.submit(function () {

                $response.find('p').html($loader);

                var data = {
                    action: "contact_form_request",
                    values: $this.serialize()
                };

                //send data to server
                $.post("php/contact-send.php", data, function (response) {

                    $('.wrong-data', $this).removeClass("wrong-data");
                    $response.find('span').remove();    

                    response = $.parseJSON(response);

                    if (response.is_errors) {

                        var p = $response.find('p');

                        p.removeClass().addClass("error");
                        $.each(response.info, function (input_name, input_label) {
                            $("[name=" + input_name + "]", $this).addClass("wrong-data");
                            p.append(input_label + '</br>');
                        });
                        $response.show(300);
                    } else {
                        $response.find('p').removeClass().addClass('success');
                        if (response.info === 'success') {
                            $response.find('p').append('Your email has been sent!');
                            $this.find('input, textarea, select').val('').attr('checked', false);
                            $response.show(300).delay(2500).hide(400);
                        }

                        if (response.info === 'server_fail') {
                            $response.find('p').append('Server failed. Send later!');
                            $response.show(300);
                        }
                    }

                    // Scroll to bottom of the form to show respond message
                    var bottomPosition = $response.offset().top - 50;

                    if ($(document).scrollTop() < bottomPosition) {
                        $('html, body').animate({ scrollTop : bottomPosition });
                    }


                });

                return false;

            });
        });

    }
抱歉,html,php和js在一起,没有设法让事情变得更好......

非常感谢你们!

雷米

1 个答案:

答案 0 :(得分:1)

删除这些行:

   $verify = $_SESSION['verify'];
    if ($verify != md5($data['verify'])) {
        $errors["verify"] = "Please enter correctly captcha";
    }