I have the code below and I’m doing it wrong. I need it to get the items from the form field ‘standardname[]’ and send it to PHP via ajax.
Here is my code:
function standardSave() {
//get the data from the form field-its a text input array
var names=document.getElementsByName('standardname[]');
//for each item in the array
for(key=0; key < names.length; key++) {
//combine the items in this 'toadd' array
var toadd = { "standard[key]" : "names[key].value" };
//send to PHP via ajax
$.ajax({
type: "post",
url: "draftsave.php",
data: toadd,
success: function(result) {
}
});
//empty the var
var toadd = [];
};
</script>
I think I just need help with this line:
var toadd = { "standard[key]" : "names[key].value" };
I just don’t know what the correct method is.
Thanks!
2
Answers
let me know if this works. I can’t put "standard[key]" in key of toadd array for some reason. i want key to be dynamically the one you provide in the for loop. also i don’t know what standard is, but I’m assuming it’s a string. you can look up other methods for it or change your implementation of the toadd array key.
anyways this should work. I checked it and this is sending values to the PHP file one by one, assuming each input field has name of "standardname[]"
Here’s, The complete example of form submit with ajax request.