canadabeeau Posted December 13, 2009 Share Posted December 13, 2009 Hi I have some javascript that I want the output of, placed in a PHP variable, ie <?php $VAR = ?>js_variable<?php ;?>; Here is my JS document.macaddressapplet.setSep( ":" ); document.macaddressapplet.setFormat( "%02x" ); var macs = eval( String( document.macaddressapplet.getMacAddressesJSON() ) ); var mac_string = ""; for( var idx = 0; idx < macs.length; idx ++ ) mac_string += "\t" + macs[ idx ] + "\n "; document.write(mac_string); Instead of document.write I want the JS variable to be the value of the PHP variable $VAR. Thankyou in advance, any help is welcomed as always :-) Quote Link to comment Share on other sites More sharing options...
Buddski Posted December 13, 2009 Share Posted December 13, 2009 PHP is server side and Javascript is client side.. The only real way that I am aware of to pass a JS variable to PHP is to use a form and post it across.. Quote Link to comment Share on other sites More sharing options...
teamatomic Posted December 13, 2009 Share Posted December 13, 2009 document.macaddressapplet.setSep( ":" ); document.macaddressapplet.setFormat( "%02x" ); var macs = eval( String( document.macaddressapplet.getMacAddressesJSON() ) ); var mac_string = ""; for( var idx = 0; idx < macs.length; idx ++ ) mac_string += "\t" + macs[ idx ] + "\n "; document.write(mac_string); location.href="some_page.php?macaddy=" + mac_string; Now the somepage you use could even be the same page you have the js on if you block it out like a form using the same page for the action. Thats the only way I know that does not involve using a form. But eother way you need to send the data back to the server. Quote Link to comment Share on other sites More sharing options...
canadabeeau Posted December 13, 2009 Author Share Posted December 13, 2009 So I would then use $_GET teamatomic?? Quote Link to comment Share on other sites More sharing options...
canadabeeau Posted December 13, 2009 Author Share Posted December 13, 2009 I am sure I may have seen another way of doing this, or was that for the other way round (PHP to be the value of a JS variable)?? Quote Link to comment Share on other sites More sharing options...
Buddski Posted December 13, 2009 Share Posted December 13, 2009 PHP to a Js variable is very commonly used. But yeah the only real way to send a JS var to PHP is to either use a form or reload the browser with the var tacked onto the URL.. What do you want to do with the JS var in PHP.. Is Ajax an option for your program? Quote Link to comment Share on other sites More sharing options...
canadabeeau Posted December 13, 2009 Author Share Posted December 13, 2009 The JS I posted is part of a script to get the MAC address, I then need to put it into the script you provided on the other post. I am not overly familiar with AJAC but do you think I could use it in this???? Quote Link to comment Share on other sites More sharing options...
Buddski Posted December 13, 2009 Share Posted December 13, 2009 What is your expected result once you put it into the PHP code (other post)? Does it return a value to JS? Insert the values into a Database? if you can tell us what your desired outcome is we can better help you with solutions. Quote Link to comment Share on other sites More sharing options...
canadabeeau Posted December 13, 2009 Author Share Posted December 13, 2009 Okay my Java applet (& JavaScript) obtains a MAC address string 00:4A:88:66:BB:AA 0Z:8A:44:99:AB:BA 00:00:00:00:00:00:00:e0 00:00:00:00:00:00:00:e0 00:00:00:00:00:00:00:e0 I need to get that JavaScript variable into a PHP variable so I can run this <?php $input = '00:4A:88:66:BB:AA 0Z:8A:44:99:AB:BA 00:00:00:00:00:00:00:e0 00:00:00:00:00:00:00:e0 00:00:00:00:00:00:00:e0'; $i = explode('00:00:00:00:00:00:00:e0',$input); $str = '';foreach ($i as $value) { $str .= trim($value); } $array = explode(' ',$str); echo sizeof($array); ?> Quote Link to comment Share on other sites More sharing options...
Buddski Posted December 13, 2009 Share Posted December 13, 2009 so you need to return the size of the array (calculated with PHP) back into the Javascript script? Quote Link to comment Share on other sites More sharing options...
canadabeeau Posted December 13, 2009 Author Share Posted December 13, 2009 Okay I'll explain better. 1. Person visits mysite.com 2. Javascript gets MAC address as JS variable mac_string 3. Make PHP variable $macstringP equal JS variable mac_string 4. Run PHP code described in http://www.phpfreaks.com/forums/index.php/topic,280485.0.html with input being the PHP variable $macstringP The part I want to do is step 3, I need to make a PHP variable equal the JS variable, so if I echo the PHP variable it will echo the same as the JS variable. Does this make more sense?? Thanks for all the help again Can I use AJAX? or another way? Quote Link to comment Share on other sites More sharing options...
teamatomic Posted December 13, 2009 Share Posted December 13, 2009 Yup, use a _GET. HTH Teamatomic Quote Link to comment 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.