0

I need to send result from my PHP file to the JavaScript function/file. I found some of answers like: var x='<?php echo $pathinfo; ?>'; or this:

myHtmlFile.html:

<script type="text/javascript" src="jquery-1.8.1.js"></script>
<script type="text/javascript" src="newEmptyPHP.php"></script>
<script language="javascript">

function test()
{
   alert(result);
}

</script>

newEmptyPHP.php:

<?php
     $res="it is result";
     echo "var result = ".json_encode($res).";";
?>

My question is whether there is way return value from php function and not from php file. something like this:

<?php

function phpFunction(){
     $res="it is result";
     return $res;
}
?>

I need simple PHP/JavaScript code without something complex. I want keep many function in one php file and not many php files.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
igorb0214
  • 67
  • 2
  • 2
  • 9
  • Use a post request, you cannot pass JavaScript variables in a PHP function directly as PHP runs on server. If the function is ambiguous you can just echo it into the JavaScript variable. – Patrick Geyer Jan 10 '14 at 12:54
  • also use an approriate header when serving PHP files as JS. `header("content-type: application/javascript");` – thpl Jan 10 '14 at 12:56
  • you will 'call' the same function in javascript which you have 'defined' in php file – Siva Tumma Jan 10 '14 at 12:57
  • Very ugly! Try this: http://www.w3schools.com/ajax/ajax_intro.asp – Thiago C. S Ventura Jan 10 '14 at 12:58
  • Also, I would suggest using classes if you have many PHP function and do not want them in different files – Patrick Geyer Jan 10 '14 at 12:59
  • possible duplicate of [Pass a PHP string to a JavaScript variable (and escape newlines)](http://stackoverflow.com/questions/168214/pass-a-php-string-to-a-javascript-variable-and-escape-newlines) – thomaux Jan 10 '14 at 13:14

3 Answers3

3
<?php 
function phpFunction(){
     $res="it is result";
     return $res;
}
?>

<script>
var result = "<?php echo phpFunction() ?>";
</script>

The above method will work. However, if you want more PHP/JS combined scripts, learn AJAX.

Eisa Adil
  • 1,743
  • 11
  • 16
  • I like your solution and it works the same file but when I tried to get the result in another file, it did not work. How can I make the result would be in another javaScript file? – igorb0214 Jan 10 '14 at 14:36
1

The best way to get your data to JavaScript from PHP is to use AJAX using jQuery you have declared.

You can use $.get()

You can call you php script which contains the function with parameters in you url request.

And when a specific parameter is specified in the url, then you can call you fonction and echo whatever you want to the javascript.

Have a good day!

bug
  • 342
  • 1
  • 10
0

You can open as many php tags as you wish inside your one php document. You can then either make this php document separate from your main document (however this would require making the main document in php and using a "php include" or "php require") or you can simply just open and close php tags as necessary. You do not even have to close the line at the end of the php, for example.

<?php if (a==a) { ?>
<div>
    <script>
    <?php echo 'do something here'; ?>
    </script>
</div>
<?php } ?>

Using the above example can provide some unique results. Just remember that anything you do with php you can just echo out the result and slot it into the middle of your javascript.

TFennis
  • 1,393
  • 1
  • 14
  • 23