weixin_33736048 2015-08-19 19:35 采纳率: 0%
浏览 32

使用ajax发送表单PHP

I have a form to upload files with 2 parameters (type = "file", type = "text") to pass through ajax in PHP. Looking at google I found the script you see below. The script works perfectly and also visualize the progress bar, but I can spend only what concerns the parameter type = "file" but I can add to what I write in the field type = "text".

<script>
function _(el){
    return document.getElementById(el);
}
function uploadFile(){
    var file = _("userfile").files[0];
    var formdata = new FormData();
    formdata.append("userfile", file);
    var ajax = new XMLHttpRequest();
    ajax.upload.addEventListener("progress", progressHandler, false);
    ajax.addEventListener("load", completeHandler, false);
    ajax.addEventListener("error", errorHandler, false);
    ajax.addEventListener("abort", abortHandler, false);
    ajax.open("POST", "TEST.php");
    ajax.send(formdata);
}
function progressHandler(event){
    _("loaded_n_total").innerHTML = "Uploaded "+event.loaded+" bytes of "+event.total;
    var percent = (event.loaded / event.total) * 100;
    _("progressBar").value = Math.round(percent);
    _("status").innerHTML = Math.round(percent)+"% uploaded... please wait";
}
function completeHandler(event){
    _("status").innerHTML = event.target.responseText;
    _("progressBar").value = 0;
    location.href="home.php";
}
function errorHandler(event){
    _("status").innerHTML = "Upload Failed";
}
function abortHandler(event){
    _("status").innerHTML = "Upload Aborted";
}
</script>

<form id="upload_form" enctype="multipart/form-data" method="post">
<input class="formUpDes" type="text" name="description" id="description">
<input class="formUpFile" type="file" name="userfile" id="userfile">
<input class="formUpBt" type="button" value="upload" onclick="uploadFile()">
<progress class="formUpProg" id="progressBar" value="0" max="100"></progress>
<h3 id="status"></h3>
<p id="loaded_n_total"></p>
</form>

I would just add the text that is written in the text field in the function UploadFile() so that it gets passed to the PHP file.

please help me

  • 写回答

1条回答 默认 最新

  • weixin_33720078 2015-08-19 19:49
    关注

    Add to uploadFile():

    formdata.append('description', _('description').value);
    
    评论

报告相同问题?

悬赏问题

  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂