J
J
Junior Development2021-05-08 16:15:06
PHP
Junior Development, 2021-05-08 16:15:06

How to get data sent by post request VUE(axios)+PHP?

I'm trying to create a file with some content.
The file is being created, but I still can't figure out how to name the file.
I pass data from the input field and send a post request to a php file that creates the file itself.

In the console you can see that there is a clear cant.
60968e9c66ea8957655965.png
Tell me how to correctly send and receive data!

<input type='text' placeholder='File name' v-model='newFileName'>
  <button @click='createFile'>Create file</button>

const Vue   = require("./vue")
const axios = require("axios")

new Vue({
    el: '#app',
    data: {
        newFileName: '',
    },
    methods: {
        createFile() {
            axios
            .post("./api/createFile.php",  {"name": this.newFileName})
            .then((response) => {
                console.dir(response);
            });
        }
    }
})

$name = $_POST['name'];
$text = "Содержимое файла";
$fp = fopen( "../../" . $name . ".txt", "w" );
fwrite( $fp, $text );
fclose( $fp );

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2021-05-08
@jun_dev

Since you are passing it as application/json, then the standard $_POST array will not be populated. You have to accept the data yourself.

$json = file_get_contents('php://input');
$data = json_decode($json, true);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question