canadabeeau Posted December 15, 2009 Share Posted December 15, 2009 Hi a I am a AJAX n00b, I need to pass a JavaScript variable "mac_string" to my server file "process.php" how can I do this, using AJAX, not HTML forms or URL method. Must be AJAX. Thanks in advance Quote Link to comment Share on other sites More sharing options...
trq Posted December 15, 2009 Share Posted December 15, 2009 I posted a perfectly good solution in your other thread. Take a look here if your stuck. Quote Link to comment Share on other sites More sharing options...
canadabeeau Posted December 15, 2009 Author Share Posted December 15, 2009 Thanks thorpe Quote Link to comment Share on other sites More sharing options...
canadabeeau Posted December 15, 2009 Author Share Posted December 15, 2009 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 Quote Link to comment Share on other sites More sharing options...
canadabeeau Posted December 15, 2009 Author Share Posted December 15, 2009 this is now process.php <script type="text/javascript" src="../../js/jquery-1.3.2.js"></script> <?php $mac = $_GET['mac']; echo "AAA"; echo $mac; echo "BBB"; ?> it echos the AAA and the BBB but not the mac Quote Link to comment Share on other sites More sharing options...
trq Posted December 15, 2009 Share Posted December 15, 2009 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> Quote Link to comment Share on other sites More sharing options...
canadabeeau Posted December 15, 2009 Author Share Posted December 15, 2009 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? Quote Link to comment Share on other sites More sharing options...
trq Posted December 15, 2009 Share Posted December 15, 2009 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. Quote Link to comment Share on other sites More sharing options...
canadabeeau Posted December 15, 2009 Author Share Posted December 15, 2009 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 Quote Link to comment Share on other sites More sharing options...
trq Posted December 15, 2009 Share Posted December 15, 2009 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. Quote Link to comment Share on other sites More sharing options...
canadabeeau Posted December 15, 2009 Author Share Posted December 15, 2009 <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 Quote Link to comment Share on other sites More sharing options...
trq Posted December 15, 2009 Share Posted December 15, 2009 That could be any number of errors. Use firebug to debug your javascript. Quote Link to comment Share on other sites More sharing options...
canadabeeau Posted December 15, 2009 Author Share Posted December 15, 2009 missing } after property list http://10.0.0.1/apps/mac/mac.php Line 51 macs is not defined http://10.0.0.1/apps/mac/mac.php Line 1 Quote Link to comment Share on other sites More sharing options...
trq Posted December 15, 2009 Share Posted December 15, 2009 <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> Quote Link to comment Share on other sites More sharing options...
canadabeeau Posted December 15, 2009 Author Share Posted December 15, 2009 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();"> Quote Link to comment Share on other sites More sharing options...
trq Posted December 15, 2009 Share Posted December 15, 2009 Remove this line alltogther. <body onload="macs.getMacAddressesJSON();"> And your for loop is missing a brace. for(var idx = 0; idx < macs.length; idx ++ ) { mac_string += "\t" + macs[ idx ] + "\n "; } Quote Link to comment Share on other sites More sharing options...
canadabeeau Posted December 15, 2009 Author Share Posted December 15, 2009 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 Quote Link to comment Share on other sites More sharing options...
trq Posted December 15, 2009 Share Posted December 15, 2009 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. Quote Link to comment Share on other sites More sharing options...
canadabeeau Posted December 15, 2009 Author Share Posted December 15, 2009 macs is not defined http://10.0.0.1/apps/mac/mac2.php Line 50 Quote Link to comment Share on other sites More sharing options...
canadabeeau Posted December 15, 2009 Author Share Posted December 15, 2009 Plus a number of errors from the jquery I downloaded reference to undefined property jQuery.cache[id][name] http://10.0.0.1/js/jquery-1.3.2.js Line 1296 Where and which JQuery should I get? Quote Link to comment Share on other sites More sharing options...
trq Posted December 15, 2009 Share Posted December 15, 2009 Sorry, that is a typo. This line should be.... mac: macs.getMacAddressesJSON(); You could at least attempt to fix the simple errors youself. I have a fultime job here . Quote Link to comment Share on other sites More sharing options...
canadabeeau Posted December 15, 2009 Author Share Posted December 15, 2009 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? Quote Link to comment Share on other sites More sharing options...
canadabeeau Posted December 15, 2009 Author Share Posted December 15, 2009 I am going to recheck everything and post back soon Quote Link to comment Share on other sites More sharing options...
canadabeeau Posted December 15, 2009 Author Share Posted December 15, 2009 Okay the error now after some checking and break is missing } after property list http://10.0.0.1/apps/mac/mac2.php Line 50 which is mac: macs.getMacAddressesJSON(); Quote Link to comment Share on other sites More sharing options...
canadabeeau Posted December 15, 2009 Author Share Posted December 15, 2009 I am still fiddling with no luck Quote Link to comment 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.