Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP access all $_POST[] variables into an array?

Tags:

arrays

oop

post

php

how to capture all fields in the $_POST VARIABLE? into an array?

$email = $_POST;
$emails = array_keys($email);
foreach($emails as $email) {
        echo '$' . nl2br($email);
}
$emailadd = new email_save;
$emailadd->insert_email_into_database("POST VARIABLES GO HERE)

I'm trying to make an email list, name and email address, how do I capture all the variables that where posted that can normal be accessed like $_POST['email'] into an array so i can add them to the arguments in my functions?

My form field has 5 fields. The method is POST.

Instead of writing,

$email = mysql_real_escape_string($_POST['email']);
$firstname = mysql_real_escape_string($_POST['firstname']);

I'm trying to access all the values in the $_POST[] array; So I can save time getting posted variables and add functions to those variables like mysql_real_escape_string();

Other security measures will be figured out.

I want to be able to add a reffrence of the array to a function so I can add them to the database.

like image 857
Aaron Avatar asked Mar 17 '11 22:03

Aaron


2 Answers

If you want to capture a list from a POSTed form, then use the array syntax trick instead of enumerated input field names:

<input type="email" name="emails[]">
<input type="email" name="emails[]">
<input type="email" name="emails[]">

This way you need no guessing in PHP, because emails[] becomes an array implicitely then:

print_r($_POST["emails"]);
foreach ($_POST["emails"] as $email) {

For database-escaping just use:

$db_emails = array_map("mysql_real_escape_string", $_POST["emails"]);
// that's an array too
like image 137
mario Avatar answered Sep 28 '22 07:09

mario


$_POST is already an array. Why not just pass that?

like image 43
SimplyZ Avatar answered Sep 28 '22 06:09

SimplyZ