• 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

    • Since they open sourced the calculator in Win 10/11 it is much better and can do a lot, I love it.
    • That's just silly imo. The lengths that man goes to just to avoid W11 is just nuts. Very, very few home users would do that. I will say this though, he is committed. Btw, I note on askwoody that Woody Leonhard passed away March, 2025 aged 73. His site was one of my favorites back in the day. Belated yes but RIP Woody.
    • Because of the EU (a good thing) newer android devices been getting 5 years worth of security patches. Except some Motorola which found the loop hole, and offer ZERO updates. In addition, Google for years have been making where it can patch some stuff by updating the core Google Play Store itself.  As echoed earlier,  you take the security risk in to your own hand beyond supported.
    • Win11Debloat 06.11.2026 by Razvan Serea Win11Debloat is a lightweight, easy to use PowerShell script that allows you to quickly declutter and customize your Windows experience. It can remove pre-installed bloatware apps, disable telemetry, remove intrusive interface elements and much more. The script also includes many features that system administrators and power users will enjoy. Such as a powerful command-line interface, support for Windows Audit mode and the option to make changes to other Windows users. All changes made by Win11Debloat can be easily reversed, and most removed apps can be restored via the Microsoft Store. A full guide on how to undo the changes is available here. Win11Debloat features: Below is an overview of the key features and functionality offered by Win11Debloat. Please refer to the wiki for more information about the default settings preset. Remove a wide variety of preinstalled apps. Click here for more info. Disable telemetry, diagnostic data, activity history, app-launch tracking & targeted ads. Disable tips, tricks, suggestions & ads across Windows. Disable Windows location services & app location access. Disable Find My Device location tracking. Disable 'Windows Spotlight' and tips & tricks on the lock screen. Disable 'Windows Spotlight' desktop background option. Disable ads, suggestions and the MSN news feed in Microsoft Edge. Hide Microsoft 365 ads on the Settings 'Home' page, or hide the 'Home' page entirely. Disable & remove Microsoft Copilot. Disable Windows Recall. Disable Click to Do, AI text & image analysis tool. Prevent AI service (WSAIFabricSvc) from starting automatically. Disable AI Features in Edge. Disable AI Features in Paint. Disable AI Features in Notepad. Disable the Drag Tray for sharing & moving files. Restore the old Windows 10 style context menu. Turn off Enhance Pointer Precision, also known as mouse acceleration. Disable the Sticky Keys keyboard shortcut. Disable Storage Sense automatic disk cleanup. Disable fast start-up to ensure a full shutdown. ...and more. Once you’ve downloaded the Win11Debloat file (Get.ps1), just follow these quick steps: Locate the Get.ps1 script file. Right-click the file and select Run with PowerShell from the context menu. If prompted by User Account Control (UAC), select Yes to grant the script the necessary administrative permissions. Win11Debloat 06.11.2026 fixes: Fix lock screen spotlight option being disabled when disabling the start recommended section by @Raphire in #619 Fix log message formatting by @Raphire Note The -RemoveCommApps and -RemoveW11Outlook command-line parameters for uninstalling a few specific apps have been removed with this release. If you previously relied on these parameters, please see this wiki page for alternative methods of removing these apps. Download: Win11Debloat 06.11.2026 | Open Source View: Win11Debloat Home Page | Screenshots 1| 2 Get alerted to all of our Software updates on Twitter at @NeowinSoftware
  • Recent Achievements

    • Rookie
      restore went up a rank
      Rookie
    • Very Popular
      AndrewSteel earned a badge
      Very Popular
    • Veteran
      Taliseian went up a rank
      Veteran
    • One Month Later
      Clizby earned a badge
      One Month Later
    • One Month Later
      Timaximus earned a badge
      One Month Later
  • Popular Contributors

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

    Love Neowin? Tell a friend!