|
Ringlet to get all the variables for POST in PHP |
|
|
|
A very rapid way of receiving all the variables of a form, sent for post. By means of a generic trip of the array $_POST, in the language PHP. We are going to see a very rapid way of receiving all the variables of a form, sent for post, in the language PHP. I make sure you that it is a small portion of code that will save you to write a heap of lines of code.
Who has not ever met in the tedious task of receiving a heap of information of a form, assigning one for one all the variables in PHP? That was done by lines like this one:
$nombre = $_POST ["name"];
$edad = $_POST ["age"];
$ciudad = $_POST ["city"];
....
If the form had 10 elements it would not be very heavy to write 10 code lines, but if there were 50 or 100 the thing would be much less agreeable. The code that we are going to see now will solve the life to us in these cases.
foreach ($_POST grasp $nombre_campo => $valor) {
$asignacion = "\$". $nombre_campo. "= '". $valor. "';";
eval ($asignacion);
}
It realizes a ringlet foreach that each of the elements is covering of the post. In every iteration, they are gaining access to all the elements of the post and one keeps in $ nombre_campo the name of the field got for the form and in $valor, the value that had got in the form.
Everything previous is deduced of the first line. In the following ones it consists in every iteration, each of the allocations that we should have written manually. Namely in the variable allocation we will keep a line of code PHP that realizes the declaration of the variable of form inside PHP and his initialization with the value that had been written.
In the following line, where the function is eval (), there is executed the judgment generated in the previous step. The function eval () of PHP executes the content of a chain of characters as if it was a judgment PHP. (We can see the papers of the function eval () on the PHP page http://es.php.net/manual/es/function.eval.php)
We hope that you should have been interested in this one minuscule, but useful, code PHP.
Miguel Angel Alvarez
http://www.desarrolloweb.com/articulos/1326.php?manual=6 |