开发者

Javascript function in PHP?

I have a function that is in PHP that I need to use in my Javascript program... how would I go about doing so? The javascript function takes in a name, and returns an array derived from that name. I'm going to assume that I'll have to make the name into a json object (and edit the php function so t开发者_Go百科hat it also returns it as a json object). Is there any way to do this? Thanks.

There's basically this function written in javascript, let's say getData, which is written in javascript. It is used like var fizz = getData(bar). I have a php program that needs to be able to input the bar as well as get the returning fizz.


Use AJAX for this. You will need to make AJAX call to the server-side php code and get whatever you need. You can use jQuery: http://api.jquery.com/category/ajax/

Here you can find some examples for the php (scroll to the bottom). One of the examples is for php+json. Although it does not really matter what server-side technology is.


you can do it like so:

<script type="text/javascript">
   var php_func_result = <?=json_encode(my_function($my_param)); ?>;
</script>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜