【问题标题】:Post Data and Multiple Image using Ajax使用 Ajax 发布数据和多个图像
【发布时间】:2017-04-28 19:49:07
【问题描述】:

我有一个包含多个输入数据和 4 个输入图像的表单,我如何使用 Ajax 发布它们?这是我尝试过的一组代码...
HTML

<form action="<?php echo base_url('link')?>/to/controller" method="POST" id="frm-update" enctype="multipart/form-data">

<!-- Normal Post -->

  <input type="text" name="input1" id="input1" class="form-control">Normal Input1
  <input type="text" name="input2" id="input2" class="form-control">Normal Input2
  <input type="text" name="input3" id="input3" class="form-control">Normal Input3
  <input type="text" name="input4" id="input4" class="form-control">Normal Input4

<!-- Image Post -->

  <input type="file" name="image1" id="image1" class="form-control">Image 1
  <input type="file" name="image2" id="image2" class="form-control">Image 2
  <input type="file" name="image3" id="image3" class="form-control">Image 3
  <input type="file" name="image4" id="image4" class="form-control">Image 4

  <button type="submit" class="btn btn-success">Save

jQuery

<script>
$("#frm-update").validate({
  ignore: ':hidden:not(.chzn)',
    errorPlacement: function (error, element) {
        if (element.is(":hidden")) {
            element.next().parent().append(error);
        } else {
            error.insertAfter(element);
        }
    },

 submitHandler: function(form) {
     $.ajax({
      type: 'POST',
      url: $("#frm-update").attr('action'),
      data: $("#frm-update").serialize(),

</script>

我一直在想的是,如果我使用 new FormData(this),会保存我表单上的所有输入吗?还是只是图像?

【问题讨论】:

  • 这篇文章可能对您有帮助 jQuery Ajax Upload (请记住,对于 Ajax 上传 contentType: false, processData: false, 是强制性的)
  • 我已经尝试过了,但没有成功,因为我将
    设置为控制器...@OldPadawan

标签: javascript php jquery ajax


【解决方案1】:

使用FormData 而不是serialize()

$("#frm-update").validate({
    ignore: ':hidden:not(.chzn)',
    errorPlacement: function (error, element) {
        if (element.is(":hidden")) {
            element.next().parent().append(error);
        } else {
            error.insertAfter(element);
        }
    },

    submitHandler: function (form) {
        var data = new FormData(form);
        $.ajax({
            url: $(form).attr('action'),
            data: data,
            type: 'POST',
            contentType: false,
            processData: false,
            success: function (data) {
               // data passed from php
                console.log(data);
            }
        });
    }
});

然后在您的PHP 中,您可以执行以下操作

<?php

// get values of input through $_POST
// move uploaded files to a directory with move_uploaded_file() function

// get inputs using $_POST
$input1 = $_POST['input1'];
$input2 = $_POST['input2'];
$input3 = $_POST['input3'];
$input4 = $_POST['input4'];

// get files using $_FILES
$image1 = $_FILES['image1']['name'];
$image2 = $_FILES['image2']['name'];
$image3 = $_FILES['image3']['name'];
$image4 = $_FILES['image4']['name'];

// save values to the database
?>

【讨论】:

  • 代码运行良好,Jquery 运行良好,但图片无法发布,我尝试检查元素,内容类型设置为 text/html; charset=UTF-8,应该是multipart/form-data吧?
  • 哪个图片没有发布?
  • 所有这些...在数据库中,已设置 image1 - image4 字段..但文件不会上传到 /uploads...所以我尝试手动设置内容类型..它工作...
  • 所以图片被发送到PHP,但是你不能移动它们?
  • 可以,使用您的代码...但只能手动将内容类型更改为 multipart/formdata,谢谢.....
猜你喜欢
  • 2013-09-19
  • 2014-05-28
  • 1970-01-01
  • 1970-01-01
  • 2013-01-23
  • 1970-01-01
  • 2017-02-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多