Solved

How to send image and data using ajax

Posted on 2016-09-07
6
77 Views
1 Endorsement
Last Modified: 2016-09-27
I want to change profile information. There are 4 input box and 2 input file type.
i can't passing input box value and input file type image using Ajax,
1
Comment
Question by:Pravin Bnakar
  • 2
  • 2
6 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 41787615
What is the question?  Do you need to have an example of AJAX so you can build upon it for your app?  Or have you got something that is giving you an error?
0
 

Author Comment

by:Pravin Bnakar
ID: 41787627
I want post image and other data like user name via jquery AJAX to php server .
Here is My form
<form id="datafiles" method="post" enctype="multipart/form-data">
    <input type="text" name="first" value="Bob" />
    <input type="text" name="middle" value="James" />
    <input type="text" name="last" value="Smith" />
    <input name="image" type="file" />
    <button type="button" class="send_data">Submit</button>
</form>
0
 
LVL 52

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 41787707
This code should do it
    <form>
      Your Name: <input type="text" name="name" value="fred" readonly/> <br/>
      File #1 <input type="file" name="file1" /> <br/>
      File #2 <input type="file" name="file2" /> <br/>
      <input type="submit" />
    </form>
    <div id="result"></div>

Open in new window

jQuery
$(function() {
  $('form').submit(function() {
    var formData = new FormData($(this)[0]);
    $.ajax({
      url: 'reflect.php',
      type: 'POST',
      data: formData,
      cache: false,
      contentType: false,
      processData: false
    }).then(function(data) {
      $('#result').html(data);
    });    
    return false;
  });
});

Open in new window

Working sample here
0
 

Author Comment

by:Pravin Bnakar
ID: 41787714
What does below line indicate :
var formData = new FormData($(this)[0]);
0
 
LVL 52

Expert Comment

by:Julian Hansen
ID: 41787752
FormData gets all the data from a Form and makes it available for use in an AJAX request. You can read more about it here
https://developer.mozilla.org/en/docs/Web/API/FormData
EDIT
This differs from the jQuery serialize() method which does not serialize file data. FormData will serialize any files included in the form.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now