开发者

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


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.


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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜