使用AJAX值的Fileupload不会以其他字段的形式传递
16lz
2021-01-22
I am using AJAX file upload,it should be working fine.i have two form fields firstname and file upload.from this code file upload value i can get but first name value not getting in next page(upload.php) while using AJAX...
我正在使用AJAX文件上传,它应该工作正常。我有两个表单字段firstname和文件upload.from这个代码文件上传值我可以得到但名字值没有进入下一页(upload.php)使用AJAX。 ..
<form class="form-horizontal form-bordered" method="POST" id="newUserForm" enctype="multipart/form-data">
<div class="form-group">
<label class="col-md-3 control-label">First Name<span class="star_mark"> *</span></label>
<div class="col-sm-6">
<input type="text" class="form-control" id="fname" name="fname" value="" aria-required="true" required="" data-msg-required="Please enter your firstname" placeholder="Enter your firstname">
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">Photo Upload<span class="star_mark"> *</span></label>
<div class="col-md-6">
<div class="fileupload fileupload-new" data-provides="fileupload">
<div class="input-append">
<div class="uneditable-input">
<i class="fa fa-file fileupload-exists"></i>
<span class="fileupload-preview"></span>
</div>
<span class="btn btn-default btn-file">
<span class="fileupload-exists">Change</span>
<span class="fileupload-new">Select file</span>
<input type="file" id="file" name="file" value="" aria-required="true" required="" data-msg-required="Please select your file">
</span>
<a href="#" class="btn btn-default fileupload-exists" data-dismiss="fileupload">Remove</a>
</div>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-3 col-sm-6">
<button class="btn btn-info btn-block" type="submit" id="user-submit">Submit</button>
</div>
</div>
</form>
</div>
</div>
<script type="text/javascript">
$(document).ready(function(){
$("#user-submit").click(function(event){
event.preventDefault();
if($("form#newUserForm").valid()){
// var formData = new FormData($(this)[0]);
var formData = new FormData();
formData.append('file', $('input[type=file]')[0].files[0]);
$.ajax({
url: 'upload.php',
type: 'POST',
data: formData,
async: false,
cache: false,
contentType: false,
processData: false,
success: function(data){
alert(data)
},
});
return false;
}else{
console.log("false");
}
});
});
</script>
upload.php
upload.php的
<?php
$fstname = $_POST['fname'];//here i can't get the first name value
if ( 0 < $_FILES['file']['error'] ) {
echo 'Error: ' . $_FILES['file']['error'] . '<br>';
}
else {
if( move_uploaded_file($_FILES['file']['tmp_name'], 'img/' . $_FILES['file']['name'])){
echo "upload success";
}else{
echo "upload Error";
}
}
3 个解决方案
#1
0
That because your formData
contain just the file
field added in :
那是因为你的formData只包含添加的文件字段:
formData.append('file', $('input[type=file]')[0].files[0]);
You could add fname
field using :
您可以使用以下命令添加fname字段:
formData.append("fname", $('#fname').val());
Then it will be sent and accessible from php page using $_POST['fname'];
.
然后它将使用$ _POST ['fname'];从php页面发送和访问。
Or you could add all the form fields to the formData
:
或者您可以将所有表单字段添加到formData:
var formData = new FormData( $("form#newUserForm") );
Hope this helps.
希望这可以帮助。
更多相关文章
- Mysql 批量修改字段存储过程
- mysql中select列表可以有group列表中没有的字段,大家不觉得奇怪吗
- 如何将空值传递给外键字段?
- MYSQL SET类型字段的SQL查询某个字段保函某个值的查询
- mysql 排序两个字段/列表先根据时间升序排序,时间相同再根据商家I
- 转!mysql 查询 distinct多个字段 注意!!
- 基础概念---mysql 列转行,合并字段
- mysql 怎么给一个表一次增加多个字段, mysql 添加 多个 字段
- MySQL中find_in_set的用法(某个字段包含某个字符)