Jump to content

AJAX to pass JS to server (PHP)


canadabeeau

Recommended Posts

Sorry to be a pain, bu this is what I have done

mac.php

<html>
<head></head>
<body onload="macs.getMacAddressesJSON();">
    <!--[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="../../js/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 ";
			$.ajax({  type: "POST",  url: "process.php",  data: {    'mac': mac_string  }});
            }
        }
    </script>	
<?php 	$require_once = 'process.php';
require_once($require_once);
?>
</body>

 

process.php

<?php
$_GET['mac'];
echo "AAA";
echo $_GET['mac'];
?>

 

now it will echo the AAA but not the mac that was submitted to process.php and then should echo on mac.php

I have no idea why it is echoing AAA and not the mac

Firstly, you need the jQuery library to execute that code. it is available on the link I provided.

 

jQuery code needs to go within a function that checks to make sure your page is loaded. eg;

 

$(document).ready(function() {

  // jquery code goes in here.

});

 

Looking at your code however, you will also need to return mac_string out of the function that is now in, otherwise it will be out f scope.

 

Your final code might look something like....

 

<script type="text/javascript" src="../../js/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",
		data: {
			'mac': macs.getMacAddressesJSON()
		}
	});
        });
</script>

okay thorpe, using your code and my existing process.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="../../js/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",
         data: {
            'mac': macs.getMacAddressesJSON()
         }
      });
        });
</script>

I still get AAABBB, could this be becuase the MAC has not processed (Java has not collected it) by the time it goes to process.php?

You will never see the process.php page. Requiring it after the fact won't help you.

 

Within process.php you will need to write $_POST['mac']'s contents to a file or database or something.

 

Or, you could make process.php look like....

 

<?php

  echo $_POST['mac']

?>

 

And chnage the javascript to....

 

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

 

Then use firfox's firebug extension to see the reponse.

thorpe I have done this now

MAC.PHP

<html>
<head></head>
<body onload="macs.getMacAddressesJSON();">
    <!--[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="../../js/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",
         data: {
            'mac': macs.getMacAddressesJSON()
         }
      });
        });
</script>
</body>
</html>

 

PROCESS.PHP

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

 

it is NOT writing to the file

Your overriding $mac with a value from $_GET['mac'] which does not exist. The ajax script you are using uses POST.

 

You really ought go and take a look at the jQuery mans, its not that difficult once you have an understanding of whats going on.

<script type="text/javascript" src="../../js/jquery-1.3.2.js"></script>
<?php
$mac = $_POST['mac'];
$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $mac);
fclose($fh);
?>

 

still dosent work

<script type="text/javascript">
var m = {
    	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",
         data: {
            mac: m.getMacAddressesJSON()
         }
      });
});
</script>

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

which is line           

 return mac_string

 

macs is not defined
http://10.0.0.1/apps/mac/mac.php
Line 1

which is line

<body onload="macs.getMacAddressesJSON();">

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="../../js/jquery-1.3.2.js"></script>
<script type="text/javascript">
   var m = {
       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",
         data: {
            mac: m.getMacAddressesJSON()
         }
      });
   });
</script>

 

PROCESS.PHP

<script type="text/javascript" src="../../js/jquery-1.3.2.js"></script>
<?php
$mac = $_POST['mac'];
$mac2 = $_GET['mac'];
$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $mac);
fwrite($fh, $mac2);
fclose($fh);
?>

 

The code above is the whol file(s)

 

It is still NOT working

Did you try this...

 

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

 

code like I suggested? This will send any output to the firebug console.

Okay all remaining errors are from jquery-1.3.2.js so there must be a problem with it

 

anonymous function does not always return a value
http://10.0.0.1/js/jquery-1.3.2.js
Line 375 & 537 & 1754 & 1767

 

variable i redeclares argument
http://10.0.0.1/js/jquery-1.3.2.js
Line 1949

 

anonymous function does not always return a value
http://10.0.0.1/js/jquery-1.3.2.js
Line 1957

 

There more too, but where should I get jquery from and which copy?

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.