在包含PHP的页面中进行jquery验证后无法使用PHP进行验证

时间:2013-07-18 17:14:41

标签: php jquery

有人可以帮我弄清楚如何在jquery验证后让我的表单提交给php。

我找到了一个教程“构建一个整洁的HTML5支持联系表单”,如果我构建表单,以便它是一个完整的页面,头部和身体验证。但是我的网站的设计使所有页面的页眉和页脚都相同,我在中心包含不同的内容,例如:

[HTTP://mysite/index.php页=接触]

这是JS脚本:

`$(function(){
        $("#refreshimg").click(function(){
            $.post('newsession.php');
            $("#captchaimage").load('image_req.php');
            return false;
        });

        $("#contact_form").validate({
            rules: {
                    name: {
                    required: true,
                    minlength: 2
                  },
                  //telephone, email & message rules removed

                captcha: {
                    required: true,
                    remote: "process.php"
                }
            },
            //messages removed

            errorContainer: $('#errors'),
            errorLabelContainer: $('#errors ul'),
            wrapper: 'li',

            var dataString = 'name='+ name + '&email=' + email + '&telephone=' + telephone + '&message=' + message;
              //alert (dataString);return false;
              submitHandler: function(form) {
              $(form).ajaxSubmit({
                type: "POST",
                url: "pages/processC.php",
                data: dataString,
                success: function() {
                  $('#contactForm').html("<div id='message'></div>");
                  $('#message').html("<h2>Contact Form Submitted!</h2>")
                  .append("<p>We will be in touch soon.</p>")
                  .hide()
                  .fadeIn(1500, function() {
                    $('#message').append("<img id='checkmark' src='images/check.png' />");
                  });
                }
              });
              }
            return false;

            onkeyup: false
        }); `

The tutorial I followed came with the following submitHandler:

` /*submitHandler: function() {
            alert("Correct captcha!");
        },
        success: function(label) {
            label.addClass("valid").text("Valid captcha!")
        },*/`

我注释掉了,当表格是一整页时,表格会提交。

我在另一个教程中找到了上面的ajaxSubmit,但是当我点击提交时,它会导航到我的索引页面。

这是我的PHP文件:

`     ?&GT;

<?php
if( isset($_POST) ){
    //form validation vars
    $formok = true;
    $p_errors = array();

    //sumbission data
    $ipaddress = $_SERVER['REMOTE_ADDR'];
    $date = date('d/m/Y');
    $time = date('H:i:s');

    //form data
    $name = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
    $email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
    $telephone = filter_var($_POST['telephone'], FILTER_SANITIZE_NUMBER_INT);
    $message = filter_var($_POST['message'], FILTER_SANITIZE_STRING);
    $captcha = filter_var($_POST['captcha'], FILTER_SANITIZE_STRING);

    //validate form data

    //validate name is not empty
    if(empty($name)){
        $formok = false;
        $errors[] = "You have not entered a name";
    }   

    //validate telephone is numbers 
    if(empty($telephone)){
        $formok = true;
    }

    //validate email address is not empty
    if(empty($email)){
        $formok = false;
        $errors[] = "You have not entered an email address";
    //validate email address is valid
    }

    //validate message is not empty
    if(empty($message)){
        $formok = false;
        $errors[] = "You have not entered a message";
    }

    //validate captcha is not empty
    session_start();
    if(empty($captcha)){
        $formok = false;
        $errors[] = "Please answer captcha question";
    }
    elseif(strtoupper($_POST['captcha']) == $_SESSION['captcha_id'])
    {
    //Do your stuff
    unset ($_SESSION["captcha_id"]);
    }
    else
    {
    $formok = false;
    $errors[] = "Wrong code entered";
    }

    //send email if all is ok --REMOVED


    //what we need to return back to our form
    $returndata = array(
        'posted_form_data' => array(
            'name' => $name,
            'email' => $email,
            'telephone' => $telephone,
            'message' => $message
        ),
        'form_ok' => $formok,
        'errors' => $errors
    );

    //if this is not an ajax request
    if(empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) !== 'xmlhttprequest'){
        //set session variables
        session_start();
        $_SESSION['cf_returndata'] = $returndata;

        //redirect back to form
        header('location: ' . $_SERVER['HTTP_REFERER']);
    }
}`  

我希望我没有放太多代码,但我对PHP和Jquery的了解非常有限,但我真的需要解决这个问题,所以任何建议都会非常感激。

提前致谢, 詹姆斯

1 个答案:

答案 0 :(得分:0)

请勿尝试手工制作贴身。使用您的方法会引入许多编码问题。

而不是:

var dataString = 'name='+ name + '&email=' + email + '&telephone=' + telephone + '&message=' + message; 

var dataObj = {"name":name, "email":email, "telephone":telephone};

然后在你的ajax呼叫改变

data: dataString,

data: dataObj;

这将避免很多极端情况和编码问题。