Web Hosting Talk







View Full Version : Javascript in PHP?


Gatordog
06-10-2009, 08:51 PM
Is it possible to put javascript image rotation into php? I think I've done it before but I can't remember.

Kohrar
06-10-2009, 09:38 PM
You can't put javascript code in a php script and expect the javascript to be parsed/executed as if it were in a browser. You can embed the javascript outside of the php tags though:


<?php
/* PHP Code Goes Here */
?>
<script type="text/javascript">
/* Javascript Code Goes Here*/

<?php
/* Output javascript code from PHP */
?>
</script>

rasin
06-11-2009, 01:49 AM
You can use php array inside javascript using the following code

it will convert php array in to javascript format



<?php
$flowers = array('rose','lilly','jasmin');
?>
<script type="text/javascript">
<?php
$jflowers = "var flowers = new Array();\n";

foreach($flowers as $key => $flower) {
$jflowers .= "flowers[$key] = '$flower'; \n";
}

echo $jflowers;
?>
</script>

Ezeelogin (http://www.ezeelogin.com/) - The ultimate multiple server administration software.
* Parallel shell * rm -rf protection * SSH logging * automated password changes * encrypted storage *
AdMod.com (http://www.admod.com/) - Delivering innovative web hosting solutions

Shinary
06-13-2009, 11:32 AM
If you want to have JavaScript on a page that is rendered by a PHP script then I would highly recommend having the two act as independently of each other as possible. You really don't want to be getting into the practice of having PHP code generate JavaScript code, because you are setting yourself up for problems.

Look into using frameworks like YUI and jQuery to make more independent and reusable JavaScript objects.

If you are trying to have PHP output an image and JavaScrip do something to said image, then you are much better of giving your HTML code a proper ID and class structure and using the YUI's onLoad or onDOMReady functions do do your manipulations.

jwebhost
06-16-2009, 04:01 PM
You can do PHP in Javascript, But not Javascript in PHP. Alot of conflict between them.