FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


handling the $_POST array





riv_
OK, so suppose I need a fairly long group of user submitted data from an html form and I want to handle it all as a unit. It's all bundled together in a nice little array, just for me.
I can get the whole array all together. ANd I can get specific values individually. But the keys (names of the input fields, of course) seem to just disappear. But how do I access the key with it's values?
Or all of the keys?
Or all of the values?
Because I want to //do something with them.
I'm beginning to see where some basic OOP can help me build some generic form handling functions to do these sorts of simple things, but I'm just not experienced enought ot see my way through and can use some help. I just need some more flexible, generic functionality.
Please explain, or point me in the right direction.
Thanks in advance!
hexkid
riv_ wrote:
I can get the whole array all together. ANd I can get specific values individually. But the keys (names of the input fields, of course) seem to just disappear. But how do I access the key with it's values?
Or all of the keys?
Or all of the values?


Experiment with print_r() and var_dump(), then write your code.

For example
Code:
echo '<pre>'; print_r($_POST); echo '</pre>';

could return something like
Code:
Array
(
    [name] => example
    [pwd] => elpmaxe
)


Also check all array functions Smile
You might want to start with array_keys().
Aredon
Fully functional example code:
Code:

<pre>
<?php
$_FIELD=array(
"username" => $_POST{username},
"password" => $_POST{password},
"firstname" => $_POST{firstname},
"lastname" => $_POST{lastname},
"age" => $_POST{age}
);

echo print_r($_FIELD);
?>
</pre>
<hr>
<form method="post" action="<?= $_SERVER['PHP_SELF'] ?>">
<p><input type="text" name="username" value="myUser"></p>
<p><input type="password" name="password" value="myPassword"></p>
<p><input type="text" name="firstname" value="myFirstName"></p>
<p><input type="text" name="lastname" value="myLastName"></p>
<p><input type="text" name="age" value="myAge"></p>
<p><input type="text" name="extrafield" value="EXTRAFIELD"></p>
<p><input type="submit" value="POST"></p>
</form>


The above can be expanded upon. Once the variable $_FIELD is setup, you can start messing with the array.
Note: There is nothing magical about $_FIELD.
It's just a neat user-defined name that I picked because it closely resembles $_POST.
array_flip
array_keys
array_values
array_key_exists

To save you from submitting the form over and over while testing, best just work with a static array while debugging.
Code:

$_FIELD=array(
"username" => "myUser",
"password" => "myPassword",
"firstname" => "myFirstName",
"lastname" => "myLastName",
"age" => "myAge"
);
riv_
I like the idea of renaming the whole array. WHy didn't I think of that?
I've been using array_key_exists to look for a hidden field to check if the for m is submitted, and for some basic error checking. Works well.
I've been able to do a few things, but I'm going to go play with my new conviently named array and see what I can come up with...
array_flip... cool

(Any way to name the whole array without entering in every field manually... say, something generic that would work on multiple forms??? just wondering how one would do that?)
hexkid
riv_ wrote:
Any way to name the whole array without entering in every field manually

Code:
<?php
### create a copy of $_POST with all the elements
$new_array = $_POST;
?>


riv_ wrote:
something generic that would work on multiple forms???

foreach()?
Code:
<?php
foreach ($_POST as $k=>$v) {
  if ($k == 'name') {
    echo "The name you entered was $v.<br>\n";
  } else {
    echo "You also entered '$v' for '$k'.<br>\n";
  }
}
?>
Aredon
The answer to your question is:
Code:

$fields=array("username", "password","firstname","lastname","age");
$_FIELD=array_diff_key($_POST, array_diff_key($_POST,array_count_values($fields)));

which is the same as:
Code:

$_FIELD=array(
"username" => $_POST{username},
"password" => $_POST{password},
"firstname" => $_POST{firstname},
"lastname" => $_POST{lastname},
"age" => $_POST{age}
);






To walk you through how it works, for the duration of the explanation $_POST will contain the following:
Code:

$_POST=array(
"username" => "myUser",
"password" => "myPassword",
"firstname" => "myFirstName",
"lastname" => "myLastName",
"age" => "myAge",
"extrafield" => "EXTRAFIELD"
);

and $fields will contain the following:
Code:

$fields=array("username", "password","firstname","lastname","age");




array_count_values is a function that totally ignores the array's keys; it counts the number of occurrences of each value throughout the array.
Code:

array_count_values($fields)

returns:
Code:

array(
"username"=>1,
"password"=>1,
"firstname"=>1,
"lastname"=>1,
"age"=>1
)

array_flip would also work and give you the following:
Code:

array(
"username"=>0,
"password"=>1,
"firstname"=>2,
"lastname"=>3,
"age"=>4
)

moving on.....
array_diff_key is a function that returns a clone of the first array after filtering and removing elements by their keys only if the key is ALSO present in the second array.


In our example:
Code:

array_diff_key($_POST,array_count_values($fields))

will return
Code:

array("extrafield" => "EXTRAFIELD")


Finally:
Code:

array_diff_key($_POST, array("extrafield" => "EXTRAFIELD") )

will return
Code:

array(
"username" => "myUser",
"password" => "myPassword",
"firstname" => "myFirstName",
"lastname" => "myLastName",
"age" => "myAge
);

which is the end result we wanted without "extrafield" nor any other fields not listed in $fields.




At this points it seems to me that I have answered your question.
A final note for the road:
Code:

$_FIELD=array(
"username" => $_POST{username},
"password" => $_POST{password},
"firstname" => $_POST{firstname},
"lastname" => $_POST{lastname},
"age" => $_POST{age}
);

is better than:
Code:

$fields=array("username", "password","firstname","lastname","age");
$_FIELD=array_diff_key($_POST, array_diff_key($_POST,array_count_values($fields)));

because it is much faster.

Readability and style are just as important if-not more to enable the programmer to understand at a glance what they wrote so they can carry on programming -- not to mention when posting each of the field names twice, one does tend to feel like a robot. Very Happy
The choice is yours; which to use depends on your fancy.
riv_
Aredon,
You are my hero Shocked
You have not only given me some insight into manipulating arrays, but you've also given me the neccessary advice. You've given me the code, and explained it (and I've learned a lot) but have not left me to go around randomly applying it.
I really need the benefit of expereince that I have not yet acquired for myself.
I am continually asking, "can it be done like this?", "What would this look like?" "Which of these 3 methods is best and why?"
I desparately need this sort of patient walk through.
Please feel free to keep it coming!
Related topics
php sessions
[HELP] script PHP
Java tutorials
PHP: Writing to txt file
Php form.
PHP Dropdown Selection
Need help with submitting form
Mail() function and SMTP
File_get_contents and url query strings
PHP login script problem
help plz
Php $_POST
PHP load array from $_POST
<input> MUST be in <form>?
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.