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

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

<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>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

 

 

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.