无法让jquery.multi .select.js验证并将选中的复选框传递给php

Cannot get jquery.multiple.select.js to validate and pass to php the boxes checked

本文关键字:复选框 php jquery multi select 验证 js      更新时间:2023-09-26

我使用jquery.multi .select.js v1.1.0,无法验证并将输出传递给我的php邮件。我的目标是为我的网站的访问者从多个选项请求报价选择。到目前为止,我能够收到所有其他字段验证的电子邮件,但在电子邮件中,我收到"quote for: array",但没有选择。同样相关的是,即使电子邮件发出,我也没有在页面上得到成功或错误信息。有人能给我指路吗?我将在这里包含有问题的页面,http://cptest.info/h2contact375.html

这是验证js

var Contact = {
initialized: false,
initialize: function() {
    if (this.initialized) return;
    this.initialized = true;
    this.build();
    this.events();
},
build: function() {
    this.validations();
},
events: function() {
},
validations: function() {
    $("#contactForm").validate({
        submitHandler: function(form) {
            $.ajax({
                type: "POST",
                url: "js/contact-form.php",
                data: {
                    "name": $("#contactForm #name").val(),
                    "email": $("#contactForm #email").val(),
                    "telephone": $("#contactForm #telephone").val(),
                    "message": $("#contactForm #message").val(),
                    "ms": $("#contactForm #ms").val()
                },
                dataType: "json",
                success: function (data) {
                    if (data.response == "success") {
                        $("#contactSuccess").removeClass("hidden");
                        $("#contactError").addClass("hidden");
                        $("#contactForm #name, #contactForm #email, #contactForm #telephone, #contactForm #message, #contactForm #ms")
                            .val("")
                            .blur()
                            .closest(".control-group")
                            .removeClass("success")
                            .removeClass("error");
                        if(($("#contactSuccess").position().top - 80) < $(window).scrollTop()){
                            $("html, body").animate({
                                 scrollTop: $("#contactSuccess").offset().top - 80
                            }, 300);                                
                        }
                    } else {
                        $("#contactError").removeClass("hidden");
                        $("#contactSuccess").addClass("hidden");
                        if(($("#contactError").position().top - 80) < $(window).scrollTop()){
                            $("html, body").animate({
                                 scrollTop: $("#contactError").offset().top - 80
                            }, 300);                                
                        }
                    }
                }
            });
        },
        rules: {
            name: {
                required: true
            },
            email: {
                required: true,
                email: true
            },
            telephone: {
                required: true
            },
            message: {
                required: true
            },
            ms:{
                required: true,
                message: 'Please select at least one'
            },
        },
        highlight: function (element) {
            $(element)
                .closest(".control-group")
                .removeClass("success")
                .addClass("error");
        },
        success: function (element) {
            $(element)
                .closest(".control-group")
                .removeClass("error")
                .addClass("success");
        }
    });
}
};
Contact.initialize();

这里是php

<?php 
// check if fields passed are empty 
 if(empty($_POST['name'])   ||    
empty($_POST['email'])  ||
empty($_POST['message'])||
empty($_POST['telephone'])||
empty($_POST['ms']) ||   
!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))    
  {     
   echo "No arguments Provided!";   return false;    
  } 
  $name = $_POST['name']; 
  $email_address = $_POST['email']; 
  $message = $_POST['message'];
  $telephone = $_POST['telephone'];
  $ms = $_POST['ms'];      
 // create email body and send it    
 $to = "myemail@gmail.com"; 
 // put your email 
 $email_subject = "Contact form submitted by:  $name"; $email_body = "You have received a new message. 'n'n".                 
               " Here are the details:'n 'nName: $name 'n ". 
               "Telephone: $telephone 'n" . 
               "Quote For: $ms 'n" .               
               "Email: $email_address'n Message: 'n $message"; 
 //$headers = "From: me@youremail.com'n"; 
 //$headers .= "Reply-To: $email_address";  

 mail($to,$email_subject,$email_body,$headers); return true;            
?>

name赋给您的字段ms,否则它将不会转到php,因为它标识的是名称而不是id,并且根据您的条件检查,它将始终显示No arguments提供!

<select id="ms" multiple="multiple" name="ms">

尝试使用$_REQUEST在您的PHP,如果$_POST不提供数据检查。

注意:没有添加样式,只是期望它向你的php发送数据,没有其他。

<?php 
// check if fields passed are empty 
print_r($_REQUEST);//it should display the send data
//rest of the code with request
演示

您可以使用以下代码进行验证,以便验证插件不会忽略隐藏的<select>元素。

奇迹发生在这一行:

ignore: ':hidden:not("#ms")',

添加到验证脚本

 //AjaxCall Code
 },
 ignore: ':hidden:not("#ms")',
 rules: {
 //Rules and messages onwards

以上将修复验证的问题,

关于不发布的数据,<select>标签中没有name="",所以添加一个,验证后,<select>值将与<form>中的其他输入一起发布

<select style="display: none;" name="ms" id="ms" multiple="multiple" >