kennedy
kennedy

Reputation: 1

save javascript function to mysql

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

Upvotes: 0

Views: 1809

Answers (3)

kennedy
kennedy

Reputation: 1

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']));  

Upvotes: 0

cletus
cletus

Reputation: 625077

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.

Upvotes: 1

alex
alex

Reputation: 490233

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.

Upvotes: 0

Related Questions