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

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.

Answer:1

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)

Answer:2

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.

Answer:3

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

Answer:4

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.

Answer:5

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

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

    hello();
   </script>
Answer:6

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

Answer:7

My Code is: $rootScope.getResource = function(id) { $http.get( "path/of/resource/" + id ) .success(function (data, status, header, config) { return data; }) .error(function (data, ...

My Code is: $rootScope.getResource = function(id) { $http.get( "path/of/resource/" + id ) .success(function (data, status, header, config) { return data; }) .error(function (data, ...

I want to draw the road map when i select the few location using check boxes in form.php. I post that locations into map.php file, I assign that posted values into $location array and now I want to ...

I want to draw the road map when i select the few location using check boxes in form.php. I post that locations into map.php file, I assign that posted values into $location array and now I want to ...

I am very very new to Angular and still very much a newbie to Javascript. I followed a tutorial, but the result is not working in my project. Tutorial: http://toddmotto.com/ultimate-guide-to-...

I am very very new to Angular and still very much a newbie to Javascript. I followed a tutorial, but the result is not working in my project. Tutorial: http://toddmotto.com/ultimate-guide-to-...

  1. angularjs scope not working
  2. angularjs scope watch not working
  3. angularjs scope apply not working
  4. angularjs scope broadcast not working
  5. angularjs scope function not working
  6. angularjs scope emit not working
  7. angularjs directive scope not working
  8. angularjs modal scope not working

I append the data from a json-object to a table: var array1 = $.map(data, function (field, i) { return '<tr><td>' + field.date + '</td><td>' + field.art + '</td><...

I append the data from a json-object to a table: var array1 = $.map(data, function (field, i) { return '<tr><td>' + field.date + '</td><td>' + field.art + '</td><...