Jump to content

Passing JS to PHP


canadabeeau

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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'].

Link to comment
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.