How to Get an Array of Data from $_Post

How to get an array of data from $_POST

Using [] in the element name

Email 1:<input name="email[]" type="text"><br>
Email 2:<input name="email[]" type="text"><br>
Email 3:<input name="email[]" type="text"><br>

will return an array on the PHP end:

$email = $_POST['email'];   

you can implode() that to get the result you want:

echo implode(", ", $email); // Will output email1@email.com, email2@email.com ...

Don't forget to sanitize these values before doing anything with them, e.g. serializing the array or inserting them into a database! Just because they're in an array doesn't mean they are safe.

Retrieve post array values

You have [] within your $_POST variable - these aren't required. You should use:

$qty = $_POST['qty'];

Your code would then be:

$qty = $_POST['qty'];

foreach($qty as $value) {

$qtyOut = $value . "<br>";

}

Make array from $_POST values

Let's name them menu_category_1, menu_category_2

You're using PHP, so let us name them menu_category[0], menu_category[1], etc (or menu_category[], menu_category[], etc) instead.

Then $_POST['menu_category'] or $_GET['menu_category'] will be an array.

which is going to be imploded and then update my database

Normalise your database. Express your one-to-many relationships using a foreign key and a second table, not by storing CSV data.

PHP Get Values from POST Array

use array index for name like

$fname = $_POST['name'][0];
$lname = $_POST['name'][1];

and use implode statement for the address like

implode(" ",$_POST['address']) (it will convert your address array to string)

$_POST Array from html form

Change

$info=$_POST['id[]'];

to

$info=$_POST['id'];

by adding [] to the end of your form field names, PHP will automatically convert these variables into arrays.

Posting array from form

Give each input a name in array format:

<input type="hidden" name="data[EstPriceInput]" value="" />

Then the in PHP $_POST['data']; will be an array:

  print_r($_POST);         // print out the whole post
print_r($_POST['data']); // print out only the data array

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

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


Related Topics



Leave a reply



Submit