【问题标题】:codeigniter uploading image through ajaxcodeigniter通过ajax上传图片
【发布时间】:2016-03-11 17:37:05
【问题描述】:

我正在尝试使用 codeigniter 以及如何在控制器中接收通过 ajax 将图像上传到某个位置。

我搜索太多但不知道问题出在哪里!

Ajax 代码:

 $('#btnform2').click(function(e){
        e.preventDefault();
 // valadation_form();
 $("form#data").submit(function(event){

//disable the default form submission
 event.preventDefault();

  //grab all form data  
var formData = new FormData($(this)[0]);

 $.ajax({
  url: '<?php echo base_url(); ?>"+"index.php/club/post_ajax',
  type: 'POST',
   data: formData,
  async: false,
  cache: false,
  contentType: false,
  processData: false,
  success: function (returndata) {
  alert(returndata);
 }
 });

 return false;
 alert("test");
});
 });

我的表单

<form method="post" class="form-horizontal" id="form_members" role="form" name="uploader">
<label for="fullname" class="col-sm-2">Full Name</label>
<input type="text" class="form-control" name="fullname" id="firstname" placeholder="Full Name" >
<label for="lastname" class="col-sm-2">Computer Number</label>
<input type="text" class="form-control" name="computernumber" id="lastname" placeholder="Computer Number">
<label for="address" class="col-sm-2">E- mail Address</label>
<input type="email" class="form-control" name="email" id="address" placeholder="Email Address">
<label for="city" class="col-sm-2">Mobile Number</label>
<input type="text" class="form-control" list="cities" name="mobilenumber" id="mobilenumber" placeholder="Mobile Number">
<label for="phone" class="col-sm-2">Nationality</label>
 <input type="tel" class="form-control" name="Nationality" id="nationality" placeholder="nationality">
<label for="email" class="col-sm-2">Upload Photo</label>
<input id="file" class="file" type="file" multiple data-min-file-count="1">
<button name="submit" class="btn btn-warning" id="btnform2">Update Player Profile</button>
</form>

【问题讨论】:

  • 你知道你的问题是你不知道如何调试,首先控制台说什么?
  • 如果我使用 alert 它不会显示错误,它会提醒消息但 ajax 没有调用

标签: php jquery html ajax codeigniter


【解决方案1】:

在没有提交事件的情况下试试这个:

 $("form#form_members button").click(function(event){

//disable the default form submission
 event.preventDefault();

  //grab all form data  
var formData = new FormData($(this).parent('form')[0]);

 $.ajax({
  url: '<?php echo base_url(); ?>/index.php/club/post_ajax',
  type: 'POST',
   data: formData,
  success: function (returndata) {
  alert(returndata);
 }
 });

});

【讨论】:

  • 他的表单在没有点击事件的情况下不会被提交,它只是一个按钮,而不是一个输入。
  • 按钮名为 submit 但没有类型 submit ,没看到
  • 是的,一开始我也错过了 :)
  • FormData($(this).parent('form')[0]点赞
  • 未捕获的类型错误:非法调用显示该错误
【解决方案2】:

这行语法不正确..

url: '<?php echo base_url(); ?>"+"index.php/club/post_ajax',

改成

url: '<?php echo base_url(); ?>'+'index.php/club/post_ajax',

你也应该试试这个而不是假的..

contentType: 'multipart/form-data',

看看这是否有效,如果没有,请告诉我,我会看看我还能找出什么。

【讨论】:

  • 如果语法不正确,您的脚本将无法运行。至少试试吧。
  • 你的脚本返回了什么? json 对象?
猜你喜欢
  • 2012-05-29
  • 2013-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-31
  • 1970-01-01
相关资源
最近更新 更多