Solved

How to send image and data using ajax

Posted on 2016-09-07
6
59 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 51

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 51

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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.

759 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

19 Experts available now in Live!

Get 1:1 Help Now