Solved

How to send image and data using ajax

Posted on 2016-09-07
6
108 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
6 Comments
 
LVL 110

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 56

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 56

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how can i break an array into a couple 5 24
Echo'd values in dropdowns 6 29
Read text on Table 7 27
CSRF session and form tokens never match when using php/AJAX 4 28
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

756 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