Home > Backend Development > PHP Tutorial > How to Capture Multiple Checkbox Selections as an Array in PHP?

How to Capture Multiple Checkbox Selections as an Array in PHP?

Barbara Streisand
Release: 2024-12-12 21:23:18
Original
297 people have browsed it

How to Capture Multiple Checkbox Selections as an Array in PHP?

PHP Multiple Checkbox Array

Creating a form with multiple checkboxes and capturing user selections into an array in PHP can be straightforward. Here's how you can implement it:

<form method='post'>
Copy after login

Now, in your PHP script:

<?php
if (isset($_POST['checkboxvar'])) {
  print_r($_POST['checkboxvar']);
}
?>
Copy after login

By passing the name attribute of your checkbox inputs as an array (checkboxvar[]), all checked values will be automatically captured within the $_POST['checkboxvar'] array. You can then echo or manipulate these values as needed.

To send the checked values in an email, you can implode the array into a string, separating the values with a comma or any other separator:

echo implode(',', $_POST['checkboxvar']); // change the comma to your desired separator
Copy after login

Remember to sanitize your input before using it in your email to prevent any malicious code from being executed. For more information on this topic, you can refer to the official PHP documentation here: http://php.net/manual/en/faq.html.php#faq.html.arrays

The above is the detailed content of How to Capture Multiple Checkbox Selections as an Array in PHP?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template