我有以下页面
- index.php
- 导入数据.php
我正在尝试使用ajax将csv文件导入MySql 我在网上使用了一些示例,但仍然无法工作 索引.php
开头的脚本文件:
$(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");
});
});
});
脚本后的 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"> </td>
</tr>
</table>
</body>
最佳答案
我现在设法获取发布的信息,执行以下操作
$(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");
}
});
});
});
现在在 php 表单上我收到以下错误 未定义索引:importData.php 第 6 行中的 Score_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>
和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)){
错误在第二行
关于php - 使用 Ajax 和 PHP 导入 csv 将 csv 导入到 mysql,而不是上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50930349/