Jump to content

Archived

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

canadabeeau

AJAX to pass JS to server (PHP)

Recommended Posts

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

Share this post


Link to post
Share on other sites

I posted a perfectly good solution in your other thread.

 

Take a look here if your stuck.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
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>

Share this post


Link to post
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?

Share this post


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

Share this post


Link to post
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

Share this post


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

Share this post


Link to post
Share on other sites

<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

Share this post


Link to post
Share on other sites

That could be any number of errors. Use firebug to debug your javascript.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
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>

Share this post


Link to post
Share on other sites

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();">

Share this post


Link to post
Share on other sites

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 ";
}

Share this post


Link to post
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

Share this post


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

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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 :).

Share this post


Link to post
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?

 

 

Share this post


Link to post
Share on other sites

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();

Share this post


Link to post
Share on other sites

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