Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.