• 0

Jquery ajax


Question

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]<?php
ob_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
https://www.neowin.net/forum/topic/1120320-jquery-ajax/
Share on other sites

4 answers to this question

Recommended Posts

  • 0

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
https://www.neowin.net/forum/topic/1120320-jquery-ajax/#findComment-595329842
Share on other sites

  • 0

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
https://www.neowin.net/forum/topic/1120320-jquery-ajax/#findComment-595329920
Share on other sites

  • 0

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]

  • Like 1
Link to comment
https://www.neowin.net/forum/topic/1120320-jquery-ajax/#findComment-595332454
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • You are clueless. The updates are done in the background so the next time you open Edge the updates are applied automatically. There is no need to close all your tabs. Just keep browsing like you normally do. Clearly you don't use Edge and are just one of those haters that complain for the sake of complaining.
    • I don't get this David. Can you explain it please.  
    • Microsoft is busy. Lots of changes to be released imminently for Windows server or soon. Also, lots happening for next version as well. Third party virus scanning software is being moved out of Kernel mode to avoid repeat of Crowdstrike incident. Windows Protected Mode and Windows Ready Print no longer require third party print drivers to be installed. New storage stack being developed. New NVME drivers now available for Windows Server 2025 to improve local NVME drive performance by 60+ percent. NVME-Of of fabric being worked on for next release to improve network access to NVME drives. ReFs (next file system) now has ability to boot and will become default file system in next release of Windows Server. ReFs improves on NTFS in several areas including resiliency and reliability and scalability. New update stack is being worked on to unify Windows updates, and updates for drivers and first party/3rd party application software. A stricter and more robust third-party driver certification program (ODI) is being worked on to improve performance, thermals, battery life, and reliability on modern Windows hardware by tightening how OEMs and IHVs (Intel, AMD, Qualcomm, NVIDIA, etc.) build and ship drivers. There is a tone more but too numerous to mention.
    • Now disable that stupid OneDrive backup request when Windows starts please. So unbelievably frustrating to only have “remind me later” instead of “no and never ask me again”
    • Hello, The Media Creation Tool is still at v10.0.26100.7019 from October 2025. Just looks like the backend has been updated. Regards, Aryeh Goretsky
  • Recent Achievements

    • One Month Later
      Markus94287 earned a badge
      One Month Later
    • Week One Done
      Markus94287 earned a badge
      Week One Done
    • One Year In
      Markus94287 earned a badge
      One Year In
    • Dedicated
      truespursfan earned a badge
      Dedicated
    • Rookie
      restore went up a rank
      Rookie
  • Popular Contributors

    1. 1
      +primortal
      506
    2. 2
      +Edouard
      168
    3. 3
      PsYcHoKiLLa
      154
    4. 4
      ATLien_0
      90
    5. 5
      Steven P.
      79
  • Tell a friend

    Love Neowin? Tell a friend!