15

I have a php file that generates a variable and I would like the variable to be put into a JavaScript function which is called by onclick on the main page. Is this possible to send from PHP to JavaScript?

halfer
  • 19,824
  • 17
  • 99
  • 186
user1879926
  • 1,283
  • 3
  • 14
  • 24
  • 1
    Does it need to be different after each click (e.g., a script that yields the current server-side time)? If so, you probably need [Ajax](https://developer.mozilla.org/en-US/docs/AJAX/Getting_Started). – apsillers Dec 05 '12 at 20:30

6 Answers6

24

You can do the following:

<script type='text/javascript'>
    document.body.onclick(function(){
        var myVariable = <?php echo(json_encode($myVariable)); ?>;
    };
</script>
Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
DiverseAndRemote.com
  • 19,314
  • 10
  • 61
  • 70
6

Just write:

<script>
    var my_variable_name = <?php echo(json_encode($php_string)); ?>;
</script>

Now it's available as a JavaScript variable by the name of my_variable_name at any point below the above code.

Hubro
  • 56,214
  • 69
  • 228
  • 381
  • What if the string contains a double quote? – mickmackusa Mar 02 '22 at 05:11
  • In that case, this solution would break. If there's a chance the string contains double quotes, you would have to encode the string in some way, for example using JSON as the accepted answer suggests. – Hubro Mar 03 '22 at 13:24
  • 1
    Seems worth an [edit] if you are going to leave this answer on the page. – mickmackusa Mar 03 '22 at 13:25
5

Your JavaScript would have to be defined within a PHP-parsed file.

For example, in index.php you could place

<?php
$time = time();
?>
<script>
    document.write(<?php echo $time; ?>);
</script>
Jason
  • 1,192
  • 7
  • 8
2

If I understand you correctly, you should be able to do something along the lines of the following:

function clicked() {
    var someVariable="<?php echo $phpVariable; ?>";
}
username tbd
  • 9,152
  • 1
  • 20
  • 35
1

A great option is to use jQuery/AJAX. Look at these examples and try them out on your server. In this example, in FILE1.php, note that it is passing a blank value. You can pass a value if you wish, which might look something like this (assuming javascript vars called username and password:

data: 'username='+username+'&password='+password,

In the FILE2.php example, you would retrieve those values like this:

$uname = $_POST['username'];
$pword = $_POST['password'];

Then do your MySQL lookup and return the values thus:

echo 'You are logged in';

This would deliver the message You are logged in to the success function in FILE1.php, and the message string would be stored in the variable called "data". Therefore, the alert(data); line in the success function would alert that message. Of course, you can echo anything that you like, even large amounts of HTML, such as entire table structures.

Here is another good example to review.

The approach is to create your form, and then use jQuery to detect the button press and submit the data to a secondary PHP file via AJAX. The above examples show how to do that.

The secondary PHP file receives the variables (if any are sent) and returns a response (whatever you choose to send). That response then appears in the Success: section of your AJAX call as "data" (in these examples).

The jQuery/AJAX code is javascript, so you have two options: you can place it within <script type="text/javascript"></script> tags within your main PHP document, or you can <?php include "my_javascript_stuff.js"; ?> at the bottom of your PHP document. If you are using jQuery, don't forget to include the jQuery library as in the examples given.

In your case, it sounds like you can pretty much mirror the first example I suggested, sending no data and receiving the response in the AJAX success function. Whatever you need to do with that data, though, you must do inside the success function. Seems a bit weird at first, but it works.

Community
  • 1
  • 1
crashwap
  • 2,846
  • 3
  • 28
  • 62
0

You can pass PHP values to JavaScript. The PHP will execute server side so the value will be calculated and then you can echo it to the HTML containing the javascript. The javascript will then execute in the clients browser with the value PHP calculated server-side.

<script type="text/javascript">
    // Do something in JavaScript
    var x = <?php echo $calculatedValue; ?>;
    // etc..
</script>
Ren
  • 1,111
  • 5
  • 15
  • 24