【发布时间】:2019-02-19 13:26:52
【问题描述】:
我正在尝试将文件路径和其他参数上传到 mysql,我使用 Ajax 和 PHP 将文件保存到文件夹中失败,对于字符串参数没关系,但是将文件参数获取到数据库我得到空文件数据。
一个错误:Uncaught TypeError: Illegal invocation
HTML 表单:
................
<form role="form" action=""
method="post" autocomplete="off" class="form"
enctype="multipart/form-data" >
<div class="form-group">
<label class="" for="">Ministry in the
church(facultative)</label>
<input type="text" name="mn_church" placeholder="Facultative" class="form-control" id="mn_church">
</div>
</fieldset>
<!-- end -->
<!-- Attachements -->
<fieldset>
<h4>Document attachments:</h4><br>
<div class="form-group">
<label class="" for="">Last degree obtained</label>
<input type="file" name="ldgree" placeholder="Upload" class=" form-control" id="ldgree">
</div>
<button type="button" class="btn btn-previous">Previous</button>
<button type="button" onclick="insertDataThirdStep() " class="btn btn-next">Next</button>
</div>
</fieldset>
.............
PHP 代码:
------------------
$mn_church = trim($_POST['mn_church']);
$mn_church = strip_tags($mn_church);
$mn_church = htmlspecialchars($mn_church);
// upload file
$ldgree = $_FILES['ldgree']['name'];
$tmpName = $_FILES['ldgree']['tmp_name'];
// Rename image with a random number
$random_digit=rand(0000,9999);
$renamed_image=$random_digit.$ldgree;
//upload renamed image and image path to db variable
$filePath = $uploadDir . $renamed_image;
//upload renamed image and path image to the folder
$result = move_uploaded_file($tmpName, $filePath);
if(!get_magic_quotes_gpc())
{
// $fileName = addslashes($fileName);
// Add slashes between folder and image
$filePath = addslashes($filePath);
}
// end first file
//start student application by inserting the form's data to database
$query = "
UPDATE
aku_student_application
SET
mychurch ='$mn_church',
last_degree_optained='$filePath ',
"
-------------------
我曾经使用 php 使用 ajax 的 Jquery 代码以避免页面重新加载:
var mn_church=$("#mn_church").val();
// Attachment
var ldgree = $('#ldgree').prop('files')[0];
$.ajax({
url:'step-4-appli-form-attachments.php',
method:'POST',
data:{
mn_church:mn_church,
ldgree:ldgree
},
success:function(response){
// alert(response);
console.log('Success fourth step');
}
【问题讨论】:
-
您是否针对该错误查看了此内容? stackoverflow.com/questions/11071100/…
-
一个
update语句需要一个where子句-除非您希望更新所有记录...这真的是所有相关代码吗?? -
@IncredibleHat,是的
It say that error occurs when I try to passs an HTML element instead of its value,我需要知道如何将file元素从Ajax传递到PHP -
@RamRaider , 不管 where 子句因为问题不在于
-
看起来我们需要更多的 JS 块而不是您提供的短片。在其他新闻中,请查看“FormData”以设置包含文件的 ajax 数据发送。 (stackoverflow.com/questions/21044798/…)