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>

7
  • Here is the HTML Part Sorry Commented Jun 19, 2018 at 14:01
  • which side is not working the server or ? Commented Jun 19, 2018 at 14:02
  • Maybe it's data formatting. Try this new FormData($('#import_form')[0]). And maybe duplicate of stackoverflow.com/questions/166221/… Commented Jun 19, 2018 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. Commented Jun 19, 2018 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 Commented Jun 19, 2018 at 14:09

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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".

2 Comments

Sorry yes. spelling is correct. i typed incorrect when doing the post. I do notreceive any errors. nothing happen at all
Also please revise the function being called on success. Look at the link below. stackoverflow.com/questions/12851839/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.