1

I am trying to upload firstname, lastname, description and image path to MySql database. And move uploaded image to specific folder.

Here is my ajax function

formData = new FormData(addPeopleForm);
var file_data = $('input[type="file"]')[0].file;
formData.append("file", file_data);

$.ajax({
  type: "POST",
  url: "functions.php",
  contentType: false,
  cache: false,
  processData: false,
  data: {
    function: "savepeople",
    data: formData
  }, success: function(data){
      console.log(data);
      getPeople();
  }

});

functions.php

if(isset($_POST['function'])){
 $f = $_POST['function'];

 if($f == "savepeople"){
  require_once("config.php");   
  echo $_POST['firstname'];
  .
  .
  .
3
  • 1
    have you try to append function variable in formData ? Commented Jun 9, 2017 at 9:45
  • I just did that and everything worked out fine :) Thanks anyway :) Commented Jun 9, 2017 at 10:38
  • happy to help :) Commented Jun 9, 2017 at 10:45

3 Answers 3

1

you can not send directly image to php file with ajax call, you have to take form enctype="multipart/form-data" while form defination

and replace this code for file upload while ajax call

for appending file in formdata use below code

formData = new FormData(); //your form name 
var file_data = $('input[type="file"]')[0].file;
formData.append("file", file_data);
formData.append("function","savepeople"); // new variable for your php condition



$.ajax({
    url: "YOUR_FILE_PATH",
    type: "POST",
    data: formData,
    contentType: false,
    cache: false,
    processData: false,
    success: function(data) {
    // success operation here
    },

and on php side you have to use $_FILES['YOUR_FILE_NAME'] instead of $_POST['YOUR_FILE_NAME'] for accessing a uploaded file on server.

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

6 Comments

Okay, so how do I echo firstname? echo $_POST['firstname']?
yes , you are right , you can echo firstname value using echo $_POST['firstname'];
Well I can't simply. I am calling specific php functions using ajax by passing function POST value as data, but with formdata I can't do that so I don't have anyway to call specific function. See my php function again, I edited it little bit to make more sense.
you can add as many data as you want in formdata by appending in formdata as a get variable
see my new code to know how to pass function value as a new variable
|
0

you can try this code

$.ajax({
    type:'POST',
    url:'functions.php',     
    data:new FormData($('#my_form')[0]),
    cache: false,
    contentType: false,
    processData: false,
    success:function(msg)
    {
         console.log(msg);
    }
});
return false;

where #my_form is your form id

3 Comments

This would work but not in my case, since I am passing two variables to php, first decides which if statement to run and second holds all the data.
@Troutfisher which two variables are you passing?
data: { function: "savepeople", data: formData },
0
var formData = new FormData($(this)[0]);
var action = "savepeople";

    $.ajax({
        url  : 'functions.php',
    type : 'POST',
    data: {action:action,formData:formData},
    contentType: false,
    cache: false,
    processData:false,
    async : false,
    , success: function(data){
console.log(data);
getPeople();
}
});

functions.php

if(isset($_POST['action']) && $_POST['action'] == "savepeople"){


    //TO DO CODE
}

Comments

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.