doujiena0025 2015-04-28 17:29
浏览 36

JQuery Ajax - 文件和数据上传

I am trying to create a form which uploads data and an image in the same submission. I've look all over and I can only seem to find people that are having trouble to upload files alone with JQuery. If I make content-type false then the data dosen't go through only the file. Here is the code:

<form action="/secret/includes/add_listing.php" id="listingfrm" name="listingfrm" action="javascript:;" enctype="multipart/form-data" method="post" accept-charset="utf-8">
  <div>
    <label for="title">Title</label>
    <div class="right">
      <div class="row">
        <div class="col-cst-1">
          <input type="text" name="title">
        </div>
      </div>
    </div>
  </div>
  <div>
    <label for="content">Description</label>
    <div class="right">
      <div class="row">
        <div class="col-cst-1">
          <textarea name="content" rows="5"></textarea>
        </div>
      </div>
    </div>
  </div>
  <div>
    <label for="image">Upload a Thumbnail</label>
    <!-- Browse to Upload Image (No more than 1MB) -->
    <div class="right">
      <div class="row">
        <div class="col-cst-1">
          <input type="file" name="listing_thumb" id="listing_thumb">
        </div>
      </div>
    </div>
  </div>
  <input type="submit" value="Submit">
</form>

function ajax(url, type, data) {
  var request
  if (type != "none") {
    request = $.ajax({
      url: url,
      type: type,
      data: data,
    });
  } else {
    request = $.ajax({
      url: url
    });
  }
  return request;
}
$(document).ready(function() {
  $(document).on("submit", "#listingfrm", function(event) {
    event.preventDefault();
    var form = $(this);
    var inputs = GetInputs(form);

    var serializedForm = form.serialize();
    inputs.prop("disabled", true);
    var request = ajax("includes/add_post.php", "post", serializedForm)
    request.done(function(response, textStatus, jqXHR) {
      console.log(response)
    })
    request.fail = ajax(function(jqXHR, textStatus, errorThrown) {
      console.error("Login Error: " + textStatus + " : " + errorThrown)
    })
    request.always(function() {
      inputs.prop("disabled", false);
    })
  });
});

When using this the title and the description don't go through to the PHP file however if I don't then the image dosen't.

</div>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
    • ¥20 软件测试决策法疑问求解答
    • ¥15 win11 23H2删除推荐的项目,支持注册表等
    • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
    • ¥15 qt6.6.3 基于百度云的语音识别 不会改
    • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
    • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
    • ¥15 lingo18勾选global solver求解使用的算法
    • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
    • ¥20 测距传感器数据手册i2c