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
You're using PHP, so let us name themLet's name them
menu_category_1
,menu_category_2
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.
Normalise your database. Express your one-to-many relationships using a foreign key and a second table, not by storing CSV data.which is going to be imploded and then update my database
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
Are There Any PHP Docblock Parser Tools Available
Converting Nsarray -> JSON -> Nsdata -> PHP Server ->JSON Representation
PHP MySQL Query Where X = $Variable
How to Add Custom Fields to Woocommerce Registration Form
How to Remove Text Between Tags in PHP
How to Prevent PHP Script Running More Than Once
Check If a String Is All Caps in PHP
Find in Set in Laravel? Example
How to Rename a Filename After Uploading with PHP
PHP - Ini_Set('Session.Gc_Maxlifetime', 5) - Why It Doesn't End the Session
Deny Direct Download of File Using PHP
How to Display HTML to the Browser Incrementally Over a Long Period of Time
Check If the Current Date Is Between Two Dates + MySQL Select Query
Create a Joomla! Article Programmatically
Php: How to Read a File Live That Is Constantly Being Written To
How to Convert Arabic Characters to Unicode Using PHP
Set Maximum Execution Time for Exec() Specifically
Maximum Function Nesting Level of '100' Reached, Aborting After Upgrading to Laravel 5.1