...

View Full Version : php grabbing data from javascript



kennedysee
01-25-2010, 04:48 AM
Apparently i try to save the data into the mysql, but it didnt work.. Anyone help?

create_users.php

<script type="text/javascript">
var mac = macs.getMacAddress();
$.get('/create_users.php?mac=' + mac, function() { alert('yeah done'); })
</script>


checkAvailability.php

$mac = $_GET['mac'];
$mac = mysql_real_escape_string($mac);
$sql = "INSERT INTO test(mac) VALUES ('$mac')";
mysql_query($sql);

bdl
01-25-2010, 06:18 AM
What doesn't work, the insert or the callback?

Where are you performing the connection to the MySQL database?

Dormilich
01-25-2010, 06:32 AM
checkAvailability.php will probably do something, if it is called instead of create_users.php.

bdl
01-25-2010, 06:35 AM
checkAvailability.php will probably do something, if it is called instead of create_users.php.

Excellent observation, I totally missed that.

kennedysee
01-25-2010, 06:42 AM
Manage to get it change.... it only works when i manually called up from the browser...
http://localhost/checkAvailability.php?mac=555

create_users.php


<form method="post" action="checkAvailability.php">
<script type="text/javascript">
var macs = {
getMacAddress : function()
{
document.macaddressapplet.setSep( "-" );
return (document.macaddressapplet.getMacAddress());
}
}
</script>


<script type="text/javascript">
var mac = macs.getMacAddress();
$.get('/createAvailability.php?mac=' + mac, function() { alert('yeah done'); })
</script>
</form>

checkAvailability.php


$dbhost = 'localhost';
$dbuser = 'root';
$dbname = 'registration';
mysql_connect($dbhost, $dbuser);
mysql_select_db($dbname);
$mac = $_GET['mac'];
$mac = mysql_real_escape_string($mac);
$sql = "INSERT INTO test(mac) VALUES ('$mac')";
mysql_query($sql);
echo mysql_error();

rajeevbharti
01-25-2010, 06:58 AM
code looks good

do you get any error ?...

or you can try this code

<?php

mysql_connect('localhost', 'username', 'password') or die("Could not connect database");
mysql_select_db('databasename');
$mac = $_GET['mac'];
$mac = mysql_real_escape_string($mac);
$sql = "INSERT INTO test(mac) VALUES ('$mac')";
mysql_query($sql);

?>

i think this code will work fine..


?>

Dormilich
01-25-2010, 07:31 AM
sure about the URL?


<form method="post" action="checkAvailability.php">
<script type="text/javascript">
var macs = {
getMacAddress : function()
{
document.macaddressapplet.setSep( "-" );
return (document.macaddressapplet.getMacAddress());
}
}
</script>


<script type="text/javascript">
var mac = macs.getMacAddress();
$.get('/createAvailability.php?mac=' + mac, function() { alert('yeah done'); })
</script>
</form>

kennedysee
01-25-2010, 08:04 AM
checkAvailability.php is a piece of php coding to connect to database and inserting data.

Len Whistler
01-25-2010, 08:29 AM
php grabbing data from javascript
Apparently i try to save the data into the mysql, but it didnt work.. Anyone help?


You could write the data to a cookie using Javascript and then read that same cookie with PHP to insert into the database.


------------

kennedysee
01-25-2010, 08:55 AM
php grabbing data from javascript
Apparently i try to save the data into the mysql, but it didnt work.. Anyone help?


You could write the data to a cookie using Javascript and then read that same cookie with PHP to insert into the database.


------------

Do you have an example for me?

Len Whistler
01-25-2010, 10:26 AM
Do you have an example for me?



<html>
<body>
<head>
<script language="JavaScript">

function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

setCookie('cookie_name','Coding Forums','365');
</script>
</head>
<body>

<?php
//Defaults to 1
$javascript_cookie = isset($_COOKIE["cookie_name"]) ? $_COOKIE["cookie_name"] : 1;
echo "$javascript_cookie";

// db insert query
?>
</body>
</html>




------------

kennedysee
01-26-2010, 04:15 AM
<html>
<body>
<head>
<script language="JavaScript">

function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

setCookie('cookie_name','Coding Forums','365');
</script>
</head>
<body>

<?php
//Defaults to 1
$javascript_cookie = isset($_COOKIE["cookie_name"]) ? $_COOKIE["cookie_name"] : 1;
echo "$javascript_cookie";

// db insert query
?>
</body>
</html>




------------

i'm alittle weak at this, would you be good enough to help me implement the function in?? :)

Len Whistler
01-26-2010, 05:57 AM
Everything is tested except for inserting into the database. I don't know what var mac = macs.getMacAddress(); is and if it will work with the script, be sure to insert it below and delete the test variable.

index.htm or index.php

<script language="JavaScript">

function setCookie(c_name,value) {
document.cookie=c_name+ "=" +escape(value);
}

// replace this line with var mac = macs.getMacAddress();
var mac="test data 1234 5677";

setCookie('cookie_name',mac);
window.location = "db_insert.php";

</script>

db_insert.php

<?php
//Defaults to 1
$javascript_cookie_value = isset($_COOKIE["cookie_name"]) ? $_COOKIE["cookie_name"] : 1;

mysql_query("INSERT INTO db_name (table_name) VALUES ('$javascript_cookie_value')");
?>


---------

kennedysee
01-26-2010, 06:21 AM
Everything is tested except for inserting into the database. I don't know what var mac = macs.getMacAddress(); is and if it will work with the script, be sure to insert it below and delete the test variable.

index.htm or index.php

<script language="JavaScript">

function setCookie(c_name,value) {
document.cookie=c_name+ "=" +escape(value);
}

// replace this line with var mac = macs.getMacAddress();
var mac="test data 1234 5677";

setCookie('cookie_name',mac);
window.location = "db_insert.php";

</script>

db_insert.php

<?php
//Defaults to 1
$javascript_cookie_value = isset($_COOKIE["cookie_name"]) ? $_COOKIE["cookie_name"] : 1;

mysql_query("INSERT INTO db_name (table_name) VALUES ('$javascript_cookie_value')");
?>


---------

Thanks. :)

Even if i replace the coding for var mac = macs.getMacAddress();
in my MySQL i still see "test data 1234 5677"... why is that it doesnt appear properly in the database?

Len Whistler
01-26-2010, 06:39 AM
Thanks. :)

Even if i replace the coding for var mac = macs.getMacAddress();
in my MySQL i still see "test data 1234 5677"... why is that it doesnt appear properly in the database?


You must replace: var mac="test data 1234 5677";

With: var mac = macs.getMacAddress();




--------

kennedysee
01-26-2010, 07:00 AM
yeap, i replace that line of coding... Output sometimes come out with "1"

create_users.php

<script type="text/javascript">
var macs = {
getMacAddress : function()
{
document.macaddressapplet.setSep( "-" );
return (document.macaddressapplet.getMacAddress());
}
}
</script>

<script language="JavaScript">
function setCookie(c_name,value) {
document.cookie=c_name+ "=" +escape(value);
}

// replace this line with var mac = macs.getMacAddress();
var mac = macs.getMacAddress();
//var mac="test data 1234 5677";

setCookie('cookie_name',mac);
window.location = "checkAvailability.php";
</script>

checkAvailability.php

$javascript_cookie_value = isset($_COOKIE["cookie_name1"]) ? $_COOKIE["cookie_name1"] : 1;

mysql_query("INSERT INTO test (mac) VALUES ('$javascript_cookie_value')");

Len Whistler
01-26-2010, 07:06 AM
yeap, i replace that line of coding... Output sometimes come out with "1"

This line will need some work:

var mac = macs.getMacAddress();

It's not passing the value of macs.getMacAddress(); to the mac variable. Can you get macs.getMacAddress(); to print out it's value? I don't know much about Javascript and maybe the people in the Javascript Forum will know how to get that line to work.



--------

kennedysee
01-26-2010, 07:14 AM
This line will need some work:

var mac = macs.getMacAddress();

It's not passing the value of macs.getMacAddress(); to the mac variable. Can you get macs.getMacAddress(); to print out it's value? I don't know much about Javascript and maybe the people in the Javascript Forum will know how to get that line to work.



--------

I'm able to print out with this function...

document.write(macs.getMacAddress());


Output: 00-21-70-17-BE-31

kennedysee
01-26-2010, 07:17 AM
For now what i noticed is that, if i change

var mac="test data";

the window explorer will keep refreshing non-stop and insert value "1" into the database..



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum