0

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 that 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.

2
  • 6
    PHP and JavaScript are completely separate. Please show us what the function actually is, and we can offer advice. Commented Aug 18, 2011 at 23:15
  • 2
    Your question isn't very clear. Do you want to take the PHP logic and implement it in Javascript or vice versa, you contradict yourself. Also, keep in mind that PHP runs on the server and Javascript runs on the client, entirely different worlds. We'll need more background. Showing the code wouldn't hurt either. Commented Aug 18, 2011 at 23:17

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

you can do it like so:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.