tags:

views:

88

answers:

5

how can we use php in between javascript ?????
like

function jst()
{
var i = 0 ;
i = <?php echo 35; ?>
alert( i );
}

Please suggest a better way

+2  A: 

Yes, you can, provided your JavaScript code is embedded into a PHP file.

Pablo Santa Cruz
can u tell please a simple hello world program ,so that my i had a little light on my mind
Extjs Commander
A "hello world" program doing what exactly?
Pablo Santa Cruz
Basically what (I think) he's saying is if the above was code snippet was in a .php file it'll work. It should be noted that it might not necessarily the way you expect it to, depending on what you expect.
Davy8
hello world that display or alert accepting var from php file
Extjs Commander
+1  A: 

You're pretty much on the ball. The only difference is I'd separate out the javascript so the majority was in an external static file. Then you just define variables or call a function from the actual PHP page:

<script type="text/javascript>
    function_in_other_file(<?php echo my_php_var; ?>);
</script>
Oli
hi not working , i tried
Extjs Commander
<?php $my_var = "amit" ; ?> <script type="text/javascript" src="output/exp.js" > function jst() { fun( <?php echo $my_var; ?> ); } jst(); </script> exp.js contains -> fun( i ) { alert( i ); }
Extjs Commander
A: 

We can't use "php in between javascript", because php runs on the server and js - on client.
However we can generate JS code as well as HTML, using all PHP features, including escaping from HTML one

Col. Shrapnel
Please see the other answers will learn new style of coading :)
Extjs Commander
@Dumbledore other answers has nothing to add to this one. Accepted answer does exactly the same. I wonder where did you find a "new style"
Col. Shrapnel
+5  A: 

If your whole JS code gets processed by PHP, then you can do it just like that. If you have individual .js files, and you don't want PHP to process them (for example, for caching reasons), then what you can do is just pass variables around in JS. For example in your index.php (or wherever you specify your layout), you'd do something like this:

<script type="text/javascript">
var my_var = <?php echo json_encode($my_var); ?>;
</script>

You could then use my_var in your JS files. This method also lets you pass other than just simple integer values, as json_encode() also deals with arrays, strings, etc. correctly, serializing them into a format that JS can use.

reko_t
man u are really genius
Extjs Commander
my hats off to u :)
Extjs Commander
+1  A: 

If you put your javascript code in php file, you can not otherwise, for example:

page.php (This will work)

function jst()
{
var i = 0 ;
i = <?php echo 35; ?>
alert( i );
}

page.js (This won't work)

function jst()
{
var i = 0 ;
i = <?php echo 35; ?>
alert( i );
}
Sarfraz
page.php is not working tooo :)
Extjs Commander
@flash: Make sure to wrap your js code in `<script>` tags.
Sarfraz
<?php $my_var = "amit" ; ?> <script type="text/javascript" src="output/exp.js" > function jst() { fun( <?php echo $my_var; ?> ); } jst(); </script> exp.js contains -> fun( i ) { alert( i ); }
Extjs Commander