Vibhuti.patel
Vibhuti.patel

Reputation: 63

can i call function created in php from java script or jquery ? How?

I want to call my function in php from java script or jquery.

I have tried this.

    <script>
    function hello(){
         <?php
              print_hello();
          ?>
    }

hello();

</script>

print_hello() is my php function which prints hello.

Upvotes: 2

Views: 111

Answers (8)

theWalker
theWalker

Reputation: 2020

It's simply

$(document).ready(
    function run_php() {
        $('.my_class').load('my.php');
    }
);

// or with parms
//$('.my_class').load('my.php', {'path': 'my_parms'});

where my_class is any class in current php

Upvotes: 0

Shaik Mahaboob Basha
Shaik Mahaboob Basha

Reputation: 1082

Its not possible to call php method directly, it gives error not defined as there is no method print_hello in the javascript file.

ReferenceError: print_hello is not defined

You have to use AJAX to call a php method from the javascript.

AJAX, is little bit tricky, if you want to make it simple you can use jQuery library.

You have to include your code in the print_hello method in a file and call the file using AJAX. Let say you have put the method print_hello in a file print_hello.php, the sample code will be:

$.ajax({
  url: "print_hello.php"
}).done(function() {
  alert( "called php code succesfully" );
});

And put your php code in file php_hello.php

<?php
  print_hello();
?>

More info on how to make ajax call found at

Ajax jQuery

NOTE: ( as you are new to web development)

Web development is all about communication. In this case, communication between 2 parties, over the HTTP protocol:

The Server - This party is responsible for serving pages.

The Client - This party requests pages from the Server, and displays them to the user. On most cases, the client is a web browser. The User - The user uses the Client in order to surf the web, fill in forms, watch videos online, etc. Each side's programming, refers to code which runs at the specific machine, the server's or the client's.

More info At this StackOverflow Question (server side and client side programming)

Upvotes: 3

dreamweiver
dreamweiver

Reputation: 6002

Make a ajax call to server side code like this

PHP CODE(target.php):

<?
  function foo( ) {
    //do some processing  here and echo the data 
  }
?>

JQUERY CODE:

$.ajax({
  url: "/target.php/foo",
  data:{}  //data to the server script 
  success: function( data ) {
        //Data returned from server side script
  }

});

Happy Coding:)

Upvotes: 0

Diwakar upadhyay
Diwakar upadhyay

Reputation: 434

No it is not possible you can not call through script.

    <script>
    function hello(){
       console.log('<?php print_hello();';?>
     }

    hello();
   </script>

Upvotes: 0

Luke Cordingley
Luke Cordingley

Reputation: 685

It is possible to dynamically generate JavaScript from the server side. This works because PHP executes on the server side before it's sent to the client. It depends on what your PHP function is outputting. If it's outputting valid JavaScript then it will work. For example if your print_hello() function outputs alert('test'); then the JavaScript code would do just that. However, I'd recommend not dynamically generating javascript on the server side unless it's absolutely necessary for the sake of maintainability.

Upvotes: 0

user13500
user13500

Reputation: 3856

No. Serverside vs. Clientside.

PHP run on server and Javascript (normally) runs on client.

If your PHP print_hello(); function is something like this

function print_hello() {
    printf "document.getElementById('some_id').innerHTML = 'Hello!';";
}

then that would result in this in users browser:

function hello(){
    document.getElementById('some_id').innerHTML = 'Hello!';
}

hello();

So in short: You can create Javascript in PHP serverside, but you cannot run PHP scripts clientside.

Upvotes: 1

Manish Kumar
Manish Kumar

Reputation: 581

I think you can. You need to store the Javascript in .php file. And Put your Php function outside the tag.

Upvotes: 0

Darren Cook
Darren Cook

Reputation: 28978

No, JavaScript is run in the browser, PHP is run server-side.

You should look at using AJAX (e.g. with jQuery to make it easy) to run specific PHP functions on the server, and return the results.

Alternatively, if the code snippet in your question was html being served by a PHP script you could write:

<script>
function hello(){
     console.log('<?php print_hello();';?>
     }

hello();

</script>

Upvotes: 1

Related Questions