Forum Moderators: coopster
<table border='1' cellspacing='2' cellpadding='5' width='640'>
<tr><td valign='top'><b>Behavior Supports:</td><td valign='top'><b>Who will do what/backup staff:</td></tr>
<td valign='Top'>
<?= $REQUIRED ?>Support Plan 1
<div id="divSupport">
<textarea name="supports[]" id="1" rows="5" cols="35"><?php echo htmlentities($support); ?></textarea>
</div>
<br><br>
</td>
<td valign='Top'>
<?= $REQUIRED ?>Backup 1
<div id="divWho">
<textarea name="supportWho[]" id="1" rows="5" cols="35"><?php echo htmlentities($supportWho); ?></textarea>
</div>
<input type="button" value="Add another support plan" onClick="addTextArea();">
</td>
</tr>
</td></tr>
</table>
The javascript function that is called:
<script type="text/javascript">
var counter = 1;
var limit = 6;
function addTextArea() {
if (counter == limit-1) {
alert("You have reached the limit of adding " + counter + " support plans");
return false;
}
else {
var newdiv = document.createElement('div');
newdiv.innerHTML = "Support Plan " + (counter + 1) + " <br><textarea name='supports[]' id=counter rows='5' cols='35'>";
document.getElementById('divSupport').appendChild(newdiv);
var newdiv = document.createElement('div');
newdiv.innerHTML = "Backup " + (counter + 1) + " <br><textarea name='supportWho[]' id=counter rows='5' cols='35'>";
document.getElementById('divWho').appendChild(newdiv);
counter++
return true;
}
}</script>
I've tried every version of foreach I can think of to get what's IN the textareas OUT so that I can send it to my database, but there's something I obviously don't understand. This gives an "Invalid argument supplied for foreach()" error, then tells me the array is empty.
if (!EMPTY($_POST['supports'])) {
foreach($_POST['supports'] as $value)
{
print $value ;
}
echo "it's empty";
}
Thank you!
function SaveUserText () {
var userText = "SaveUserText=" + encodeURIComponent( document.getElementById('yourDivID').innerHTML ) +
"&YourAdditionalVariablesForPHP=ValueHere";
makePOSTRequest('/ThePath/ToYourPHPScript.php', userText);
}
I have read a post here and was recently asked to fix a form which was function correctly and then suddenly stopped working with no changes... I was lucky to have read the post here, because according to the PHP documentation passing an array from a form as name="variable[]" is perfectly valid, but unfortunately it was either not passing or not being received correctly for some reason. (I'm not sure which it was... The passing or receiving.)
To get the form I was asked to fix to work I had to switch from variable[] to unique names EG variable_1 and so on, then loop them together in the PHP script....
I did not notice that portion of your code previously, so it might be a good thing you pointed it out. JS is still the way to go to get the info someone enters, but as far as passing values I would use unique names throughout the form rather than arrays. I personally don't like passing arrays into PHP via form, so I have always used unique names and PHP to manipulate them if necessary, so this is not something I have run into until recently. I do not know if it's a bug in a newer version of PHP or if something else changed, but either way, passing arrays via POST submission is definitely 'out' right now in some settings for some reason.
I actually played around with the form I was asked to fix for about an hour trying to access the information within the arrays, because according to the documentation the form was completely valid and I wanted to see if I could track down the issue...
All I could get out of PHP was basically:
Array( Array( Array( Array (
Without the entered information within any of them...
If you number the first form ie supportWho[+ counter +] you can then use the foreach loop to grab each element and do as you like
$supportWho = $_POST["supportWho"][$i].
For the second type, if you number the variables in ascending order, you can then loop them like:
$array = array();
$i = 1 ;
while ($_POST['variable_'.$i]) {
$array[] = $_POST['variable_'.$i];
$i++;
}
print_r($array);