Lamez Posted February 14, 2008 Share Posted February 14, 2008 I have this JS code here: userpick[0]=0; It is just a snippet. How do I set it as a php variable, but it still can be processed as a JS variable? like this maybe? $a = userpick[0]=0; Quote Link to comment https://forums.phpfreaks.com/topic/91015-javascript-and-php/ Share on other sites More sharing options...
kenrbnsn Posted February 14, 2008 Share Posted February 14, 2008 You can't, at least not directly. PHP runs on the server, Javascript runs on the client. You can do it if you employ AJAX techniques. Ken Quote Link to comment https://forums.phpfreaks.com/topic/91015-javascript-and-php/#findComment-466515 Share on other sites More sharing options...
Lamez Posted February 14, 2008 Author Share Posted February 14, 2008 Well how would I do that? Quote Link to comment https://forums.phpfreaks.com/topic/91015-javascript-and-php/#findComment-466526 Share on other sites More sharing options...
bpops Posted February 14, 2008 Share Posted February 14, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/91015-javascript-and-php/#findComment-466528 Share on other sites More sharing options...
Lamez Posted February 14, 2008 Author Share Posted February 14, 2008 alright, I will try this, also you forgot a ; when you where showing the JS example lol, I been using PHP for the last 4 or 5 days stright, and I just notice these things now. Quote Link to comment https://forums.phpfreaks.com/topic/91015-javascript-and-php/#findComment-466534 Share on other sites More sharing options...
bpops Posted February 14, 2008 Share Posted February 14, 2008 Heh, I did forget the ';'. Shows how much javascript I actually write. But like ken said, php is server-side, and javascript is client-side, so anything client side you can technically create dynamically, including javascript. It just takes patience. Quote Link to comment https://forums.phpfreaks.com/topic/91015-javascript-and-php/#findComment-466537 Share on other sites More sharing options...
kenrbnsn Posted February 14, 2008 Share Posted February 14, 2008 That's using PHP to generate Javascript, which isn't what the OP seemed to request. Ken Quote Link to comment https://forums.phpfreaks.com/topic/91015-javascript-and-php/#findComment-466539 Share on other sites More sharing options...
haku Posted February 14, 2008 Share Posted February 14, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/91015-javascript-and-php/#findComment-466549 Share on other sites More sharing options...
Lamez Posted February 14, 2008 Author Share Posted February 14, 2008 <script language="JavaScript" type="text/javascript"> <?php echo $a ?> </script> it should be <script language="JavaScript" type="text/javascript"> <?php echo $a; ?> </script> Quote Link to comment https://forums.phpfreaks.com/topic/91015-javascript-and-php/#findComment-466552 Share on other sites More sharing options...
haku Posted February 14, 2008 Share Posted February 14, 2008 That's php, not javascript. That being said, it would have worked without the semi-colon in that case as well. Quote Link to comment https://forums.phpfreaks.com/topic/91015-javascript-and-php/#findComment-466571 Share on other sites More sharing options...
Lamez Posted February 14, 2008 Author Share Posted February 14, 2008 oh, never knew that well could I set this as a variable? Wtf, every time I try to post the code I get a 406?? here is a link to what I want to set as a variable: http://uploads.lamezz.com/var.txt Quote Link to comment https://forums.phpfreaks.com/topic/91015-javascript-and-php/#findComment-466573 Share on other sites More sharing options...
haku Posted February 14, 2008 Share Posted February 14, 2008 I'm not sure what you are trying to ask. Quote Link to comment https://forums.phpfreaks.com/topic/91015-javascript-and-php/#findComment-466578 Share on other sites More sharing options...
Lamez Posted February 14, 2008 Author Share Posted February 14, 2008 sorry, I was getting mad. The link takes you to a text file, and its contents is what I want to set as a PHP variable. Quote Link to comment https://forums.phpfreaks.com/topic/91015-javascript-and-php/#findComment-466581 Share on other sites More sharing options...
haku Posted February 14, 2008 Share Posted February 14, 2008 include("file/name.txt"); Quote Link to comment https://forums.phpfreaks.com/topic/91015-javascript-and-php/#findComment-466589 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.