{"id":1649,"date":"2021-05-14T07:07:27","date_gmt":"2021-05-14T07:07:27","guid":{"rendered":"https:\/\/phptutorial.net\/?page_id=1649"},"modified":"2025-04-07T14:24:51","modified_gmt":"2025-04-07T14:24:51","slug":"php-multiple-checkboxes","status":"publish","type":"page","link":"https:\/\/www.phptutorial.net\/php-tutorial\/php-multiple-checkboxes\/","title":{"rendered":"PHP Multiple Checkboxes"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn how to handle a form with multiple checkboxes in PHP.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='how-to-handle-multiple-checkboxes-on-a-form'>How to handle multiple checkboxes on a form <a href=\"#how-to-handle-multiple-checkboxes-on-a-form\" class=\"anchor\" id=\"how-to-handle-multiple-checkboxes-on-a-form\" title=\"Anchor for How to handle multiple checkboxes on a form\">#<\/a><\/h2>\n\n\n\n<p>A form may contain multiple <a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-checkbox\/\">checkboxes<\/a> with the same name. When you submit the form, you&#8217;ll receive multiple values on the server under one name. <\/p>\n\n\n\n<p>To get all the values from the checked checkboxes, you need to add the square brackets (<code>[]<\/code>) after the name of the checkboxes. <\/p>\n\n\n\n<p>When PHP sees the square brackets (<code>[]<\/code>) in the field name, it&#8217;ll create an <a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-associative-arrays\/\">associative array<\/a> of values where the key is the checkbox&#8217;s name and the values are the selected values.<\/p>\n\n\n\n<p>The following example shows a form that consists of three checkboxes with the same name (<code>colors[]<\/code>) with different values <code>\"red\"<\/code>, <code>\"green\"<\/code>, and <code>\"blue\"<\/code>.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">&lt;form action=<span class=\"hljs-string\">\"index.php\"<\/span> method=<span class=\"hljs-string\">\"post\"<\/span>&gt;\n\t&lt;input type=<span class=\"hljs-string\">\"checkbox\"<\/span> name=<span class=\"hljs-string\">\"colors&#91;]\"<\/span> value=<span class=\"hljs-string\">\"red\"<\/span> id=<span class=\"hljs-string\">\"color_red\"<\/span> \/&gt;\n\t&lt;label <span class=\"hljs-keyword\">for<\/span>=<span class=\"hljs-string\">\"color_red\"<\/span>&gt;Red&lt;\/label&gt;\n\n\t&lt;input type=<span class=\"hljs-string\">\"checkbox\"<\/span> name=<span class=\"hljs-string\">\"colors&#91;]\"<\/span> value=<span class=\"hljs-string\">\"green\"<\/span> id=<span class=\"hljs-string\">\"color_green\"<\/span> \/&gt;\n\t&lt;label <span class=\"hljs-keyword\">for<\/span>=<span class=\"hljs-string\">\"color_red\"<\/span>&gt;Green&lt;\/label&gt;\n\n\t&lt;input type=<span class=\"hljs-string\">\"checkbox\"<\/span> name=<span class=\"hljs-string\">\"colors&#91;]\"<\/span> value=<span class=\"hljs-string\">\"blue\"<\/span> id=<span class=\"hljs-string\">\"color_blue\"<\/span> \/&gt;\n\t&lt;label <span class=\"hljs-keyword\">for<\/span>=<span class=\"hljs-string\">\"color_red\"<\/span>&gt;Blue&lt;\/label&gt;\n        &lt;input type=<span class=\"hljs-string\">\"submit\"<\/span> value=<span class=\"hljs-string\">\"Submit\"<\/span>&gt;\n&lt;\/form&gt;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>When you check three checkboxes and submit the form, the <code>$_POST['colors']<\/code> will contain an array of three selected values:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">array(3) \n{ \n    &#91;0]=&gt; string(3) \"red\" \n    &#91;1]=&gt; string(5) \"green\" \n    &#91;2]=&gt; string(4) \"blue\" \n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>If you don&#8217;t check any checkboxes and submit the form, the <code>$_POST<\/code> array won&#8217;t have the <code>colors<\/code> key. And you can use the <code>isset()<\/code> function to check if the <code>$_POST['colors']<\/code> is set:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-keyword\">isset<\/span>($_POST&#91;<span class=\"hljs-string\">'colors'<\/span>])<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Alternatively, you can use the <code><a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-filter_has_var\/\">filter_has_var()<\/a><\/code> function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">filter_has_var(INPUT_POST, <span class=\"hljs-string\">'colors'<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='php-multiple-checkboxes-example'>PHP multiple checkboxes example <a href=\"#php-multiple-checkboxes-example\" class=\"anchor\" id=\"php-multiple-checkboxes-example\" title=\"Anchor for PHP multiple checkboxes example\">#<\/a><\/h2>\n\n\n\n<p>We&#8217;ll create a <a href=\"https:\/\/phptutorial.net\/app\/multiplecheckboxes\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">simple app that allows users to order pizza toppings<\/a>.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"490\" height=\"435\" src=\"https:\/\/phptutorial.net\/wp-content\/uploads\/2021\/05\/php-multiple-checkboxes.png\" alt=\"PHP multiple checkboxes example\" class=\"wp-image-1668\" srcset=\"https:\/\/www.phptutorial.net\/wp-content\/uploads\/2021\/05\/php-multiple-checkboxes.png 490w, https:\/\/www.phptutorial.net\/wp-content\/uploads\/2021\/05\/php-multiple-checkboxes-300x266.png 300w\" sizes=\"auto, (max-width: 490px) 100vw, 490px\" \/><\/figure>\n<\/div>\n\n\n<p>First, create the following file and directory structure:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">.\n\u251c\u2500\u2500 css\n\u2502   \u2514\u2500\u2500 style.css\n\u251c\u2500\u2500 img\n\u2502   \u2514\u2500\u2500 pizza.svg\n\u251c\u2500\u2500 inc\n\u2502   \u251c\u2500\u2500 .htaccess\n\u2502   \u251c\u2500\u2500 footer.php\n\u2502   \u251c\u2500\u2500 functions.php\n\u2502   \u251c\u2500\u2500 get.php\n\u2502   \u251c\u2500\u2500 header.php\n\u2502   \u2514\u2500\u2500 post.php\n\u2514\u2500\u2500 index.php<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>File<\/th><th>Directory<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td>index.php<\/td><td>.<\/td><td>Contain the main logic that loads get.php or post.php depending on the HTTP request method<\/td><\/tr><tr><td>get.php<\/td><td>inc<\/td><td>Contain the code for showing a form with a checkbox when the HTTP request is GET.<\/td><\/tr><tr><td>post.php<\/td><td>inc<\/td><td>Contain the code for handling POST request<\/td><\/tr><tr><td>header.php<\/td><td>inc<\/td><td>Contain the code for the header<\/td><\/tr><tr><td>footer.php<\/td><td>inc<\/td><td>Contain the code for the footer<\/td><\/tr><tr><td>functions.php<\/td><td>inc<\/td><td>Contain the common functions<\/td><\/tr><tr><td>.htaccess<\/td><td>inc<\/td><td>Prevent direct access to the files in the inc directory<\/td><\/tr><tr><td>style.css<\/td><td>css<\/td><td>Contain the CSS code<\/td><\/tr><tr><td>pizza.svg<\/td><td>img<\/td><td>The pizza image that shows on the form<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Second, add the following code to the <code>header.php<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-meta\">&lt;!DOCTYPE <span class=\"hljs-meta-keyword\">html<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">html<\/span> <span class=\"hljs-attr\">lang<\/span>=<span class=\"hljs-string\">\"en\"<\/span>&gt;<\/span>\n\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">head<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">meta<\/span> <span class=\"hljs-attr\">charset<\/span>=<span class=\"hljs-string\">\"UTF-8\"<\/span> \/&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">meta<\/span> <span class=\"hljs-attr\">name<\/span>=<span class=\"hljs-string\">\"viewport\"<\/span> <span class=\"hljs-attr\">content<\/span>=<span class=\"hljs-string\">\"width=device-width, initial-scale=1.0\"<\/span> \/&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">link<\/span> <span class=\"hljs-attr\">rel<\/span>=<span class=\"hljs-string\">\"preconnect\"<\/span> <span class=\"hljs-attr\">href<\/span>=<span class=\"hljs-string\">\"https:\/\/fonts.gstatic.com\"<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">link<\/span> <span class=\"hljs-attr\">href<\/span>=<span class=\"hljs-string\">\"https:\/\/fonts.googleapis.com\/css2?family=Nanum+Gothic+Coding:wght@400;700&amp;display=swap\"<\/span> <span class=\"hljs-attr\">rel<\/span>=<span class=\"hljs-string\">\"stylesheet\"<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">link<\/span> <span class=\"hljs-attr\">rel<\/span>=<span class=\"hljs-string\">\"stylesheet\"<\/span> <span class=\"hljs-attr\">href<\/span>=<span class=\"hljs-string\">\"css\/style.css\"<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">title<\/span>&gt;<\/span>PHP Multiple Checkboxes - Pizza Toppings<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">title<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">head<\/span>&gt;<\/span>\n\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">body<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"center\"<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">main<\/span>&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">img<\/span> <span class=\"hljs-attr\">src<\/span>=<span class=\"hljs-string\">\"img\/pizza.svg\"<\/span> <span class=\"hljs-attr\">height<\/span>=<span class=\"hljs-string\">\"72\"<\/span> <span class=\"hljs-attr\">width<\/span>=<span class=\"hljs-string\">\"72\"<\/span> <span class=\"hljs-attr\">title<\/span>=<span class=\"hljs-string\">\"Pizza Toppings\"<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Note that the page uses the Nanum Gothic Coding font. The <code>header.php<\/code> links to the style.css file. At the beginning of the body, it shows the <code>pizza.svg<\/code> image.<\/p>\n\n\n\n<p>Third, add the following code to the <code>footer.php<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">main<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">body<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">html<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>footer.php<\/code> file contains the closing tags corresponding to the opening tags in the <code>header.php<\/code> file.<\/p>\n\n\n\n<p>Fourth, define a function checked in the <code>functions.php<\/code> file:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">checked<\/span><span class=\"hljs-params\">($needle, $haystack)<\/span>\n<\/span>{\n\t<span class=\"hljs-keyword\">if<\/span> ($haystack) {\n\t\t<span class=\"hljs-keyword\">return<\/span> in_array($needle, $haystack) ? <span class=\"hljs-string\">'checked'<\/span> : <span class=\"hljs-string\">''<\/span>;\n\t}\n\n\t<span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">''<\/span>;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>checked()<\/code> function returns the string <code>'checked'<\/code> if the <code>$need<\/code> exists in the array <code>$haystack<\/code> or an empty string otherwise.<\/p>\n\n\n\n<p>We&#8217;ll use this <code>checked()<\/code> function to refill the selected checkboxes on the form.<\/p>\n\n\n\n<p>Fifth, add the following code to the index.php:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\nsession_start();\n\n<span class=\"hljs-keyword\">require<\/span> <span class=\"hljs-string\">'inc\/header.php'<\/span>;\n\n<span class=\"hljs-keyword\">require<\/span> <span class=\"hljs-string\">'inc\/functions.php'<\/span>;\n\n$pizza_toppings = &#91;\n\t<span class=\"hljs-string\">'pepperoni'<\/span> =&gt; <span class=\"hljs-number\">0.5<\/span>,\n\t<span class=\"hljs-string\">'mushrooms'<\/span> =&gt; <span class=\"hljs-number\">1<\/span>,\n\t<span class=\"hljs-string\">'onions'<\/span> =&gt; <span class=\"hljs-number\">1.5<\/span>,\n\t<span class=\"hljs-string\">'sausage'<\/span> =&gt; <span class=\"hljs-number\">2.5<\/span>,\n\t<span class=\"hljs-string\">'bacon'<\/span> =&gt; <span class=\"hljs-number\">1.0<\/span>,\n];\n\n$request_method = $_SERVER&#91;<span class=\"hljs-string\">'REQUEST_METHOD'<\/span>];\n\n<span class=\"hljs-keyword\">if<\/span> ($request_method === <span class=\"hljs-string\">'GET'<\/span>) {\n\t<span class=\"hljs-keyword\">require<\/span> <span class=\"hljs-string\">'inc\/get.php'<\/span>;\n} <span class=\"hljs-keyword\">elseif<\/span> ($request_method === <span class=\"hljs-string\">'POST'<\/span>) {\n\t<span class=\"hljs-keyword\">require<\/span> <span class=\"hljs-string\">'inc\/post.php'<\/span>;\n}\n\n<span class=\"hljs-keyword\">require<\/span> <span class=\"hljs-string\">'inc\/footer.php'<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How the index.php works.<\/p>\n\n\n\n<p>The index.php calls the <code>session_start()<\/code> function to start (or resume) a session. It loads the code from the header.php, functions.php, and footer.php.<\/p>\n\n\n\n<p>The <code>$pizza_toppings<\/code> array stores the pizza toppings and prices. In a real application, you may get it from a database or an API.<\/p>\n\n\n\n<p>If the HTTP method is GET, the index.php loads the form from the get.php file. In case the HTTP method is POST, it loads the code from the post.php file.<\/p>\n\n\n\n<p>Sixth, create a form in the <code>get.php<\/code> file:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">&lt;form action=<span class=\"hljs-string\">\"&lt;?php echo htmlspecialchars($_SERVER&#91;'PHP_SELF']) ?&gt;\"<\/span> method=<span class=\"hljs-string\">\"post\"<\/span>&gt;\n\n    &lt;h1&gt;Please select your pizza toppings:&lt;\/h1&gt;\n    &lt;ul&gt;\n        <span class=\"hljs-meta\">&lt;?php<\/span> <span class=\"hljs-keyword\">foreach<\/span> ($pizza_toppings <span class=\"hljs-keyword\">as<\/span> $topping =&gt; $price) : <span class=\"hljs-meta\">?&gt;<\/span>\n            &lt;li&gt;\n                &lt;div&gt;\n                    &lt;input type=<span class=\"hljs-string\">\"checkbox\"<\/span> name=<span class=\"hljs-string\">\"pizza_toppings&#91;]\"<\/span> value=<span class=\"hljs-string\">\"&lt;?php echo $topping ?&gt;\"<\/span> id=<span class=\"hljs-string\">\"pizza_topping_&lt;?php echo $topping ?&gt;\"<\/span> <span class=\"hljs-meta\">&lt;?php<\/span> <span class=\"hljs-keyword\">echo<\/span> checked($topping, $_SESSION&#91;<span class=\"hljs-string\">'selected_toppings'<\/span>] ?? &#91;]) <span class=\"hljs-meta\">?&gt;<\/span> \/&gt;\n                    &lt;label <span class=\"hljs-keyword\">for<\/span>=<span class=\"hljs-string\">\"pizza_topping_&lt;?php echo $topping ?&gt;\"<\/span>&gt;<span class=\"hljs-meta\">&lt;?php<\/span> <span class=\"hljs-keyword\">echo<\/span> ucfirst($topping) <span class=\"hljs-meta\">?&gt;<\/span>&lt;\/label&gt;\n                &lt;\/div&gt;\n                &lt;span&gt;<span class=\"hljs-meta\">&lt;?php<\/span> <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">'$'<\/span> . $price <span class=\"hljs-meta\">?&gt;<\/span>&lt;\/span&gt;\n            &lt;\/li&gt;\n        <span class=\"hljs-meta\">&lt;?php<\/span> <span class=\"hljs-keyword\">endforeach<\/span> <span class=\"hljs-meta\">?&gt;<\/span>\n    &lt;\/ul&gt;\n\n    &lt;button type=<span class=\"hljs-string\">\"submit\"<\/span>&gt;Order Now&lt;\/button&gt;\n&lt;\/form&gt;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The get.php file uses the <code>$pizza_toppings<\/code> array to dynamically generate checkboxes. The <code>checked()<\/code> function checks the checkbox if the value exists in the <code>$_SESSION['selected_toppings']<\/code> variable. <\/p>\n\n\n\n<p>When the page loads for the first time, the <code>$_SESSION['selected_toppings']<\/code> is always empty. Later, we&#8217;ll add the selected values to it in the <code>post.php<\/code>.<\/p>\n\n\n\n<p>Finally, place the code to handle form submission in the <code>post.php<\/code> file:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n\n<span class=\"hljs-keyword\">if<\/span> (filter_has_var(INPUT_POST, <span class=\"hljs-string\">'pizza_toppings'<\/span>)) {\n    <span class=\"hljs-comment\">\/\/ get the pizza toppings from the form<\/span>\n    $selected_toppings = filter_input(INPUT_POST, <span class=\"hljs-string\">'pizza_toppings'<\/span>,  FILTER_DEFAULT, FILTER_REQUIRE_ARRAY) ?? &#91;];\n\n    <span class=\"hljs-comment\">\/\/ select the topping names<\/span>\n    $toppings = array_keys($pizza_toppings);\n\n    $_SESSION&#91;<span class=\"hljs-string\">'selected_toppings'<\/span>] = &#91;]; <span class=\"hljs-comment\">\/\/ for storing selected toppings<\/span>\n    $total = <span class=\"hljs-number\">0<\/span>; <span class=\"hljs-comment\">\/\/ for storing total<\/span>\n\n    <span class=\"hljs-comment\">\/\/ check data against the original values<\/span>\n    <span class=\"hljs-keyword\">if<\/span> ($selected_toppings) {\n        <span class=\"hljs-keyword\">foreach<\/span> ($selected_toppings <span class=\"hljs-keyword\">as<\/span> $topping) {\n            <span class=\"hljs-keyword\">if<\/span> (in_array($topping, $toppings)) {\n                $_SESSION&#91;<span class=\"hljs-string\">'selected_toppings'<\/span>]&#91;] = $topping;\n                $total += $pizza_toppings&#91;$topping];\n            }\n        }\n    } \n    \n}\n<span class=\"hljs-meta\">?&gt;<\/span>\n\n\n<span class=\"hljs-meta\">&lt;?php<\/span> <span class=\"hljs-keyword\">if<\/span> ($_SESSION&#91;<span class=\"hljs-string\">'selected_toppings'<\/span>]) : <span class=\"hljs-meta\">?&gt;<\/span>\n    &lt;h1&gt;Order Summary&lt;\/h1&gt;\n    &lt;ul&gt;\n        <span class=\"hljs-meta\">&lt;?php<\/span> <span class=\"hljs-keyword\">foreach<\/span> ($_SESSION&#91;<span class=\"hljs-string\">'selected_toppings'<\/span>] <span class=\"hljs-keyword\">as<\/span> $topping) : <span class=\"hljs-meta\">?&gt;<\/span>\n            &lt;li&gt;\n                &lt;span&gt;<span class=\"hljs-meta\">&lt;?php<\/span> <span class=\"hljs-keyword\">echo<\/span> ucfirst($topping) <span class=\"hljs-meta\">?&gt;<\/span>&lt;\/span&gt;\n                &lt;span&gt;<span class=\"hljs-meta\">&lt;?php<\/span> <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">'$'<\/span> . $pizza_toppings&#91;$topping] <span class=\"hljs-meta\">?&gt;<\/span>&lt;\/span&gt;\n            &lt;\/li&gt;\n        <span class=\"hljs-meta\">&lt;?php<\/span> <span class=\"hljs-keyword\">endforeach<\/span> <span class=\"hljs-meta\">?&gt;<\/span>\n\n        &lt;li <span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span>=\"<span class=\"hljs-title\">total<\/span>\"&gt;&lt;<span class=\"hljs-title\">span<\/span>&gt;<span class=\"hljs-title\">Total<\/span>&lt;\/<span class=\"hljs-title\">span<\/span>&gt;&lt;<span class=\"hljs-title\">span<\/span>&gt;&lt;?<span class=\"hljs-title\">php<\/span> <span class=\"hljs-title\">echo<\/span> '$' . $<span class=\"hljs-title\">total<\/span> ?&gt;&lt;\/<span class=\"hljs-title\">span<\/span>&gt;&lt;\/<span class=\"hljs-title\">li<\/span>&gt;\n    &lt;\/<span class=\"hljs-title\">ul<\/span>&gt;\n&lt;?<span class=\"hljs-title\">php<\/span> <span class=\"hljs-title\">else<\/span> : ?&gt;\n    &lt;<span class=\"hljs-title\">p<\/span>&gt;<span class=\"hljs-title\">You<\/span> <span class=\"hljs-title\">didn<\/span>'<span class=\"hljs-title\">t<\/span> <span class=\"hljs-title\">select<\/span> <span class=\"hljs-title\">any<\/span> <span class=\"hljs-title\">pizza<\/span> <span class=\"hljs-title\">toppings<\/span>.&lt;\/<span class=\"hljs-title\">p<\/span>&gt;\n&lt;?<span class=\"hljs-title\">php<\/span> <span class=\"hljs-title\">endif<\/span> ?&gt;\n\n&lt;<span class=\"hljs-title\">menu<\/span>&gt;\n    &lt;<span class=\"hljs-title\">a<\/span> <span class=\"hljs-title\">class<\/span>=\"<span class=\"hljs-title\">btn<\/span>\" <span class=\"hljs-title\">href<\/span>=\"&lt;?= <span class=\"hljs-title\">htmlentities<\/span>($<span class=\"hljs-title\">_SERVER<\/span>&#91;'<span class=\"hljs-title\">PHP_SELF<\/span>']) ?&gt;\" <span class=\"hljs-title\">title<\/span>=\"<span class=\"hljs-title\">Back<\/span> <span class=\"hljs-title\">to<\/span> <span class=\"hljs-title\">the<\/span> <span class=\"hljs-title\">form<\/span>\"&gt;<span class=\"hljs-title\">Change<\/span> <span class=\"hljs-title\">Toppings<\/span>&lt;\/<span class=\"hljs-title\">a<\/span>&gt;\n&lt;\/<span class=\"hljs-title\">menu<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>post.php<\/code> file get the form input using the <code><a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-filter_input\/\">filter_input()<\/a><\/code> function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"> $selected_toppings = filter_input(INPUT_POST, <span class=\"hljs-string\">'pizza_toppings'<\/span>,  FILTER_DEFAULT, FILTER_REQUIRE_ARRAY) ?? &#91;];<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>It checks selected pizza toppings against the original values in the <code>$pizza_toppings<\/code> array and adds the selected values to the <code>$_SESSION['selected_toppings']<\/code> variable. Also, it calculates the total price based on the selected pizza toppings.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">$toppings = array_keys($pizza_toppings);\n\n$_SESSION&#91;<span class=\"hljs-string\">'selected_toppings'<\/span>] = &#91;]; <span class=\"hljs-comment\">\/\/ for storing selected toppings<\/span>\n$total = <span class=\"hljs-number\">0<\/span>; <span class=\"hljs-comment\">\/\/ for storing total<\/span>\n\n<span class=\"hljs-comment\">\/\/ check data against the original values<\/span>\n<span class=\"hljs-keyword\">if<\/span> ($selected_toppings) {\n\t<span class=\"hljs-keyword\">foreach<\/span> ($selected_toppings <span class=\"hljs-keyword\">as<\/span> $topping) {\n\t\t<span class=\"hljs-keyword\">if<\/span> (in_array($topping, $toppings)) {\n\t\t\t$_SESSION&#91;<span class=\"hljs-string\">'selected_toppings'<\/span>]&#91;] = $topping;\n\t\t\t$total += $pizza_toppings&#91;$topping];\n\t\t}\n\t}\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The markup part shows the order summary if one or more pizza toppings are selected.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul id=\"block-29c28ced-4e6f-42ef-b910-c19e32548c9c\" class=\"wp-block-list\">\n<li>Add square brackets (<code>[]<\/code>) at the end of the checkbox name when a form has multiple checkboxes with the same name. <\/li>\n\n\n\n<li>PHP creates an associative array to stored values of the selected checkboxes if checkboxes have the same name that ends with <code>[]<\/code>.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Did you find this tutorial useful?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"1649\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-tutorial\/php-multiple-checkboxes\/\"\n\t\t\t\tdata-post-title=\"PHP Multiple Checkboxes\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"1649\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-tutorial\/php-multiple-checkboxes\/\"\n\t\t\t\tdata-post-title=\"PHP Multiple Checkboxes\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn how to handle a form with multiple checkboxes in PHP.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":15,"menu_order":88,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1649","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/1649","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/comments?post=1649"}],"version-history":[{"count":5,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/1649\/revisions"}],"predecessor-version":[{"id":3283,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/1649\/revisions\/3283"}],"up":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/15"}],"wp:attachment":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/media?parent=1649"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}