Reputation: 1664
I am attempting to save 2 pieces of information to the database via JQuery using the .ajax function. I want to save the postID and the userID in WordPress. I think I get the jist of most of the function but need to figure out how to send an array to the processing page so I can insert it. This is what i have written so far:
$(document).ready(function() {
$('#saveme').click(function() {
$.ajax({
type: "POST",
url: "save_data.php",
contentType: "application/json; charset=utf-8",
data: "",
dataType: "json",
success: function (msg) {
alert("This recipe has been saved in your profile area!");
}
});
});
Can anyone shed some light one what goes into the data value to store the 2 pieces of information that I can send to the processing page?
I am using PHP and the code is in a .js file so I might need to also know to send the information over to the js file. Thanks!!
Upvotes: 0
Views: 8454
Reputation: 233
The type of Data to be sent to the server is JavaScript object containing zero or more key-value pairs or String. For your case
data: {
'postID' : $('#postID').val(),
'userID' : $(('#userID').val()
},
Upvotes: 1
Reputation: 1745
You forgot to add data
$(document).ready(function() {
var postID='<?php the_id(); ?>';
var userId='<?php author_id(); ?>';
$('#saveme').click(function() {
$.ajax({
type: "post",
url: "save_data.php",
contentType: "application/json; charset=utf-8",
data: {postID: postID, userId: userId},
dataType: "json",
success: function (msg) {
alert("This recipe has been saved in your profile area!");
}
});
});
Upvotes: 0
Reputation: 10719
Just create a JSON object and send it: (assuming say there is an element on the page by the id of postID and userID
var jsonData = { "postID" : $("#postID").val(),
"userID" : $(("#userID").va;() }
$(document).ready(function() {
$('#saveme').click(function() {
$.ajax({
type: "POST",
url: "save_data.php",
contentType: "application/json; charset=utf-8",
data: jsonData,
dataType: "json",
success: function (msg) {
alert("This recipe has been saved in your profile area!");
}
});
});
Upvotes: 0
Reputation: 5749
data should be a JSON object containing the data you want to save, i.e.
$(document).ready(function() {
$('#saveme').click(function() {
$.ajax({
type: "POST",
url: "save_data.php",
contentType: "application/json; charset=utf-8",
data: {postID: "A123456", userId: "HGSADKJ"},
dataType: "json",
success: function (msg) {
alert("This recipe has been saved in your profile area!");
}
});
});
Upvotes: 1