Gandalf
Gandalf

Reputation: 13693

axios formdata does not post data to server

I have this axios code i am using to post form data

updatesetting: function(){

    let formData = new FormData();
    formData.append('email', this.email);
    formData.append('country', this.country);
    formData.append('names', this.names);
    formData.append('language', this.language);
    formData.append('userid', this.$store.state.userid);
    formData.append('telephone', this.telephone);

    axios.post('https://example.com/users/update_profile',
        formData,
        {
        headers: {
            'Content-Type': 'multipart/form-data'
        }
      }
    ).then(function(response){
      console.log(response.data);
    })
    .catch(function(){
      console.log('FAILURE!!');
    });

},

On submit i am executing the function. On the server side i have this code

public function update_profile(){
    header('Access-Control-Allow-Origin: *');

    header('Access-Control-Allow-Methods: GET, POST');

    header("Access-Control-Allow-Headers: X-Requested-With");
    header('Content-Type: application/json');
        
    
    $obj = json_decode(file_get_contents("php://input"), TRUE);
    
                    
    $currency = $obj['currency']; 
    $email = $obj['email']; 
    $country = $obj['country']; 
    $names = $obj['names']; 
    $language = $obj['language']; 
    $userid = $obj['userid']; 
    $telephone = $obj['telephone'; 

My html

  <div class="form-group">
    <label for="exampleInputEmail1">Your Names</label>
    <input type="text" class="form-control" v-model="names" placeholder="Names">
    <small id="emailHelp" class="form-text text-muted"></small>
  </div>

When i return the value of $obj its returning a blank and i dont know why i am not receiving the posted form data as i should. How can i receive the data on the server side? I have confirmed that the form data is being posted.

Upvotes: 0

Views: 50

Answers (1)

Gandalf
Gandalf

Reputation: 13693

I have seen dozens of articles and answers that point out that this is the correct way

$obj = json_decode(file_get_contents("php://input"), TRUE);

then

 $obj['email'];

which does not work at all.

Simply, $_POST['email']; and this works

Upvotes: 1

Related Questions