Home > Net >  How to call Php function with js params
How to call Php function with js params

Time:12-24

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!!

CodePudding user response:

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

CodePudding user response:

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.

  • Related