Part 1: (JS)
This is sort of confusing to explain... I sort of have the first part done actually... The JS is here, the question has been asked a few different ways on StackOverflow (haven't seen the second part of this question around though)... but I can't seem to be getting this for loop working in the JS part of things:
function add_fields() {
var toAdd = document.createDocumentFragment();
for (var i = 1; i < 2; i++) {
var newRow = document.createElement('div');
var inTxtID = 'custom-item-' + i;
var inNumID = 'custom-amount-' + i;
var inTxtName = 'custom_item_' + i;
var inNumName = 'custom_amount_' + i;
newRow.className = 'row';
newRow.innerHTML = '<span><input type="text" placeholder="Item" name="' + inTxtName + '" id="' + inTxtID + '"></span><span><input type="number" placeholder="Amount" name="' + inNumName + '" id="' + inNumID + '"></span>';
document.getElementById('wrapper').appendChild(newRow);
}
document.appendChild(toAdd);
}
My error seems to be with this...
for (var i = 1; i < 2; i++) {
...if I make that middle part i < 10 then it technically works, it just spits out all the inputs at once, which I don't want.
It adds the div's, but I need the id's and name's to be counting up for each new div... not sure how to get that working.
Part 2: (PHP)
On the PHP side of things... The form will obviously post the name, I can catch the names like this:
if (!empty($_POST['custom_item_1']) && !empty($_POST['custom_amount_1'])) {
$receive_custom_item = $_POST['custom_item_1'];
$receive_custom_amount = $_POST['custom_amount_1'];
}
But, let's say there were 3 items/amounts added on the JS side of things, obviously we know that there would have been 3 items/amounts posted that would be available for PHP to pick up... but how do I find out how many items are available?
Pseudo code:
$i == 1;
$custom_amount_check = 'custom_amount_';
$custom_item_check = 'custom_item_';
for each (if ($custom_amount_check + i) exists) {
$receive_custom_item = $_POST['custom_item_1'];
$receive_custom_amount = $_POST['custom_amount_1'];
}
i++;
}
It has to be something like that?
I see a problem with my approach though... If one input is empty, it will stop there, so if someone added 3, and for some reason only filled out the last 2, then it won't read them at all?
I understand I could just not allow more than 10 inputs to be added in the JS, and then just have 1 through 10 !empty()'s in the PHP... but that just feels sloppy to me?
This is also a learning experience for me, so I don't expect anyone to do the work for me, but I would appreciate an explanation for how you think this can be done.
EDIT:
// create custom field variables
$iCheck = 1;
while ((isset($_POST['custom_item_' . $iCheck]) && isset($_POST['custom_amount_' . $iCheck])) || (isset($_GET['ci' . $iCheck]) && isset($_GET['ca' . $iCheck]))) {
if (!empty($_POST['custom_item_' . $iCheck]) && !empty($_POST['custom_amount_' . $iCheck])) {
$receive_custom_item_ . $iCheck = safify($_POST['custom_item_' . $iCheck]);
$receive_custom_amount_ . $iCheck = safify($_POST['custom_amount_' . $iCheck]);
} else if (!empty($_GET['ci' . $iCheck]) && !empty($_GET['ca' . $iCheck])) {
$receive_custom_item_ . $iCheck = safify($_GET['ci' . $iCheck]);
$receive_custom_amount_ . $iCheck = safify($_GET['ca' . $iCheck]);
}
if (isset($receive_custom_item_ . $iCheck) && isset($receive_custom_amount_ . $iCheck)) {
echo '<p>' . $receive_custom_item_ . $iCheck . ' & ' . $receive_custom_amount_ . $iCheck . '</p>';
}
$iCheck++;
}
var ioutside of your function or global, because everytime you add new field it enters function andiis 1$_POSTconsistently and not$POST_$POST_is a valid variable name in PHP but it is not populated by default. If you wanted to use it then you would need to declare$POST_ = $_POST;at the beginning of your PHP code but I do not recommend doing that :)