Send form EXCEL file without refresh

0

Hello, I would like to send an excel file to a php page, where I would read the data and then send it to the database. I was able to do this by giving a refresh of the page, but wanted that when the file was sent there would appear a message of success (alert), without refresh.

<form action="uploadexcel.php" enctype="multipart/form-data" method="POST" >

                              <input type="file"  name="file" >

                             <input type= "submit" value ="Enviar" >

                            </form> 

I tried to make a modification to use jquery, but I do not have much knowledge and it did not work. This is the code, it does not refresh but does not send the file in a format suitable for php.

<script type="text/javascript">
jQuery(document).ready(function(){
    jQuery('#ajax_form').submit(function(){
        var formdata = new FormData($("#ajax_form"));

        jQuery.ajax({
            type: "POST",
            url: "uploadexcel.php",
            data: formdata,
            processData: false,
            contentType: false
            success: function( data )
            {
                alert( data );
            }
        });

        return false;
    });
});
</script>             
    
asked by anonymous 13.11.2016 / 19:33

1 answer

0

Try this:

<form id="excelForm" enctype="multipart/form-data">
  <input type="file"  name="file" />
  <input type= "submit" value ="Enviar"/>
</form>

<script type="text/javascript">
  jQuery(document).ready(function(){
    jQuery('#excelForm').submit(function(event){
      event.preventDefault();
      var formdata = new FormData($("#ajax_form"));

      jQuery.ajax({
        type: "POST",
        url: "uploadexcel.php",
        data: formdata,
        processData: false,
        contentType: false
        success: function( data ) {
          alert( data );
        }
      });
    });
  });
</script>

This question is the same as I answered this week, a look at my answer to this question: #

    
13.11.2016 / 20:06