Jump to content

AJAX to pass JS to server (PHP)


canadabeeau

Recommended Posts

All the code between the tags are the FULL code, so every line, none spared

MAC.PHP

	<!--[if !IE]> Firefox and others will use outer object -->
    <embed type="application/x-java-applet"
           name="macaddressapplet"
           width="0"
           height="0"
           code="MacAddressApplet"
           archive="macaddressapplet.jar"
           pluginspage="http://java.sun.com/javase/downloads/index.jsp"
           style="position:absolute; top:-1000px; left:-1000px;">
        <noembed>
        <!--<![endif]-->
            <!---->
            <object classid="clsid:CAFEEFAC-0016-0000-FFFF-ABCDEFFEDCBA"
                    type="application/x-java-applet"
                    name="macaddressapplet"
                    style="position:absolute; top:-1000px; left:-1000px;"
                    >
                <param name="code" value="MacAddressApplet">
                <param name="archive" value="macaddressapplet.jar" >
                <param name="mayscript" value="true">
                <param name="scriptable" value="true">
                <param name="width" value="0">
                <param name="height" value="0">
              </object>
        <!--[if !IE]> Firefox and others will use outer object -->
        </noembed>
    </embed>
    <!--<![endif]-->
<script type="text/javascript" src="jquery-1.3.2.js"></script>
<script type="text/javascript">
        var macs = {
       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 ";
		}
            return mac_string
        }
   }

$(document).ready(function() {
   $.ajax({
       type: "POST",
       url: "process.php",
      dataType: "json",
       data: {
          mac: macs.getMacAddressesJSON();
       };
      success: function(d) {
         console.log(d)
      }
     });
});
</script>

 

PROCESS.PHP

<?php
$mac = $_POST['mac'];
$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $mac);
fclose($fh);
?>

 

I manually created testFile.txt so ist does exist

 

ERRORS ARE:

missing } after property list
http://10.0.0.1/apps/mac/mac2.php
Line 50
which is 

                mac: macs.getMacAddressesJSON();

 

and a range for jquery.js (I presume that is normal as it is a straight download from their site and they are only warnings not errors)

 

thorpe thanks for all your help your #1

Link to comment
Share on other sites

Your declaring macs twice for starters. This will break things.

 

All this embbeding code? Where is it within your document?

 

You might want to simply try sending a simple string via ajax so we can at least eliminate that as a problem.

 

Link to comment
Share on other sites

Here is my simpler test code:

mac9.php

<script type="text/javascript" src="jquery-1.3.2.js"></script>
<script type="text/javascript">
       var macs = {
       getMacAddressesJSON : function() {
            var mac_string = "aaaaaaaaaaaaaaa";
            return mac_string
        }
   }

$(document).ready(function() {
   $.ajax({
       type: "POST",
       url: "process.php",
      dataType: "json",
       data: {
          'mac': macs.getMacAddressesJSON()
       }
     });
});
</script>

 

process.php

<?php
$mac = $_POST['mac'];
$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $mac);
fclose($fh);
?>

Link to comment
Share on other sites

IT WORKS :-)

mac.php

	<!--[if !IE]> Firefox and others will use outer object -->
    <embed type="application/x-java-applet"
           name="macaddressapplet"
           width="0"
           height="0"
           code="MacAddressApplet"
           archive="macaddressapplet.jar"
           pluginspage="http://java.sun.com/javase/downloads/index.jsp"
           style="position:absolute; top:-1000px; left:-1000px;">
        <noembed>
        <!--<![endif]-->
            <!---->
            <object classid="clsid:CAFEEFAC-0016-0000-FFFF-ABCDEFFEDCBA"
                    type="application/x-java-applet"
                    name="macaddressapplet"
                    style="position:absolute; top:-1000px; left:-1000px;"
                    >
                <param name="code" value="MacAddressApplet">
                <param name="archive" value="macaddressapplet.jar" >
                <param name="mayscript" value="true">
                <param name="scriptable" value="true">
                <param name="width" value="0">
                <param name="height" value="0">
              </object>
        <!--[if !IE]> Firefox and others will use outer object -->
        </noembed>
    </embed>
    <!--<![endif]-->
<script type="text/javascript" src="jquery-1.3.2.js"></script>
<script type="text/javascript">
        var macs = {
       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 ";
		}
            return mac_string
        }
   }

$(document).ready(function() {
   $.ajax({
       type: "POST",
       url: "process.php",
      dataType: "json",
       data: {
          mac: macs.getMacAddressesJSON()
       }
     });
});
</script>

 

Process.php

<?php
$mac = $_POST['mac'];
$myFile = "testtextfile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $mac);
fclose($fh);
?>

 

Thanks a billion thorpe, I dont know what I did but I (thanks to you) got it working, had to remove some of your lines of code i.e.

      success: function(d) {
         console.log(d)
      }

 

which seemed to make it work with some other stuff

 

So thanks a million, this is now sorted, finally :-)

Link to comment
Share on other sites

okay now another problem with it,

If i visit mysite.com it will not display the MAC but if I first visit mysite.com/apps/mac/index.php then go back to mysite.com the mac works

any thoughts on this.

 

from mysite.com/index.php I call it like this

<?php
$require_once = 'apps/mac/index.php';
require_once($require_once);
?>

in the body of the page <body>

Link to comment
Share on other sites

root/apps/mac/index.php

 <!--[if !IE]> Firefox and others will use outer object -->
    <embed type="application/x-java-applet"
           name="macaddressapplet"
           width="0"
           height="0"
           code="MacAddressApplet"
           archive="macaddressapplet.jar"
           pluginspage="http://java.sun.com/javase/downloads/index.jsp"
           style="position:absolute; top:-1000px; left:-1000px;">
        <noembed>
        <!--<![endif]-->
            <!---->
            <object classid="clsid:CAFEEFAC-0016-0000-FFFF-ABCDEFFEDCBA"
                    type="application/x-java-applet"
                    name="macaddressapplet"
                    style="position:absolute; top:-1000px; left:-1000px;"
                    >
                <param name="code" value="MacAddressApplet">
                <param name="archive" value="macaddressapplet.jar" >
                <param name="mayscript" value="true">
                <param name="scriptable" value="true">
                <param name="width" value="0">
                <param name="height" value="0">
              </object>
        <!--[if !IE]> Firefox and others will use outer object -->
        </noembed>
    </embed>
    <!--<![endif]-->
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
       var macs = {
       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 ";
         }
            return mac_string
        }
   }

$(document).ready(function() {
   $.ajax({
       type: "POST",
       url: "process.php",
      dataType: "json",
       data: {
          mac: macs.getMacAddressesJSON()
       }
     });
});
</script>

 

root/apps/mac/process.php

<?php
session_start();
$mac = $_POST['mac'];
$input = $mac;
$i = explode('00:00:00:00:00:00:00:e0',$input);
$str = '';foreach ($i as $value) {	
$str .= trim($value);
}
$array = explode(' ',$str); 
$_SESSION['clientmac'] = $array[0];
?>

 

root/index.php

<?php
ob_start();
session_start();
$require_once = '_inc/php/functions.php';
require_once($require_once);
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Rhodry Korb</title>
<link rel="stylesheet" type="text/css" href="/css/screen.css" />
<script type="text/javascript" src="/js/jquery.js"></script>
</head>
<body>
<?php
$require_once = 'apps/mac/index.php';
require_once($require_once);
?>
        <?php echo $_SESSION['clientmac'];?>
</body>
</html>

Link to comment
Share on other sites

You need to think about the process. You have a page that echos an unset $_SESSION variable when it is executed. Once this script is executed it gets sent to the client. Once on the client you have some javascript which executes and makes another request back to the server and sets the $_SESSION variable you attempted to echo within the last request.

 

Thats it, the whole process.

 

The only way the page which is still sitting on the client can get to echo this $_SESSION variable is if you refresh it and make another request or to let Javascript update the current page.

 

If all you want to do is display a user mac address why are you sending it back to the server just to try and retrieve it again when it already exists on the client?

Link to comment
Share on other sites

found a/the errror

document.macaddressapplet.setSep is not a function
http://10.0.0.1/
Line 43

 

which is

           document.macaddressapplet.setSep( ":" );

 

why would I get this? This is the code required from the mac/index.php which works well by itself

 

Actually the error only disappears after two refreshes (at least on apps/mac/index.php) so why would I get it?

Link to comment
Share on other sites

No doubt your logic is screwed up somewhere. Its likely that your embed tags need to actually go within your actual document somewhere. hence attaching it to the document object.

 

I'm not real sure as I haven't and wouldn't use embed tags. They are not part of xhtml, and are only officially supported in the brand new html5.

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.