简体   繁体   中英

Saving data to the database via JQuery .ajax

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

data should be a JSON object containing the data you want to save, ie

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

        });
    });

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

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

        });
    });

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

        });
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM