简体   繁体   English

使用jQuery验证插件,输入类型=文件未传递到PHP页面

[英]Input type=file is not passing to php page using Jquery Validation Plugin

I have a html form with file upload functionality which I want to validate using jquery plugin. 我有一个带有文件上传功能的html表单,我想使用jquery插件进行验证。 Now if the file is empty then it's showing an error message, but if the file is different extension then it should show the message "File type is not allowed" but it seems it's not passed to the file filecheck.php page which would validate the file type. 现在,如果文件为空,则显示错误消息,但是如果文件的扩展名不同,则应显示消息“不允许文件类型”,但似乎没有传递到文件filecheck.php页面,该页面将验证文件类型。

How can I validate this file type using this jquery plugin ? 如何使用此jquery插件验证此文件类型? Can you help me plz ? 你能帮我一下吗? Thanks. 谢谢。

Html part: HTML部分:

<div id="result"></div> <!--error or success message will be show here-->

<form action="editContactDetails.php" method="post" enctype="multipart/form-data" id="all_contact_details">
  <tr>
    <td></td>  
    <td align="left"><input name="file1" type="file" id="file" class="file"/></td>
    <td></td>
  </tr>
</form>

Jquery part: jQuery部分:

$(document).ready(function() {  
    // validate signup form on keyup and submit
    $("#all_contact_details").validate({
    submitHandler: function(form) {
        $.ajax({
            url: form.action,
            type: form.method,
            //async: false,
            data: $(form).serialize(),
            beforeSend : function (){
              $('input[type=submit]').attr('disabled', false); 
            },
            success: function(response) {
            $('#result').html(response);
            //$(form).find('div').hide();
            $(form).hide();

            }            
        });
    },

        rules: {            
            file1: {
                    required: true,                    
                    remote: {
                        url: "filecheck.php",
                        type: "post"
                     }
                },

        },
        messages: {         
            file1: {
                    required: "Upload your file",                    
                    remote: "File is not allowed"
                },          
        }
    });

});

Php part (filecheck.php) PHP部分(filecheck.php)

<?php
$file1 =  $_FILES['file1']['name']; 
$allowedExts = array("jpg");    
$temp = stripslashes($file1);
$temp = explode(".", $temp);
$extension = end($temp);    

if(!in_array($extension, $allowedExts))
    echo 'false';
else
    echo 'true';        
?>

The SubmitHandler handles the actual submit when the form is valid : 当表单有效时, SubmitHandler 处理实际的提交
http://jqueryvalidation.org/validate/#submithandler http://jqueryvalidation.org/validate/#submithandler

So this won't validate the file extension itself. 因此,这不会验证文件扩展名本身。

You can however use the extension validator built into jqueryvalidation which will do exactly what you are trying to achive. 但是,您可以使用内置于jqueryvalidation的extension验证器,该extension验证器将完全执行您要达到的目标。 See this link for reference and for examples: 请参阅此链接以获取参考和示例:

http://jqueryvalidation.org/extension-method/ http://jqueryvalidation.org/extension-method/

Before trying any harder, it is not supported by all browsers to submit a file with AJAX. 在尝试更多之前,并非所有浏览器都支持使用AJAX提交文件。 You can however do a trick which is actually submitting the form to a target iframe and then using javascript to read the contents of that iframe and show/use them somehow. 不过,您可以采取一些技巧,实际上是将表单提交到目标iframe,然后使用javascript读取该iframe的内容并以某种方式显示/使用它们。 This way you actually submitted the form without refreshing the main window. 这样,您实际上提交了表单而不刷新主窗口。

This article quite covers it: Ajax: a simplified version of file upload form using IFRAME 本文对此进行了详细介绍: Ajax:使用IFRAME的文件上传表单的简化版本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM