this is driving me crazy so any help is much appreciated.
I am posting records with name=opc[1] cat[2] etc. Ideally this would created an array. When I print_r the posted data, it prints what looks like an array though with ticks and slashes around the keys.
However, when I set a variable, $array or whatever equal to the array, print_r($array) returns 0. Echo returns Array.
Possibly related to this, when I try to do foreach on the array, it gives an invalid argument error. What do I need to do to the posted data so that I can set it to a variable and use it as a valid array. I’ve tried various combinations of implode, explode and stripslashes to put it into better form but only the $_POST prints out in print_r. Implode generates an error. Explode creates an Array with just 1 item.
Here is print_r($_POST[‘opc’]);
Array ( [\'0\'] => dog [\'1\'] => cat [\'2\'] => mouse [\'3\'] => chicken [\'4\'] => runner [\'5\'] => )
What do I need to do to work with this?
form code (first two records):
<tr><form action="editcats.php" method="post"><td align="right">Existing:</td><td align="left"><input type="text" size="12" name="ocp[0]" value="dog"><input type="hidden" name="id[0]" value="1"></tr>
<tr><td align="right"></td><td align="left"><input type="text" size="12" name="ocp[1]" value="cat"><input type="hidden" name="id[1]" value="2"><input type="submit" value="submit"></form></tr>
After reviewing your HTML source code, it appears that you are mixing up the method on how to pass an array variable to
$_POST. You shouldn’t have two elements with the same ‘name’ either, even if you wish them to post to the same array. Either rectify your method for entering data into the array variable, or name the elements differently.Remove the indices from your
nameattribute and try running your form again.See this related question for some help as well.