0

I have followin pages

  • index.php
  • importdata.php

Im trying to use ajax importing a csv file into MySql I used dsome exapmples on the net but still wont work index.php

the Script file in the beginning:

$(document).ready(function(){

    $("#but_import").click(function() {
        var formData = new FormData($("#import_form"));     
            $.ajax({
                url:"importData.php",
                data:formData,
                type:'POST',
                success:function(response) 
                {
                    var resp = $.trim(response);
                    $("#output").html(resp);
                }else[

                    $("#output").html("Error");

                });

    });
   }); 

The HTML after the script:

<html>
<head>
    <title>Import CSV file data to the MySQL using PHP</title>

</head>
<body>
 <form id="import_form" method="post" action="" enctype="multipart/form-data" >
        <table width="100%">

            <tr>
                <td style="width: 58px">
                    <input type='file' name="score_file" id="score_file">
                </td>

            </tr>
            <tr>
                <td colspan="2" >
                <input type="button" id="but_import" name="but_import" value="Import File"></td>
            </tr>

        </table>
    </form>


<table style="width: 100%">
    <tr>
        <td id="output">&nbsp;</td>
    </tr>
</table>
</body>

The Wizard
  • 63
  • 9
  • Here is the HTML Part Sorry – The Wizard Jun 19 '18 at 14:01
  • which side is not working the server or ? – Masivuye Cokile Jun 19 '18 at 14:02
  • Maybe it's data formatting. Try this `new FormData($('#import_form')[0])`. And maybe duplicate of https://stackoverflow.com/questions/166221/how-can-i-upload-files-asynchronously – executable Jun 19 '18 at 14:04
  • Im have trouble passing the form infromation to the importData.php form.Not 100% sure if the ajax format is correctr. im a noob to this. – The Wizard Jun 19 '18 at 14:06
  • I had a look at the post. Im actully trying to import data from csv file to mysql using the ajax call. I dont want to refresh the page. I previously manage to get queries back using the format. but now stuck on passing the filename to import from index.php to the importdata.php using the ajax call – The Wizard Jun 19 '18 at 14:09
  • Possible duplicate of [How can I upload files asynchronously?](https://stackoverflow.com/questions/166221/how-can-i-upload-files-asynchronously) – Masivuye Cokile Jun 19 '18 at 14:10
  • Your curly braces and brackets are wildly wrong, it seems. Try fixing those. – Michael Conard Jun 19 '18 at 14:13

2 Answers2

1

I manage to get information posted now doing the following

$(document).ready(function() {

$("#but_import").click(function() {
    var formData = new FormData($('score_file')[0]);        



    alert(formData);    


        $.ajax({
             url: 'importData.php',
               data: formData,
              type: 'POST',
              contentType: false, 
                 processData: false,                
                 success:function(response) {
                        var resp = $.trim(response);
                        $("#message").html("yes");

                        }
        });

});

});

Bit now on the php form Im receiving following error Undefined index: score_file in importData.php on line 6

This is the inputbox in html file

<form id="import_form" name ="import_form" method="post" action="post" enctype="multipart/form-data" >
        <table width="100%">

            <tr>
                <td style="width: 58px">
                    <input type='file' name="score_file" id="score_file">
                </td>

            </tr>
            <tr>
                <td colspan="2" >
                <input type="button" id="but_import" name="but_import" value="Import File"></td>
            </tr>

        </table>
    </form>

and the first 2 line of php file

 $csvMimes = array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel', 'text/plain');
if(!empty($_FILES['score_file']['name']) && in_array($_FILES['score_file']['type'],$csvMimes)){

The error is on the 2nd line

The Wizard
  • 63
  • 9
0

What error does it put out.

Also look at the spelling of "importdata.php".

In the ajax you have "url:"importData.php"" but at the start you said you have "importdata.php".

Codebender
  • 195
  • 1
  • 10
  • Sorry yes. spelling is correct. i typed incorrect when doing the post. I do notreceive any errors. nothing happen at all – The Wizard Jun 19 '18 at 15:10
  • Also please revise the function being called on success. Look at the link below. https://stackoverflow.com/questions/12851839/add-if-else-function-in-ajax-jquery-function – Codebender Jun 19 '18 at 15:24