此代码将空值发送到表。我怎样才能解决这个问题

时间:2016-07-18 08:18:31

标签: javascript jquery ajax

我的代码应该验证表单并将信息发送到php页面。我知道验证和php工作,但由于某种原因,它是插入表中的空白。我怎样才能解决这个问题?我认为它的ajax部分不起作用。 虽然它确实发送到php页面,但它发送空变量。我该如何解决这个问题?

$(function() {
    // Setup form validation on the #register-form element
    $("#val2").validate({
        // Specify the validation rules
        rules: {
            Name: {
                required: true,
                maxlength: 50
            },
            telephone: {
                required: true,
                number: true
            },
            Email: {
                required: true,
                email: true
            },
            message: {
                required: true,
                minlength: 30
            },
        },

        // Specify the validation error messages
        messages: {
            Name: {
                required: "Please enter your name",
                maxlength: "Please Keep your name under 50 characters"
            },
            telephone: {
                required: "Please enter your number",
                number: "Please enter a valid number"
            },
            message: {
                required: "Please provide a password",
                minlength: "Your message must be at least 30 characters long"
            },
            Email: "Please enter a valid email address",
        },

        submitHandler: function(form) {
            debugger;
            var name = form.Name.value;
            var email = form.Email.value;
            var number = form.telephone.value;
            var message = form.message.value;
            var product = form.product.value;

            $.ajax({
                type: "POST",
                url: 'process.php',
                data: name,
                email,
                number,
                message,
                product,
                dataType: 'html'
            });
        }
    });
}); 

3 个答案:

答案 0 :(得分:1)

您只在数据字段中传递'name'。将ajax调用更改为:

$.ajax({
    type: "POST",
    url: 'process.php',
    data: {
        name : name,
        email : email,
        number : number,
        message : message,
        product : product
    },
    dataType: 'html'
});

答案 1 :(得分:0)

您的PHP页面有什么期望?对象或表单?

现在你实际上只是将名称设置为数据选项。

$.ajax({
                type: "POST",
                url: 'process.php',
                data: name,
                email, // Email will be treated as something else than data.
                number,
                message,
                product,
                dataType: 'html'
)};

将其作为对象传递:

$.ajax({
                type: "POST",
                url: 'process.php',
                data:
                    { name: name,
                      email: email, 
                      number: number,
                      message: message,
                      product: product 
                },
                dataType: 'html'
)};

或序列化表格:

$.ajax({
                type: "POST",
                url: 'process.php',
                data: $(form).serialize(),
                dataType: 'html'
)};

答案 2 :(得分:0)

尝试使用: -

<script type="text/javascript">

                      $(function() {

                        // Setup form validation on the #register-form element
                        $("#val2").validate({

                            // Specify the validation rules
                            rules: {
                                Name: {
                                    required:true,
                                    maxlength:50
                                },
                                telephone: {
                                    required:true,
                                    number:true
                                },
                                Email: {
                                    required: true,
                                    email: true
                                },
                                message: {
                                    required: true,
                                    minlength: 30
                                },
                            },

                            // Specify the validation error messages
                            messages: {
                                Name: {
                                    required:"Please enter your name",
                                    maxlength:"Please Keep your name under 50 characters"
                                },
                                telephone: {
                                    required:"Please enter your number",
                                    number:"Please enter a valid number"
                                },
                                message: {
                                    required: "Please provide a password",
                                    minlength: "Your message must be at least 30 characters long"
                                },
                                Email: "Please enter a valid email address",
                            },

                            submitHandler: function(form) {
                                debugger;
                                var name = form.Name.value;
                                var email= form.Email.value;
                                var number = form.telephone.value;
                                var message = form.message.value;
                                var product = form.product.value;

                                $.ajax({
                                  type: "POST",
                                  url: 'process.php',
                                  data: {name:name,email:email,number:number,message:message,product:product},
                                  dataType:'html'
                                });
                            }
                        });

                      });
                </script>