Forum Moderators: coopster

Message Too Old, No Replies

Dynamically creating textareas, then getting array

         

sbrocks

10:09 pm on Oct 20, 2009 (gmt 0)

10+ Year Member



I'm using javascript to create textareas, but I can't figure out the php for accessing the data that was entered into the dynamically created textareas. I think I'm not understanding the arrays that are being created.
Can anyone help me get my data out of these textareas?
Here is how the textareas are created:
The HTML:

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

TheMadScientist

11:54 pm on Oct 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If I'm understanding correctly, I think you'll have to use JS to get the value, then POST it to your PHP script...

function SaveUserText () {
var userText = "SaveUserText=" + encodeURIComponent( document.getElementById('yourDivID').innerHTML ) +
"&YourAdditionalVariablesForPHP=ValueHere";
makePOSTRequest('/ThePath/ToYourPHPScript.php', userText);
}

sbrocks

3:37 pm on Oct 21, 2009 (gmt 0)

10+ Year Member



Thank you MadScientist, you're bailing me out again. However, I still need help to implement your solution.
I'm not sure where to call the SaveUserText function. It would need to be before POST? Would it need to be in a loop so it can store the text in each of the textareas as they are filled in by the user?
Should I be doing more studying of innerHTML?
Thanks
Sara

sbrocks

4:01 pm on Oct 21, 2009 (gmt 0)

10+ Year Member



Actually, it looks like I may have over-complicated the issue by trying to access all the supports[] as an array. I can gather the text in the textarea by using print support[0], support[1], etc. So, I'm thinking I can build a loop such as-
"until $theText in supports[i] is EMPTY, build the query to insert supports[i] into the table".
I'll post back if that reasoning isn't sound.
Sara

TheMadScientist

7:25 pm on Oct 21, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



@ sbrocks

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

LooInSpain

9:33 am on Oct 26, 2009 (gmt 0)

10+ Year Member



I've done two different types of form like this, wither using the brackets or the unique name elements.

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);

or add them to a database or anything.