Then you’re not getting a status 200, which means there’s an error.
There’s a couple issues with the call. First off, a post method doesn’t make a post call like from an HTML form - all that means is the data is passed as a form object instead of as a URL querystring that comes with a GET.
Personally, I prefer the more common ajax method call with the done and fail event handlers attached to them. Top of my head and obviously without testing but this should be closer…
@DaveMaxwell That code is working although it says failure, as oppose to success. I’m using wamp and since all the files I’m using are in the same folder, is the file name enough for the url?
If that doesn’t give you enough information, you can extend the error handling further by delving into the properties of the jgXHR object. Might I suggest you read the JQuery ajax documentation a bit to understand how it works and how to take advantage of the built in functionality?
OK. So that gives you next steps. A quick google search for parsererror says that you’re not returning json from the form. So if you set the datatype as json, you need to return json in the form…
@DaveMaxwell I am getting success using POST, however as its sending the data via url, I should be using GET, not POST. So I changed the method to GET with success. Now I should be able to verify the data in my php file using a vardump(), but its not executing. That’s a question for the PHP forum.
??? Not sure exactly what you’re trying to say there.
A POST method on an ajax call acts like an html form submission where the variable data is in the post object. A GET will add the variable data to a querystring. They both require a URL to send to.
A POST is by nature more secure than a GET but the both do the same thing in slightly different ways. But if you’re performing a form based operation, POST is the correct pattern to follow.
If you have your elements already wrapped into a form element then you can just use
var form = $('yourFormName')[0]; // You need to use standard javascript object here
var formData = new FormData(form);
$.ajax({
url: 'Your url here',
data: formData,
type: 'POST'
// ... Other options like success and etc
});
Otherwise you can also create your own form data
var formData = new FormData();
formData.append('Week1Game1Home', yourData);
var formData = new formData();
formData.append("Week1Game1Home", document.getElementById("Home1Goals").innerText);
formData.append("Week1Game1Away", document.getElementById("Away1Goals").innerText);
No success or failure.
Tried this:
var homeGoals = document.getElementById("Home1Goals").innerText;
var awayGoals = document.getElementById("Away1Goals").innerText;
$.ajax({
method: "POST",
url: "Operations.php",
data: {"Week1Game1Home": homeGoals, "Week1Game1Home": awayGoals}
})
success, but no form data as vardump didn’t execute.
You should learn to use the browsers development tools. There you can see what your request looks like and what it returns. It makes no sense to try and error.