Call php function inside javascript

Before start I am not knowing AJAX yet.It’s the next step that I plan to do.
The problem:
I has this php function in a php file and that returns an array

 function hitsMonth(){

      return $data_hit_month;

And i want to call this function inside a javascript function and assign to a var data_hits.
Your answers are all welcome


PHP is used as internal utilities : Server Side and Javascript is used Client Side.

The only ways for them to communicate are HTTP (Ajax) or Websocket.

So you should consider to learn Ajax, Rest, Socket and all communication process/protocol between client code and server ones.


My friend
Your answer is ironic.I ask for help explain that i donnt know yet ajax (i am newbie) and your answer is remind me that i must learn this that i asked for.You know…knowledge without ethics is like to wear an expensive brand underwear but dirty underwear.

I am confused. I understood you do not want to learn Ajax to solve this problem.
So, I just wanted to highlight that Ajax is the requirement to achieve your goal.


Globally, Ajax can help you to send an asynchronous request to the server that php (or other) can catch, in this way you can implement and play with some callback functions , an example is better than all :slight_smile:

this example use jQuery (javascript user friendly framework)

       url : "yourScript.php", // the resource where youre request will go throw
       type : "POST", // HTTP verb
       data : { action: 'myActionToGetHits', param2 : myVar2 },
       dataType: "json",
       success : function (response) {
          //in your case, you should return from the php method some fomated data that you  //need throw the data var object in param
             data = toJson(response) // optional
            //heres your code
       error : //some code, 
       complete : //...

what i did : 
Ajax builder need some params to configure correctly the request like data, url, type...
what is interesting, is to use the events param like success, error and complete  where you can put anonymous function and handle it.


in your php script, you’ll receive the request posted throw the superglobal vars like POST (for this example)


$action = (string)$_POST['action']; //this is unsecure, its just for the example

if("myActionToGetHits" == $action) {
 //here you have to call your php function and so on..
$data = hitsMonth();
echo $data;

This is a basic example to do it, there is other ways.

I hope this was helpfull and that my english was not so confusing :slight_smile:

1 Like

To understand why this is so, some understanding of how H T Transfer Protocol between the Server and the Client works is needed.

PHP can put together many things - including JavaScript code with assigned variable values.
But the client (i.e. browser( can only get “new / different” PHP output by making another HTTP Request.
The browser can not run PHP code.

IMHO it is best to allow a page to make another Request even if JavaScript fails to run for whatever reason.

Although a type of “click here for updated information” link works (and as stated should be a fallback) the trend has been to update pages without making an HTTP Request obvious to users.

Again, the HTTP Request still needs to happen if PHP output is wanted.

AJAX is used to make the Request…


This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.