-1

How do I construct the post data for a jQuery post? This does not work.

var postData = "";
var postInputs = document.querySelectorAll("[id^='itemInput']");
for (var i = 0; i < postInputs .length; i++) {
    var itemName = postInputs [i].name;
    var itemValue = postInputs [i].value ;
    postData += itemName + ": '" + itemValue  + "', ";
 }
 postData += "upload: 'Y'";

$.post("addNewItem.php", {postData}, 
    function(data, status) {
    }
);

And please if you down vote explain why.

UPDATE:

This question was closed and I was pointed to alternatives. However what I have found that works is the following. The post data needs to be a javascript object.

let postData = {}; //Create the object
item['upload'] = 'Y'; //Adding to the object ['key'] and the 'value'
var postInputs = document.querySelectorAll("[id^='itemInput']");
for (var i = 0; i < postInputs .length; i++) {
    item[postInputs[i].name] = postInputs[i].value;
}
$.post("addNewItem.php", item, 
    function(data, status) {
        }
);
RGriffiths
  • 5,722
  • 18
  • 72
  • 120
  • 1
    What is that string? It isn't any standard format. Why do you have your data in that format in the first place? – Quentin Nov 21 '20 at 13:27
  • @Quentin It is an example. The actual code generates a much longer post data string by iterating through a number of elements by id. For simplicity to people to read the question I have written it like this. What format are you suggesting? – RGriffiths Nov 21 '20 at 13:31
  • 1
    Is it a *representative* example? Why are you using that non-standard format? I'm not suggesting any format in particular, I'm asking why you are using *that* one. – Quentin Nov 21 '20 at 13:33
  • @Quentin Again I am unclear what you are getting at. Excuse my ignorance, but what format do you suggest? – RGriffiths Nov 21 '20 at 13:34
  • 1
    I'm not suggesting any particular. I said that. I'm trying to understand the question which you asked giving data in that format. You said something about getting data from elements. Can we ignore the string format you used in the example? If so, what sort of elements are you getting the data from? Are they inputs in a form? – Quentin Nov 21 '20 at 13:36
  • Can you stop editing your question to invalidate the answers you've been given? First you asked why your format didn't work. Then you changed the question to ask what the format should be. Then you changed the question again to ask how to change some code which generates your format into code which generates the correct format. These are all distinct questions with different answers. – Quentin Nov 21 '20 at 13:47
  • @Quentin I edited the question to try and answer your queries about it. The principle remained the same. I appreciate your looking. Will have to try and work it out. – RGriffiths Nov 21 '20 at 13:54
  • @RGriffiths i would suggesting making postData an array `postData = []` and then pushing to this array in the loop `postData.push({itemName: itemName, itemValue: itemValue})`. – Adam Griffith Nov 21 '20 at 14:03
  • @Quentin In caes you are interested I have edited the question with an answer. – RGriffiths Nov 22 '20 at 18:56

1 Answers1

1

jQuery accepts a plain object for the data parameter:

var postData = {
  itemid: '1',
  itemName: 'Foo'
};

$.post("addNewItem.php", postData, function(data, status) {

});
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
Adam Griffith
  • 357
  • 1
  • 13