canadabeeau Posted December 14, 2009 Share Posted December 14, 2009 So I have the following <?php getdetails(?><script type="text/javascript">document.write(jsvariable)</script><?php );?> The idea is that the getdetails() will process the JS output rather than doing mysite.com?data=jsoutput as I REALLY REALLY RALLY dont want urls like that. So why cant I get it to work, I think I have a syntax problem with opening and closing the getdetails() Any help appreciated Quote Link to comment https://forums.phpfreaks.com/topic/185086-passing-js-to-php/ Share on other sites More sharing options...
Buddski Posted December 14, 2009 Share Posted December 14, 2009 As stated in the last post about this, its not going to happen unless you use a GET, Form or AJAX.. Sorry to be the bearer of bad news.. Quote Link to comment https://forums.phpfreaks.com/topic/185086-passing-js-to-php/#findComment-976976 Share on other sites More sharing options...
canadabeeau Posted December 14, 2009 Author Share Posted December 14, 2009 okay I remember you mentioning AJAX how can I do this in AJAX. And your not the bearer of bad news just the person snapping me into reality Quote Link to comment https://forums.phpfreaks.com/topic/185086-passing-js-to-php/#findComment-976978 Share on other sites More sharing options...
trq Posted December 14, 2009 Share Posted December 14, 2009 So why cant I get it to work, I think I have a syntax problem with opening and closing the getdetails() Syntax problems are the least of your worries. You seem to also have a complete misunderstanding of client / server relationships and where php and Javascript fit in. PHP is executed on the server long before it is sent to the client, while Javascript is sent to the client and then executed. They are literally worlds apart. Quote Link to comment https://forums.phpfreaks.com/topic/185086-passing-js-to-php/#findComment-976980 Share on other sites More sharing options...
Daniel0 Posted December 14, 2009 Share Posted December 14, 2009 Javascript can be run on the server just fine... http://en.wikipedia.org/wiki/Server-side_JavaScript Quote Link to comment https://forums.phpfreaks.com/topic/185086-passing-js-to-php/#findComment-976982 Share on other sites More sharing options...
trq Posted December 14, 2009 Share Posted December 14, 2009 The idea is that the getdetails() will process the JS output rather than doing mysite.com?data=jsoutput as I REALLY REALLY RALLY dont want urls like that. Can you explain in detail what your trying to do? Quote Link to comment https://forums.phpfreaks.com/topic/185086-passing-js-to-php/#findComment-976983 Share on other sites More sharing options...
trq Posted December 14, 2009 Share Posted December 14, 2009 Javascript can be run on the server just fine... http://en.wikipedia.org/wiki/Server-side_JavaScript No kidding. But it doesn't contain a document object. I'm sure the OP doesn't really need to be confused further by being made aware. Quote Link to comment https://forums.phpfreaks.com/topic/185086-passing-js-to-php/#findComment-976985 Share on other sites More sharing options...
canadabeeau Posted December 14, 2009 Author Share Posted December 14, 2009 Okay I need to pass the output of the JS which can ONLY be done in JS to a PHP variable, now I don't want to use (or cant) use forms or the URL method, so how can I, all suggestions welcome PS I understand PHP is server side and JS is client side, nevertheless I still need to do what I am after Quote Link to comment https://forums.phpfreaks.com/topic/185086-passing-js-to-php/#findComment-976993 Share on other sites More sharing options...
Garethp Posted December 14, 2009 Share Posted December 14, 2009 Here's your AJAX. http://www.w3schools.com/Ajax/Default.Asp It's a round about way of doing it, but it's the only other way. Quote Link to comment https://forums.phpfreaks.com/topic/185086-passing-js-to-php/#findComment-976995 Share on other sites More sharing options...
Daniel0 Posted December 14, 2009 Share Posted December 14, 2009 Javascript can be run on the server just fine... http://en.wikipedia.org/wiki/Server-side_JavaScript No kidding. But it doesn't contain a document object. I'm sure the OP doesn't really need to be confused further by being made aware. As far as I can tell, he is only using document to output things. I have never used any of the SSJS implementations, but I'm sure they support basic things like outputting to stdout. Depending on what kind of JS he wants to execute, I don't see why he couldn't use something like exec to execute it on the server with an SSJS implementation. Quote Link to comment https://forums.phpfreaks.com/topic/185086-passing-js-to-php/#findComment-976996 Share on other sites More sharing options...
trq Posted December 14, 2009 Share Posted December 14, 2009 Depending on what kind of JS he wants to execute, I don't see why he couldn't use something like exec() to execute it on the server with an SSJS implementation. Could also use this simple js interpreter (j4p5) written in php. Okay I need to pass the output of the JS which can ONLY be done in JS What does the js do? Quote Link to comment https://forums.phpfreaks.com/topic/185086-passing-js-to-php/#findComment-977000 Share on other sites More sharing options...
canadabeeau Posted December 14, 2009 Author Share Posted December 14, 2009 Here is the JS <script type="text/javascript"> var macs = { getMacAddress : function() { document.macaddressapplet.setSep( "-" ); alert( "Mac Address = " + document.macaddressapplet.getMacAddress() ); document.write(mac_string); }, getMacAddressesJSON : function() { 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); } } </script> Which is thanks to a Java Applet for getting MAC address, so any ideas no that you know the code Quote Link to comment https://forums.phpfreaks.com/topic/185086-passing-js-to-php/#findComment-977012 Share on other sites More sharing options...
canadabeeau Posted December 14, 2009 Author Share Posted December 14, 2009 So AJAX is the way but anyone done it before and can tell me where to start never done AJAX before Quote Link to comment https://forums.phpfreaks.com/topic/185086-passing-js-to-php/#findComment-977017 Share on other sites More sharing options...
trq Posted December 14, 2009 Share Posted December 14, 2009 Yep, you'll need to use ajax to send your data back to the server. Quote Link to comment https://forums.phpfreaks.com/topic/185086-passing-js-to-php/#findComment-977019 Share on other sites More sharing options...
canadabeeau Posted December 14, 2009 Author Share Posted December 14, 2009 Okay so after reading all the posts I understand I will need to use AJAX to send the JS variable back to the server to be part of the PHP function (variable), but as a person nver done AJAX before can someone point me in the correct direction please Quote Link to comment https://forums.phpfreaks.com/topic/185086-passing-js-to-php/#findComment-977020 Share on other sites More sharing options...
trq Posted December 14, 2009 Share Posted December 14, 2009 Take a look at jQuery's library, it makes ajax simple. For example... $.ajax({ type: "POST", url: "http://yoursite/process.php", data: { 'mac': mac_string } }); would be enough to send the mac_string data back to process.php on your site. Within process.php it would show up within $_POST['mac']. Quote Link to comment https://forums.phpfreaks.com/topic/185086-passing-js-to-php/#findComment-977022 Share on other sites More sharing options...
canadabeeau Posted December 14, 2009 Author Share Posted December 14, 2009 okay thanks thorpe Quote Link to comment https://forums.phpfreaks.com/topic/185086-passing-js-to-php/#findComment-977028 Share on other sites More sharing options...
Daniel0 Posted December 14, 2009 Share Posted December 14, 2009 Wouldn't it be smarter making the Java applet sending the mac address to your server? Quote Link to comment https://forums.phpfreaks.com/topic/185086-passing-js-to-php/#findComment-977070 Share on other sites More sharing options...
canadabeeau Posted December 14, 2009 Author Share Posted December 14, 2009 Um... how? Quote Link to comment https://forums.phpfreaks.com/topic/185086-passing-js-to-php/#findComment-977073 Share on other sites More sharing options...
canadabeeau Posted December 14, 2009 Author Share Posted December 14, 2009 Here is the file Its not really a .txt its a .java only way I could post though [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/185086-passing-js-to-php/#findComment-977076 Share on other sites More sharing options...
Garethp Posted December 14, 2009 Share Posted December 14, 2009 Rhodry, my previous post included a link to a tutorial on how to use AJAX. Here's the link again http://www.w3schools.com/Ajax/Default.Asp Quote Link to comment https://forums.phpfreaks.com/topic/185086-passing-js-to-php/#findComment-977085 Share on other sites More sharing options...
canadabeeau Posted December 14, 2009 Author Share Posted December 14, 2009 Garethp I got your link, just have no idea where to start with it :-) Maybe you can help me a bit more with that Quote Link to comment https://forums.phpfreaks.com/topic/185086-passing-js-to-php/#findComment-977088 Share on other sites More sharing options...
canadabeeau Posted December 15, 2009 Author Share Posted December 15, 2009 $.ajax({ type: "POST", url: "http://yoursite/process.php", data: { 'mac': mac_string }}); Do I have to run this code or just <?php $.ajax({ type: "POST", url: "http://yoursite/process.php", data: { 'mac': mac_string }});?> Quote Link to comment https://forums.phpfreaks.com/topic/185086-passing-js-to-php/#findComment-977422 Share on other sites More sharing options...
canadabeeau Posted December 15, 2009 Author Share Posted December 15, 2009 I am really not sure how to proceed with this AJAX Quote Link to comment https://forums.phpfreaks.com/topic/185086-passing-js-to-php/#findComment-977429 Share on other sites More sharing options...
canadabeeau Posted December 15, 2009 Author Share Posted December 15, 2009 If I can get this data passed back to the server that will be the end of this thread for good :-) Quote Link to comment https://forums.phpfreaks.com/topic/185086-passing-js-to-php/#findComment-977430 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.