简体   繁体   English

使用带有输入类型按钮的ajax提交表单输入文件类型时遇到困难

[英]having difficulty in submitting form input file types using ajax with input type button

I have two forms on one page. 我在一页上有两种形式。 I am using input type button and javascript function to submit the form. 我正在使用输入类型按钮和javascript函数提交表单。 First form is being submitted correctly as per requirements. 根据要求正确提交了第一份表格。 However, second form was not. 但是,第二种形式不是。 So I used ajax code to submit that form. 因此,我使用了ajax代码来提交该表单。 As I am new to ajax so I copied the code. 由于我是ajax的新手,所以我复制了代码。 That code worked having some issue. 该代码存在一些问题。 Issue is if I post text then this works great but if I post photo or video means input type file then its submitting the form but not posting that file type data. 问题是,如果我发布文本,那么效果很好,但是如果我发布照片或视频意味着输入类型文件,则它提交表单但不发布该文件类型数据。 Here is my code please help me to solve this problem 这是我的代码,请帮助我解决此问题

<script>
    function makeAjaxCall(){
        $.ajax({
            type: "post",
            url: "<?php echo basic_url('time_line/create'); ?>",
            cache: false,               
            data: $('#add_data').serialize(),
            success: function(json){
            location.reload();                  
           try{     
            var obj = jQuery.parseJSON(json);
            alert( obj['STATUS']);
    }catch(e) {     
            //alert('Exception while request..');
        }       
        },
        error: function(){                      
            alert('Error while request..');
        }
 });
}
</script>


here is my form 这是我的表格

<form action="<?php echo basic_url('time_line/create'); ?>" method="post" name="add_data" id="add_data" enctype="multipart/form-data">

                 <span class="btn btn-success btn-file-image_upload" style="margin-left:0;" id="cap_photo">
                 <i class="fa fa-image">Add Photo </i><input type="file" name="photo" id="photo" accept="image/*" onchange="readURL1(event)">
                       </span>
                       <span class="btn btn-success btn-file-video_upload" id="cap_video">
                              <i class="fa fa-video-camera">Add Video </i><input type="file" name="video" id="video" onchange="readURL2(event)">
                       </span>
                  </div>
                </div>
                 <textarea rows="2" class="form-control"  style="margin-top:15px;" placeholder="Whats in your mind today?" name="description" id="description"></textarea>
                </div><!--#caption-->
               <div class="panel-footer">
             <input type="button" class="btn btn-success pull-right" value="Post" id="post" onClick="javascript:makeAjaxCall();"/>
            <input type="button" class="btn btn-danger pull-right" id="abort" value="Cancel" onclick="reloadpg()" style="margin-right:2%;"/>
                      <ul class="nav nav-pills">   
                      </ul>
                </div>
               </form><br/>

user can add 2 or anyone of the following fields 用户可以添加2个或以下任意字段

Try this to post file using ajax: 尝试使用ajax发布文件:

$('#upload').on('click', function() {
        var file_data = $('#pic').prop('files')[0];
        var form_data = new FormData();
        form_data.append('file', file_data);

        $.ajax({
                url         : 'upload.php',     // point to server-side PHP script 
                dataType    : 'text',           // what to expect back from the PHP script, if anything
                cache       : false,
                contentType : false,
                processData : false,
                data        : form_data,                         
                type        : 'post',
                success     : function(output){
                    alert(output);              // display response from the PHP script, if any
                }
         });
         $('#pic').val('');                     /* Clear the file container */
    });

I think it should be help to you. 我认为这应该对您有帮助。

$('#upload').on('click', function() {
        var file_data = $('#pic').prop('files')[0];
        var form_data = $('#add_data').serialize(); // fetching form data
        form_data.append('file', file_data); // adding file

        $.ajax({
                url         : 'upload.php',     
                dataType    : 'json',           
                cache       : false,
                data        : form_data,                         
                type        : 'post',
                success     : function(response){
                    alert(JSON.stringify(response));           
                }
         });
         $('#pic').val('');                     /* Clear the file container */
    });

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

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