Send EXCEL form file without refresh

Asked

Viewed 127 times

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 managed to do this by refreshing the page, but I wanted when the file was sent to appear a successful sending message (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 don’t know much and it didn’t work. This was the code, it does not refresh but does not send the file in proper format to 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>             

1 answer

0

Try it like 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>

That question of yours is the same as the one I answered this week, from a glance at my answer to that question: .load Jquery letting the form be submitted

  • Hello, I tried this way but it didn’t work. I don’t know if this changes anything, but my php reads the excel file and sends it to mysql. if (!empty($_FILES['file']['name'])) {&#xA; ... however I put an error msg in case it was not excel file or this error and msg does not appear. Therefore it is not calling my php for what I understood.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.