Click to See Complete Forum and Search --> : $_POST returns array
Gorbo
October 16th, 2008, 01:06 AM
One more question i need to make an array of items i type in a form so i used $_POST
but it works strangely it puts the entire array in pos 0
so arr has the entire "4","6","aga","aza","goo","1" in arr[0] but arr2 which is hard coded works fine why is that?
$arr = array($_POST['pname']);
$arr2 = array("4","6","aga","aza","goo","1");
PeejAvery
October 16th, 2008, 06:36 AM
[ split thread ]
PeejAvery
October 16th, 2008, 06:41 AM
$_POST always returns an associative array based on the names of the HTML elements passed in the form. You don't need to put it into one.
If you put and array as a value of an array (your first example of code) it will make it a two-dimensional array. That is how it is supposed to work.
$pname = $_POST['pname'];
Gorbo
October 16th, 2008, 09:36 AM
Sorry for the split thread thing and thanks for your answer i understand now how it works but it doesn't solve my problem.
i want to receive things from the form to an array so that if i wrote a list say 1,2,3,4,5 i will get an array of 12345 one in each cell but $_POST seems to put each character to each cell so instead i get 1,2,3 as my array
PeejAvery
October 16th, 2008, 12:28 PM
You're not making sense. Perhaps you can post more of your code to the form you are submitting.
Let me see if I can clear this up, and then you can explain in more detail what you are attempting.
$_POST is an associative array. Its members come from all <input> and <select> HTML tags of a given form. The keys correspond the the tags name attribute.
For example...
<form ...>
<input type="hidden" name="id" value="42" />
<input type="text" name="firstname" value="Jose" />
<input type="text" name="lastname" value="Fulano" />
</form>
Will produce the following server-side...
array (
"id" => 42,
"firstname" => Jose,
"lastname" => Fulano
)
Gorbo
October 16th, 2008, 02:39 PM
Yeah i got that from your previous post but in my case instead of having just one input in the form like jose i have a list like this
red,black,blue,green
and i want to be able to have that list in an array so i can get the items one by one but it seems that $_post takes that list as one item
now for what i'm trying to do
i'm using a loop to write
<li><a href="1.php">Main</a>
severl times it's a menu so i need all the pages so i'm using the counter from the loop to fill the page name like so
<li><a href="$count.php>Main</a>
this is done in separate parts of course and then connected together but instead of main i need the names for the pages so i want to be able to put a list in a form and then an array and just access the list items from the array using the loop like so
$arr[$count];
i hope i explained well (i'm pretty bad at explanations..)
mmetzger
October 16th, 2008, 03:01 PM
If I understand you correctly, you're passing a comma separated list of values as an entry for a given key. When accessing it via $_POST, you get that list back, but you want an array.
If that's the case, access the value via $_POST and pass it through split. This will give you a further array that you can access by element number.
http://www.php.net/split
If you want something different, you may need to change how you're POSTing the data, or will need to process it afterwards.
PeejAvery
October 16th, 2008, 03:55 PM
I wouldn't use split() unless you want to use regular expressions. In this case, I would use explode() (http://us2.php.net/manual/en/function.explode.php).
Gorbo
October 16th, 2008, 10:18 PM
Thanks a lot both of you now it works as it should
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.