I have followin pages
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"> </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
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
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".
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.