0

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

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

[http://mysite/index.php?page=contact]

这是 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 文件:

` ?>

<?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 的了解非常有限,但我真的需要解决这个问题,所以非常感谢任何建议。

在此先感谢,詹姆斯

4

1 回答 1

0

不要尝试手动构建您的帖子正文。使用您的方法会引入很多编码问题。

代替:

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

只是

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

然后在您的ajax调用更改

data: dataString,

data: dataObj;

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

于 2013-07-18T17:20:15.110 回答