【问题标题】:How to post a file and form data from a Bootstrap Modal如何从引导模式发布文件和表单数据
【发布时间】:2018-07-23 00:15:44
【问题描述】:

我在从引导模式发布文件和表单数据时遇到问题。

我有代码可以从中发布文件附件,但其他字段我无法通过

我的 Javascript 在下面

$(document).ready(function(){
  $('#upload').click(function(){

    var fd = new FormData();
    var files = $('#file')[0].files[0];
    fd.append('file',files);

    // AJAX request
    $.ajax({
      url: 'clienttest.php',
      type: 'post',
      data: fd,
      contentType: false,
      processData: false,
      success: function(response){
        if(response != 0){
          // Show image preview
          $('#preview').html(" Process Started");
        }else{
          alert('file not uploaded');
        }
      }
    });
  });
});

我的表单数据如下

                <div class="modal fade" id="myModal1" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                    <div class="modal-dialog">
                        <div class="modal-content">
                            <div class="modal-header">
                                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                                <h4 class="modal-title" id="myModalLabel">Load Secure Document</h4>
                            </div>
                            <div class="modal-body">
                            <form method='post' action='' enctype="multipart/form-data">
                            <label for="nametag">Name</label>
                            <input type="text" name="nametag" size="20" />

                            Select file : <input type='file' name='file' id='file' class='form-control' ><br>
                            <input type='button' class='btn btn-info' value='Upload' id='upload'>
                            </form>

                            <!-- Preview-->
                            <div id='preview'></div>
                            </div>
                            <div class="modal-body3">
                            </div>
                            <div class="modal-footer">
                                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                            </div>
                        </div>
                    </div>
                </div>

最后我有了接收数据的文件,保存文件并将人名通过电子邮件发送给我以检查

// file name
$filename = $_FILES['file']['name'];

// Location
$location = $filename;

// file extension
$file_extension = pathinfo($location, PATHINFO_EXTENSION);
$file_extension = strtolower($file_extension);

// Valid image extensions
$image_ext = array("jpg","png","jpeg","gif","pdf");

$response = 0;
if(in_array($file_extension,$image_ext)){
  // Upload file
  if(move_uploaded_file($_FILES['file']['tmp_name'],$location)){
    $response = $location;
  }
}

mail ('email@email.com','email of name',"name is ".$_POST['nametag']);

echo $response;

我确定 Javascript 函数中有一些东西需要添加,很可能是在标记数据的行上,但我在互联网上找不到它是什么。有人可以帮忙吗。

【问题讨论】:

  • 我想了解您的问题是关于文件输入以外的字段未通过?
  • 没错。文件通过但其他字段不通过

标签: javascript ajax forms bootstrap-modal


【解决方案1】:

其他字段未传递,因为您没有将它们附加到FormData,更好的方法是在初始化FormData() 时使用表单对象,而不是手动附加所有表单输入,见下文,

注意:使用前在表单中添加id="my-form"属性

$(document).ready(function(){
  $('#upload').click(function(){

    event.preventDefault();
     var form = $("#my-form")[0];
     var data = new FormData(form);
    // AJAX request
    $.ajax({
      url: 'clienttest.php',
      type: 'post',
      data: data,
      contentType: false,
      processData: false,
      success: function(response){
        if(response != 0){
          // Show image preview
          $('#preview').html(" Process Started");
        }else{
          alert('file not uploaded');
        }
      }
    });
  });
});

【讨论】:

  • 你太棒了。非常感谢你
  • :) 乐于帮助@RenegadeRob
  • 这是我需要的,谢谢..
猜你喜欢
  • 2016-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-25
  • 2021-03-30
  • 1970-01-01
  • 2016-12-29
相关资源
最近更新 更多