Jump to content

Javascript and PHP?


Lamez

Recommended Posts

Lamez,

 

I often create 'dynamic' javascript based off a php function. I'm not sure if this is exactly what you're looking for, but you could always generate the javascript code like

<?php

$a = 'userpick[0]=0';
// put straight into string here, but
// could also be done dynamically such
// as $a = 'userpick['.$index.']=0';

?>

 

and then echo that code when needed in your html

 

<script language="JavaScript" type="text/javascript">
<?php echo $a ?>
</script>

 

Maybe this isn't what you're asking for, but if it is, it can be done, the only somewhat tricky part is getting all of your quotation marks to work out correctly.  :P

Link to comment
https://forums.phpfreaks.com/topic/91015-javascript-and-php/#findComment-466528
Share on other sites

you forgot a ; when you where showing the JS example

 

Semi-colons are optional in javascript.

 

That's using PHP to generate Javascript, which isn't what the OP seemed to request.

 

I agree. Its possible (and even necessary) to output javascript from PHP, so that when its passed from the server to the browser, the javascript is there to be accessed. But its impossible to use javascript in PHP because php is processed on the server and javascript is processed on the browser. The closest you can do is set cookies with javascript and read them with php.

Link to comment
https://forums.phpfreaks.com/topic/91015-javascript-and-php/#findComment-466549
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.