【发布时间】:2015-02-01 01:26:03
【问题描述】:
我目前正在编写该脚本:
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<?php
$path="upload/";
$path=$path.$_FILES['file_upload']['name'];
if(isset($_POST['upload']))
{
if(move_uploaded_file($_FILES['file_upload']['tmp_name'],$path))
{
echo'The file '.basename($_FILES['file_upload']['name']).' has been uploaded';
}
else
{
echo"There is an error";
}
}
?>
<form action="#" method="post" enctype="multipart/form-data">
<table width="384" border="1" align="center">
<tr>
<td width="108">Select File</td>
<td width="260"><label>
<input type="file" name="file_upload">
</label></td>
</tr>
<tr>
<td><label>
<button id=clickToUpload type="submit" name="upload">Upload File</button>
</label></td>
<td> </td>
</tr>
</table>
</form>
</body>
</html>
它正在正常工作。
现在,我希望能够用 jQuery 做同样的事情...
<script src="js/jquery.js"></script>
<script>
$('#clickToUpload').click(function(event)
{
event.preventDefault();
$.ajax({
type: 'POST',
url: "post.php",
success: function(data)
{
}
});
});
</script>
这是我的 post.php 页面(与正在工作的 index.php 中的相同)
$path="upload/";
$path=$path.$_FILES['file_upload']['name'];
if(isset($_POST['upload']))
{
if(move_uploaded_file($_FILES['file_upload']['tmp_name'],$path))
{
echo'The file '.basename($_FILES['file_upload']['name']).' has been uploaded';
}
else
{
echo"There is an error";
}
}
但使用 ajax 似乎无法正常工作:(
这可能是什么原因?
【问题讨论】:
-
您的 AJAX 调用实际上并没有向服务器发送任何内容。
-
来自 firebug 控制台:
通知:未定义索引:C:\xampp\htdocs\post.php 中的 file_upload 上线 4
标签: javascript php ajax upload