1

I got a html5 upload script from http://tutorialzine.com/2011/09/html5-file-upload-jquery-php/

can i pass php variable to js? in this html5 upload script the script.js call the post_file.php to upload file in post_file.php

$rand = time();

i set the rand is the filename for example uploaded filename: 1331956640.jpg

can i pass this $rand to script.js? because i can't print the result in php, only can print something in script.js

this is the html5 upload script download link from tutorialzine enter link description here

sorry my english not good, thank

heyman
  • 375
  • 2
  • 4
  • 16

5 Answers5

4
<script>
var my_javascript_var = <?php echo $rand; ?>
</script>

or

<input id="my_rand_value" type="hidden" value="<?php echo $rand;?>" />

in js do so

var my_javascript_var = $("#my_rand_value").val():
Fawad Ghafoor
  • 6,039
  • 7
  • 41
  • 53
4

It's not actually about "passing" a variable from PHP to JavaScript.

Remember that PHP is a server-side scripting language, and JavaScript resides on a client's browser.

So, you could actually... write directly any javascript you wish from your PHP script.

Let's say, you've got a $a variable... then you could simply enter it in your javascript code like this :

<script type='text/javascript'>
     var a = <?php echo $a; ?>
</script>

However :

If what you mean is to actually use the $a var while the page has loaded, or retrieve the result in some way, WITHOUT reloading, then what you probably need is Ajax.

To use AJAX, I would either suggest :

Dr.Kameleon
  • 22,532
  • 20
  • 115
  • 223
1

You can do this

<script>
    var javascriptvar = <?=$rand ?>
</script>
Tarang
  • 75,157
  • 39
  • 215
  • 276
1

You dont need to ....

<script>
  var rand = new Date().getTime();
</script>

this uses only JavaScript to get the same result

Manse
  • 37,765
  • 10
  • 83
  • 108
0

You can do it this way:

<script language="javascript" type="text/javascript" src="your.js"></script>

...

<a href="javascript:void(0)" onclick="your_js_function(<?php  echo $time; ?>);"><img src="your_button.gif" /></a>
ChatCloud
  • 1,152
  • 2
  • 8
  • 22