Skip to content
Advertisement

how to call a php function from a class and return the value in vue js?

I’m learning vue but for some reason couldn’t find an answer to this simple question. I have a php class called DataFetcher and i have a vue file and inside of it i wanna call a method from DataFetcher and print the result in the vue page. I’ve always done it with data passed from props like this (learned it from the tutorials i’ve watched):

<form-input name="someName" id="someName" v-model="alien.id"></form-input>

Now i wanna call the method getAlienName($alienId) of the class DataFetcher and put the result in the v-model. Something like this:

<form-input name="someName2" id="someName2" v-model="DataFetcher.getAlienName(alien.id)"></form-input>

How can I achieve this?

getAlienName($alienId) is merely a method that has a db query that returns a name.

Advertisement

Answer

You can’t call PHP functions from a vue context. PHP is executed on the server side, while vue is executed on the client side.

You will have to define an API of some sort to call the function. Just an example:

vue: HTTP GET localhost/api.php?alienId=1337

php:

<?PHP

echo myFunction($_GET['alienId']);

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement