0

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!!

2
  • data: "", is empty data is key of ajax object in which you send the data to save_data.php. Commented Feb 6, 2014 at 13:04
  • data keyword stores the values which will be going to send on other page with ajax request you have to define your values in var id='2' and store it in data:"id="id; Commented Feb 6, 2014 at 13:10

4 Answers 4

1

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() },

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Would I have to add hidden inputs for the postID and the userID on the page so I can get those?
1

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!");
            }

        });
    });

6 Comments

No such thing as a "JSON object". You have JSON that represents an object, but it's a string. What you're setting as the value of the data property in that code is a JavaScript object.
A JSON is not a String in any way. It's an object: typeof {foo: 'bar'} == "object"
No, that's incorrect. JSON is JavaScript Object Notation; it's the string representation of an object or array. What you've just described is a JavaScript object, not JSON.
Take a look at json.org/js.html First example uses a variable named myJSONObject. But I don't know, maybe Douglas Crockford doesn't know how to name his variables...
You're right, clearly he doesn't. The term "JSON object" is nonsense.
|
0

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!");
            }

        });
    });

1 Comment

There's no such thing as a "JSON object". JavaScript has objects, JSON is a string that might represent one.
0

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!");
            }

        });
    });

1 Comment

They didn't forget to add it, the whole question is "what should it be?"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.