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"?
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.
$_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.
$_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.
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.
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_ } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With