0

how do i save the output from javascript function to mysql??

    var macs = {
        getMacAddress : function()
        {
            document.macaddressapplet.setSep( "-" );
return (document.macaddressapplet.getMacAddress());
        }
    }

document.write(macs.getMacAddress());

someone told me to use ajax, but ive tried to figure out but cant get anything.. Anyone would be good to help me. thanks

1
  • Are you using PHP as server side language? Commented Jan 25, 2010 at 2:45

3 Answers 3

1

AJAX would be the way to do that. You can do this with vanilla Javascript but it's messy. Personally I always use a library for this with jQuery being my preferred choice. Then your code becomes:

<input type="button" id="sendmac" value="Send MAC Address">

with:

$(function() {
  $("#sendmac").click(function() {
    document.macaddressapplet.setSep( "-" );
    $.post("savemacaddress.php", {
      getMacAddress: document.macaddressapplet.getMacAddress()
    });
  });
});

savemacaddress.php:

<?php
$addr = $_POST['savemacaddress'];
$addr = mysql_real_escape_string($addr);
$sql = "INSERT INTO macaddress ('$addr')";
mysql_connect(...);
mysql_query($sql);
?>

assuming you're using PHP.

Sign up to request clarification or add additional context in comments.

Comments

0
var mac = macs.getMacAddress();

// if using jQuery - really an AJAX library is very useful

$.get('/send-mac.php?mac=' + mac, function() { alert('yeah done'); })

Then in PHP you would do something like

mysql_query('INSERT INTO macs SET mac = ' . mysql_real_escape_string($_GET['mac']));

Obviously you'll want some more error handling etc.

Comments

0

thanks for the prompt answer...

I've placed the code at create_user.php

var mac = macs.getMacAddress(); 
$.get('/send-mac.php?mac=' + mac, function() { alert('yeah done'); })

can you explain what, "$.get('/send-mac.php?mac=' + mac, function() { alert('yeah done'); })" means?

and checkAvailability,

mysql_select_db($dbname);
$sql_query = mysql_query("SELECT * from user WHERE UserID ='".$_POST['newUserID']."'");
mysql_query('INSERT INTO test SET mac = ' . mysql_real_escape_string($_GET['mac']));  

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.