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

0

5 Answers 5

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():
1
  • increase your reputation by marking the Tik to the answer that helped you Commented Jul 27, 2012 at 7:05
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 :

0
1

You can do this

<script>
    var javascriptvar = <?=$rand ?>
</script>
0
1

You dont need to ....

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

this uses only JavaScript to get the same result

4
  • thank^_^ i will use this first~ but can i get the server time? because my server time is faster then this result:] Commented Mar 16, 2012 at 13:22
  • also this result is longer then php time() js:13319xxxxxxxx (13digits) php:13319xxxxx (10digits) Commented Mar 16, 2012 at 13:35
  • js time is in milliseconds you need to divide by 1000 to get php time (in seconds) Commented Mar 16, 2012 at 13:44
  • thank. but how can i get the server time? Commented Mar 16, 2012 at 13:57
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>
0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.