2

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.

1

8 Answers 8

3

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)

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

Comments

1

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>

3 Comments

Can you help me to write code because i am new to AJAX.
@Vibhs - No because StackOverflow is not a code-writing service site.
jQuery in Action (manning.com/bibeault2) had good AJAX coverage. There is head first AJAX. And hundreds of others. :-)
1

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.

Comments

0

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

Comments

0

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.

Comments

0

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

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

    hello();
   </script>

Comments

0

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:)

Comments

0

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

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.