简体   繁体   中英

Submitting file with jquery / ajax / php

I'm having a problem with my ajax file for submission. There doesn't appear to be anything submitted (neither variables nor the files) to the receiving php file.

This is my form:

<form id="ProtocolForm" enctype="multipart/form-data" method="post">
   <input name="ProtocolName" type="hidden" value="sdfsdf">
   <input name="ProtocolType" type="hidden" value="Image">        
   <input name="name_major" id="name_major" type="text">
   <input name="name_minor" id="name_minor" type="text">
   <input name="creator" id="creator" type="text">
   <input name="File" class="File" id="File" type="file">
   <input id="Submit" type="submit" value="Save Protocol">
</form>

This is my js code:

 $(document).on( "submit", "#ProtocolForm", function( event ) {
        event.preventDefault();

        var form = $('form')[0];  
        var formData = new FormData(form)

          $.ajax({
            url: "_save_protocol.php",
            type: "POST",
            data:  formData,
            contentType: false,
            cache: false,
            processData:false,
            beforeSend : function()
            {
                alert('before send:' + $('form').serialize());
            },
            success: function(data)
            {
                if(data=='invalid')
                {
                    alert('Invalid file');
                }
                else
                {                         
                    alert('Response:' + data);                     
                }
            },
            error: function(e) 
            {
                alert('Error '+ e);

            }          
            });


    });

When the form is submitted the alert I can see all the variables serialized in the beforeSend function. However when I try to access the variables in the php file they are all blank. $_POST is blank as is $_FILES. What am I missing? Any help would be greatly appreciated. Thanks!

Solved the problem; The issue was not the ajax but rather the server side of things. php.ini max_post_size was not set appropriately and the server was not handling the post requests properly as a result. It's fixed and working now.

Reading some of the other questions around the topic on this site makes me think that this is a rather common problem - especially to those that are on a shared hosting service like myself. Hope this helps others.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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