简体   繁体   English

使用Ajax和PHP导入csv将csv导入mysql,而不是上传文件

[英]importing csv using Ajax and PHP import csv to mysql, not uploading a file

I have followin pages 我有后续页面

  • index.php 的index.php
  • importdata.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 我试图使用ajax将csv文件导入MySql,但我在网上使用了dsome示例,但仍无法正常工作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:

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

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 现在在php表单上的位Im收到以下错误未定义的索引:在第6行的importData.php中的score_file

This is the inputbox in html file 这是HTML文件中的输入框

<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 和PHP文件的前2行

 $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 错误在第二行

What error does it put out. 它发出什么错误。

Also look at the spelling of "importdata.php". 还要看一下“ importdata.php”的拼写。

In the ajax you have "url:"importData.php"" but at the start you said you have "importdata.php". 在ajax中,您有“ url:” importData.php“”,但一开始您说您有“ importdata.php”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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