0

I am trying to upload image to a location through ajax using codeigniter and how to receive in the controller.

I have search too much but don't know where is the problem!

Ajax code:

 $('#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");
});
 });

My form

<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>
2
  • YOU know what your problem is you don't know how to debug , first thing first what does the console say? Commented Mar 11, 2016 at 8:19
  • its shows no errors if i use alert its alert the message but ajax is not calling Commented Mar 11, 2016 at 8:30

2 Answers 2

2

Try this without the submit event:

 $("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);
 }
 });

});
Sign up to request clarification or add additional context in comments.

6 Comments

his form won't be submitted without the click event, it's just a button, not an input.
the button is named submit but doesn't have a type submit ,didn't see that
yeah i missed it too at first :)
kudos for FormData($(this).parent('form')[0]
Uncaught TypeError: Illegal invocation showing that error
|
0

This line is not correct syntax..

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

change to

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

you should also try this instead of false..

contentType: 'multipart/form-data',

and see if that works, if not, let me know and I'll see what else I can figure out.

2 Comments

if you have incorrect syntax your script won't run. just try it at least.
what is your script returning? json object?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.