Is it possible to format the data like so?
{name: $('#elementId').text()}
I have tried many options, but nothing seems to work. I get ajax success but no data is sent to the server. The return value is null.
$.ajax({
type: "POST",
url: "ReportTest.php",
data: {machineId: $('#FormOverviewMachineID').text()},
success: function(output){
console.log(jQuery.parseJSON(output));
}
})
Any help on this would be fantastic. I feel like I'm pretty close.
Thanks.
Edit:
My PHP is like so (for testing):
$MachineID = $_POST['machineID'];
print_r($MachineID);
I can also print the values to the console before using them in the ajax request like so
machineIdTest = $('#FormOverviewMachineID').text();
console.log("MachineID: "+machineIdTest);
This returns the value fine.
Final Edit
Okay okay, I was dumb but thanks for the responses.
Typo: machineId =/= machineID
And thanks for the mention about parsing the response in JSON.
Cheers everyone!
Initialize the data outside the ajax call:
var machineIdValue = $('#FormOverviewMachineID').text();
$.ajax({
type: "POST",
url: "ReportTest.php",
data: {machineId: machineIdValue},
success: function(output){
console.log(jQuery.parseJSON(output));
}
})
Breakpoint on the machineIdValue assignment line and then check its value in the console.
yes possible
$('#FormOverviewMachineID').val() instead of $('#FormOverviewMachineID').text();
Its a table cell so you can use $('#FormOverviewMachineID').html()
to get its value.
Try and let me know.
Change your script to this...
$.ajax({
type: "POST",
url: "ReportTest.php",
data: {machineID: $('#FormOverviewMachineID').text()},
success: function(output){
console.log(output);
}
});
I've only made 1 small change. You originally passed the data variable machineId
, but then looked for machineID
in PHP. They're different names in your post (uppercase/lowercase D).