Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all $_POST variables starting with certain text

Tags:

I have an html form with a section that generates inputs with random names.

Each input name is generated with the text "book" at the beginning and random text at the end.

<input type="text" name="book_4552f" /> <input type="text" name="book_3507p" /> <input type="text" name="book_8031b" /> 

How do I use PHP to get all $_POST variables which start with the text "book"?

like image 977
supercoolville Avatar asked Jan 11 '12 18:01

supercoolville


People also ask

What does $post mean in PHP?

PHP $_POST is a PHP super global variable which is used to collect form data after submitting an HTML form with method="post". $_POST is also widely used to pass variables. The example below shows a form with an input field and a submit button.

What is the difference between $post and $_ POST?

$_POST is a superglobal whereas $POST appears to be somebody forgetting the underscore. It could also be a standard variable but more than likely it's a mistake.

What is $_ GET and $_ POST in PHP?

$_GET is an array of variables passed to the current script via the URL parameters. $_POST is an array of variables passed to the current script via the HTTP POST method.

Which variable will be used to pick the value using POST method?

PHP POST method This is the built in PHP super global array variable that is used to get values submitted via HTTP POST method. The array variable can be accessed from any script in the program; it has a global scope.


1 Answers

The following uses strpos() to check that the POST string begins with book_

foreach($_POST as $key => $value) {     if (strpos($key, 'book_') === 0) {         // value starts with book_     } } 
like image 55
drew010 Avatar answered Sep 19 '22 09:09

drew010