Craig Hopson Posted November 17, 2012 Share Posted November 17, 2012 hi can some one explain to me what is going on with this code please index.php <!DOCTYPE html><script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" type="text/javascript"></script><script>var data = 'something-here';$.ajax({ type: 'POST', url: 'post.php', data: data});</script>[/CODE] post.php [CODE]<?phpob_start();var_dump($_POST);$data = ob_get_clean();$fp = fopen("textfile.txt", "w");fwrite($fp, $data);fclose($fp);?>[/CODE] textfile.txt [CODE]array(0) {}[/CODE] why is the $_POST variable empty? and how would i post 2 variables like $_POST['firstname'] and $_POST['lastname'] Link to comment Share on other sites More sharing options...
0 Mr. Half Posted November 18, 2012 Share Posted November 18, 2012 The jQuery ajax method expects 'data' to be an object, rather than a string. You should change this line: var data = 'something-here';[/CODE] to this: [CODE]var data = { 'firstname': 'John', 'lastname': 'Smith' };[/CODE] Craig Hopson 1 Share Link to comment Share on other sites More sharing options...
0 Craig Hopson Posted November 18, 2012 Author Share Posted November 18, 2012 The jQuery ajax method expects 'data' to be an object, rather than a string. You should change this line: var data = 'something-here';[/CODE] to this: [CODE]var data = { 'firstname': 'John', 'lastname': 'Smith' };[/CODE] Thanks been looking for this answer for long time i'm still learning how would i replace "John" with a variable [CODE]var firstname = 'john';[/CODE] Link to comment Share on other sites More sharing options...
0 Craig Hopson Posted November 18, 2012 Author Share Posted November 18, 2012 ok some more details here is the full script <!DOCTYPE html><html> <head> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" type="text/javascript"></script> <style> #tripmeter { border: 3px double black; padding: 10px; margin: 10px 0; } p { color: #222; font: 14px Arial; } span { color: #00C; } </style> </head> <body> <div id="tripmeter"> <p> Starting Location (lat, lon):<br/> <span id="startLat">???</span>°, <span id="startLon">???</span>° </p> <p> Current Location (lat, lon):<br/> <span id="currentLat">???</span>°, <span id="currentLon">???</span>° </p> </div> <script> window.onload = function() { var startPos; if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(function(position) { startPos = position; document.getElementById("startLat").innerHTML = startPos.coords.latitude; document.getElementById("startLon").innerHTML = startPos.coords.longitude; }, function(error) { alert("Error occurred. Error code: " + error.code); // error.code can be: // 0: unknown error // 1: permission denied // 2: position unavailable (error response from locaton provider) // 3: timed out }); navigator.geolocation.watchPosition(function(position) { document.getElementById("currentLat").innerHTML = position.coords.latitude; document.getElementById("currentLon").innerHTML = position.coords.longitude; }); } }; </script> <script>$.ajax({ type: 'POST', url: 'post.php', data: data});</script> </body></html>[/CODE] What i'm trying to do is log the current location every 20 seconds and pass it to post.php so it can insert it into a mysql database Link to comment Share on other sites More sharing options...
0 Mr. Half Posted November 19, 2012 Share Posted November 19, 2012 Sounds like an interesting project. Answering your first question, you can just replace the string with the name of the variable and it will work in exactly the same way: var firstname = 'john';var data = { 'firstname': firstname, 'lastname': 'Smith' };[/CODE] If you want to perform an action at a particular interval, take a look at the window.setInterval method. It would look something like this (the interval is in milliseconds): [CODE]window.setInterval(function() { // Get position here // Save position here}, 20000);[/CODE] Seahorsepip 1 Share Link to comment Share on other sites More sharing options...
Question
Craig Hopson
hi can some one explain to me what is going on with this code please
index.php
post.php
ob_start();
var_dump($_POST);
$data = ob_get_clean();
$fp = fopen("textfile.txt", "w");
fwrite($fp, $data);
fclose($fp);
?>[/CODE]
textfile.txt
}[/CODE]
why is the $_POST variable empty? and how would i post 2 variables like $_POST['firstname'] and $_POST['lastname']
Link to comment
Share on other sites
4 answers to this question
Recommended Posts