Reputation: 21
I need to call php function inside javascript with js params. something like.
function test(data) {
$.each(data,function(key, item) {
let resVal = '{{encryptId('+item.id+') }}';
})
}
encryptId is a function defined in php helper file.
Please suggest can it work
Thanks!!
Upvotes: 0
Views: 175
Reputation: 2738
Javascript ran on the Client Side (ie the browser) and PHP was a server side tool (ie the Server side). CLEARLY the two just cant interact.
you can try with AJAX
#JS FILE
function encryptId(item) {
.ajax {
file : phpFile
item : item
}
}
function test(data) {
$.each(data,function(key, item) {
encryptId(item.id)
})
}
#PHP FILE
Here You can catch that variable and call this function
Upvotes: 1
Reputation: 627
That's not possible but a way around it could be using ajax requests. You can hit the function of php through javascript and also send parameters.
Upvotes: 2
Reputation: 3738
This is not possible, PHP and javascript are executed at different time,
Your PHP is executed on the server, before any data is sent to the client, the server himself has no idea about what javascript is
Your javascript is executed on the client, after he received all the code preformated by PHP, the client doesn't know what PHP is nor how to interpret it
The only way to do it is to have the same function in javascript
Upvotes: 1