{"id":964,"date":"2021-04-08T02:45:22","date_gmt":"2021-04-08T02:45:22","guid":{"rendered":"https:\/\/phptutorial.net\/?page_id=964"},"modified":"2025-04-07T14:33:59","modified_gmt":"2025-04-07T14:33:59","slug":"php-radio-button","status":"publish","type":"page","link":"https:\/\/www.phptutorial.net\/php-tutorial\/php-radio-button\/","title":{"rendered":"PHP Radio Button"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to create a form with radio buttons and handle radio groups in PHP.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-radio-buttons'>Introduction to radio buttons <a href=\"#introduction-to-radio-buttons\" class=\"anchor\" id=\"introduction-to-radio-buttons\" title=\"Anchor for Introduction to radio buttons\">#<\/a><\/h2>\n\n\n\n<p>To create a radio button, you use the <code>&lt;input&gt;<\/code> element with the type <code>radio<\/code>. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" 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\">input<\/span> <span class=\"hljs-attr\">type<\/span>=<span class=\"hljs-string\">\"radio\"<\/span> <span class=\"hljs-attr\">name<\/span>=<span class=\"hljs-string\">\"contact\"<\/span> <span class=\"hljs-attr\">id<\/span>=<span class=\"hljs-string\">\"contact_email\"<\/span> <span class=\"hljs-attr\">value<\/span>=<span class=\"hljs-string\">\"email\"<\/span> \/&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><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>A radio button doesn&#8217;t have a label. Therefore, you should always use a radio button with a <code>&lt;label&gt;<\/code> element like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" 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\">input<\/span> <span class=\"hljs-attr\">type<\/span>=<span class=\"hljs-string\">\"radio\"<\/span> <span class=\"hljs-attr\">name<\/span>=<span class=\"hljs-string\">\"contact\"<\/span> <span class=\"hljs-attr\">id<\/span>=<span class=\"hljs-string\">\"contact_email\"<\/span> <span class=\"hljs-attr\">value<\/span>=<span class=\"hljs-string\">\"email\"<\/span>\/&gt;<\/span>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">label<\/span> <span class=\"hljs-attr\">for<\/span>=<span class=\"hljs-string\">\"contact_email\"<\/span>&gt;<\/span>Email<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">label<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><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>To associate a radio button with a <code>&lt;label&gt;<\/code> element, the value of the <code>for<\/code> attribute of the label needs to be the same as the value of the <code>id<\/code> of the radio button.<\/p>\n\n\n\n<p>A radio button has two states: checked and unchecked. <\/p>\n\n\n\n<p>When you link a label with a radio button, you can check the radio button by clicking the label or the radio button itself. Hence, the label increases the usability of the radio button because it expands the selection area.<\/p>\n\n\n\n<p>Alternatively, you can place the radio button within a <code>&lt;label&gt;<\/code> element like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" 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\">label<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">input<\/span> <span class=\"hljs-attr\">type<\/span>=<span class=\"hljs-string\">\"radio\"<\/span> <span class=\"hljs-attr\">name<\/span>=<span class=\"hljs-string\">\"contact_email\"<\/span> <span class=\"hljs-attr\">value<\/span>=<span class=\"hljs-string\">\"email\"<\/span>&gt;<\/span> Email\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">label<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><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>In this case, the radio links to the label without matching the for and id attributes.<\/p>\n\n\n\n<p>To select a radio button automatically when the page loads for the first time, you can use the <code>checked<\/code> Boolean attribute:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" 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\">input<\/span> <span class=\"hljs-attr\">type<\/span>=<span class=\"hljs-string\">\"radio\"<\/span> <span class=\"hljs-attr\">name<\/span>=<span class=\"hljs-string\">\"contact\"<\/span> <span class=\"hljs-attr\">id<\/span>=<span class=\"hljs-string\">\"contact_email\"<\/span> <span class=\"hljs-attr\">value<\/span>=<span class=\"hljs-string\">\"email\"<\/span> <span class=\"hljs-attr\">checked<\/span> \/&gt;<\/span>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">label<\/span> <span class=\"hljs-attr\">for<\/span>=<span class=\"hljs-string\">\"contact_email\"<\/span>&gt;<\/span>Email<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">label<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><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<h3 class=\"wp-block-heading\" id='define-a-radio-group'>Define a radio group <a href=\"#define-a-radio-group\" class=\"anchor\" id=\"define-a-radio-group\" title=\"Anchor for Define a radio group\">#<\/a><\/h3>\n\n\n\n<p>In practice, you often use the radio buttons in a group. A group of radio buttons is called a radio group. A radio group allows you to select only one radio button at a time.<\/p>\n\n\n\n<p>If you select any radio button in a group, the currently-selected radio button in the same group is automatically deselected.<\/p>\n\n\n\n<p>To define a radio group, you assign the same name to all the radio buttons in the same group.<\/p>\n\n\n\n<p>The following example defines a radio group that consists of two radio buttons.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" 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\">input<\/span> <span class=\"hljs-attr\">type<\/span>=<span class=\"hljs-string\">\"radio\"<\/span> <span class=\"hljs-attr\">name<\/span>=<span class=\"hljs-string\">\"contact\"<\/span> <span class=\"hljs-attr\">id<\/span>=<span class=\"hljs-string\">\"contact_email\"<\/span> <span class=\"hljs-attr\">value<\/span>=<span class=\"hljs-string\">\"email\"<\/span> \/&gt;<\/span>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">label<\/span> <span class=\"hljs-attr\">for<\/span>=<span class=\"hljs-string\">\"contact_email\"<\/span>&gt;<\/span>Email<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">label<\/span>&gt;<\/span>\n\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">input<\/span> <span class=\"hljs-attr\">type<\/span>=<span class=\"hljs-string\">\"radio\"<\/span> <span class=\"hljs-attr\">name<\/span>=<span class=\"hljs-string\">\"contact\"<\/span> <span class=\"hljs-attr\">id<\/span>=<span class=\"hljs-string\">\"contact_phone\"<\/span> <span class=\"hljs-attr\">value<\/span>=<span class=\"hljs-string\">\"phone\"<\/span> \/&gt;<\/span>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">label<\/span> <span class=\"hljs-attr\">for<\/span>=<span class=\"hljs-string\">\"contact_phone\"<\/span>&gt;<\/span>Phone<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">label<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><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<h2 class=\"wp-block-heading\" id='handle-radio-buttons-in-php'>Handle radio buttons in PHP <a href=\"#handle-radio-buttons-in-php\" class=\"anchor\" id=\"handle-radio-buttons-in-php\" title=\"Anchor for Handle radio buttons in PHP\">#<\/a><\/h2>\n\n\n\n<p>When a form has a radio button with a name, you can get the checked radio button by accessing either <code>$_POST<\/code> or <code>$_GET<\/code> array, depending on the request method.<\/p>\n\n\n\n<p>If a radio button is not checked, the <code>$_POST<\/code> or <code>$_GET<\/code> does not contain the key of the radio button. Therefore, you need to use the <code>isset()<\/code> function to check whether a radio button is checked or not:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" 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\">'radio_name'<\/span>])<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><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-7\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">filter_has_var(INPUT_POST, <span class=\"hljs-string\">'radio_name'<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>filer_has_var()<\/code> returns <code>true<\/code> if it finds the radio button name in the <code>INPUT_POST<\/code>.<\/p>\n\n\n\n<p>If a radio button is checked, you get the value of the radio button from the <code>$_POST<\/code> using the radio button name:<\/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\">$_POST&#91;<span class=\"hljs-string\">'radio_name'<\/span>]<\/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<h2 class=\"wp-block-heading\" id='php-radio-button-example'>PHP radio button example <a href=\"#php-radio-button-example\" class=\"anchor\" id=\"php-radio-button-example\" title=\"Anchor for PHP radio button example\">#<\/a><\/h2>\n\n\n\n<p>We&#8217;ll create a <a href=\"https:\/\/phptutorial.net\/app\/radiobutton\/\" target=\"_blank\" rel=\"noreferrer noopener\">simple form with a radio group<\/a>. If you do not select any option and submit the form, it&#8217;ll show an error message. Otherwise, it&#8217;ll show the value of the selected radio button.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/phptutorial.net\/app\/radiobutton\/\" target=\"_blank\" rel=\" noreferrer noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"452\" height=\"276\" src=\"https:\/\/phptutorial.net\/wp-content\/uploads\/2021\/05\/php-radio-button.png\" alt=\"PHP Radio Button Demo\" class=\"wp-image-1681\" srcset=\"https:\/\/www.phptutorial.net\/wp-content\/uploads\/2021\/05\/php-radio-button.png 452w, https:\/\/www.phptutorial.net\/wp-content\/uploads\/2021\/05\/php-radio-button-300x183.png 300w\" sizes=\"auto, (max-width: 452px) 100vw, 452px\" \/><\/a><\/figure>\n<\/div>\n\n\n<p>First, create the following directory and file structure:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">.\n\u251c\u2500\u2500 css\n|  \u2514\u2500\u2500 style.css\n\u251c\u2500\u2500 inc\n|  \u251c\u2500\u2500 footer.php\n|  \u251c\u2500\u2500 get.php\n|  \u251c\u2500\u2500 header.php\n|  \u2514\u2500\u2500 post.php\n\u2514\u2500\u2500 index.php<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><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>header.php<\/td><td>inc<\/td><td>Contain the HTML header<\/td><\/tr><tr><td>footer.php<\/td><td>inc<\/td><td>Contain the HTML footer<\/td><\/tr><tr><td>get.php<\/td><td>inc<\/td><td>Contain the code for showing a form with radio buttons 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>style.css<\/td><td>css<\/td><td>Contain the CSS code<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Second, add the following code to the <code>header.php<\/code> file:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" 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\">title<\/span>&gt;<\/span>PHP Radio Button<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">title<\/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\">head<\/span>&gt;<\/span>\n\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">body<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">main<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><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>Third, add the following code to the <code>footer.php<\/code> file:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" 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-11\"><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>Fourth, add the following code to the <code>index.php<\/code> file:<\/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\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-keyword\">require<\/span> <span class=\"hljs-keyword\">__DIR__<\/span> . <span class=\"hljs-string\">'\/inc\/header.php'<\/span>;\n\n$contacts = &#91;\n\t<span class=\"hljs-string\">'email'<\/span> =&gt; <span class=\"hljs-string\">'Email'<\/span>,\n\t<span class=\"hljs-string\">'phone'<\/span> =&gt; <span class=\"hljs-string\">'Phone'<\/span>\n];\n\n$errors = &#91;];\n\n$request_method = strtoupper($_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-keyword\">__DIR__<\/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-keyword\">__DIR__<\/span> . <span class=\"hljs-string\">'\/inc\/post.php'<\/span>;\n}\n\n<span class=\"hljs-keyword\">require<\/span> <span class=\"hljs-keyword\">__DIR__<\/span> . <span class=\"hljs-string\">'\/inc\/footer.php'<\/span>;\n<\/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>How it works.<\/p>\n\n\n\n<p>The <code>$contacts<\/code> array is used to generate radio buttons dynamically. In a real application, the data may come from a database table or a result of an API call.<\/p>\n\n\n\n<p>The <code>$errors<\/code> array is used to collect the error messages.<\/p>\n\n\n\n<p>To get the HTTP request method, you access the <code>'REQUEST_METHOD'<\/code> key of the <code>$_SERVER<\/code> array. The <code>strtoupper()<\/code> function converts the request method to uppercase.<\/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\">$request_method = strtoupper($_SERVER&#91;<span class=\"hljs-string\">'REQUEST_METHOD'<\/span>]);<\/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 <code>index.php<\/code> file loads the <code>get.php<\/code> from the <code>inc<\/code> directory if the request is GET. When the form is submitted with the POST request, the <code>index.php<\/code> loads the <code>post.php<\/code> from the <code>inc<\/code> directory:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><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-keyword\">__DIR__<\/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-keyword\">__DIR__<\/span> . <span class=\"hljs-string\">'\/inc\/post.php'<\/span>;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><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>Fifth, create a form in the <code>get.php<\/code> file:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" 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    &lt;div&gt;Please choose your preferred method of contact:&lt;\/div&gt;\n    <span class=\"hljs-meta\">&lt;?php<\/span> <span class=\"hljs-keyword\">foreach<\/span> ($contacts <span class=\"hljs-keyword\">as<\/span> $key =&gt; $value) : <span class=\"hljs-meta\">?&gt;<\/span>\n        &lt;div&gt;\n            &lt;input type=<span class=\"hljs-string\">\"radio\"<\/span> name=<span class=\"hljs-string\">\"contact\"<\/span> id=<span class=\"hljs-string\">\"contact_&lt;?php echo $key ?&gt;\"<\/span> value=<span class=\"hljs-string\">\"&lt;?php echo $key ?&gt;\"<\/span> \/&gt;\n            &lt;label <span class=\"hljs-keyword\">for<\/span>=<span class=\"hljs-string\">\"contact_&lt;?php echo $key ?&gt;\"<\/span>&gt;<span class=\"hljs-meta\">&lt;?php<\/span> <span class=\"hljs-keyword\">echo<\/span> $value <span class=\"hljs-meta\">?&gt;<\/span>&lt;\/label&gt;\n        &lt;\/div&gt;\n    <span class=\"hljs-meta\">&lt;?php<\/span> <span class=\"hljs-keyword\">endforeach<\/span> <span class=\"hljs-meta\">?&gt;<\/span>\n    &lt;div&gt;\n        &lt;small <span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span>=\"<span class=\"hljs-title\">error<\/span>\"&gt;&lt;?<span class=\"hljs-title\">php<\/span> <span class=\"hljs-title\">echo<\/span> $<span class=\"hljs-title\">errors<\/span>&#91;'<span class=\"hljs-title\">contact<\/span>'] ?? '' ?&gt;&lt;\/<span class=\"hljs-title\">small<\/span>&gt;\n    &lt;\/<span class=\"hljs-title\">div<\/span>&gt;\n    &lt;<span class=\"hljs-title\">div<\/span>&gt;\n        &lt;<span class=\"hljs-title\">button<\/span> <span class=\"hljs-title\">type<\/span>=\"<span class=\"hljs-title\">submit<\/span>\"&gt;<span class=\"hljs-title\">Submit<\/span>&lt;\/<span class=\"hljs-title\">button<\/span>&gt;\n    &lt;\/<span class=\"hljs-title\">div<\/span>&gt;\n&lt;\/<span class=\"hljs-title\">form<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><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 following code uses a <code><a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-foreach\/\">foreach<\/a><\/code> statement to generate a radio group from the <code>$contacts<\/code> array:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span> <span class=\"hljs-keyword\">foreach<\/span> ($contacts <span class=\"hljs-keyword\">as<\/span> $key =&gt; $value) : <span class=\"hljs-meta\">?&gt;<\/span>\n&lt;div&gt;\n    &lt;input type=<span class=\"hljs-string\">\"radio\"<\/span> name=<span class=\"hljs-string\">\"contact\"<\/span> id=<span class=\"hljs-string\">\"contact_&lt;?php echo $key ?&gt;\"<\/span> value=<span class=\"hljs-string\">\"&lt;?php echo $key ?&gt;\"<\/span> \/&gt;\n    &lt;label <span class=\"hljs-keyword\">for<\/span>=<span class=\"hljs-string\">\"contact_&lt;?php echo $key ?&gt;\"<\/span>&gt;\n        <span class=\"hljs-meta\">&lt;?php<\/span> <span class=\"hljs-keyword\">echo<\/span> $value <span class=\"hljs-meta\">?&gt;<\/span>\n    &lt;\/label&gt;\n&lt;\/div&gt;\n<span class=\"hljs-meta\">&lt;?php<\/span> <span class=\"hljs-keyword\">endforeach<\/span> <span class=\"hljs-meta\">?&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><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 following code shows the error message from the <code>$errors<\/code> array. Note that we use the <a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-null-coalescing-operator\/\">null coalescing operator<\/a> (??) which is available in PHP 7+. <\/p>\n\n\n\n<p>The expression <code>$errors['contact'] ?? ''<\/code> returns <code>$errors['contact']<\/code> if it exists and is not null or &#8221; otherwise.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-17\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">&lt;small <span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span>=\"<span class=\"hljs-title\">error<\/span>\"&gt;&lt;?<span class=\"hljs-title\">php<\/span> <span class=\"hljs-title\">echo<\/span> $<span class=\"hljs-title\">errors<\/span>&#91;'<span class=\"hljs-title\">contact<\/span>'] ?? '' ?&gt;&lt;\/<span class=\"hljs-title\">small<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-17\"><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>Finally, add the code that handles the POST request:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-18\" 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-keyword\">if<\/span>(filter_has_var(INPUT_POST, <span class=\"hljs-string\">'contact'<\/span>)) {\n\t\n\t$contact = trim($_POST&#91;<span class=\"hljs-string\">'contact'<\/span>]);\n\n\t<span class=\"hljs-comment\">\/\/ check the selected value against the original values<\/span>\n\t<span class=\"hljs-keyword\">if<\/span> ($contact &amp;&amp; array_key_exists($contact, $contacts)) {\n\t\t$contact = htmlspecialchars($contact);\n\t} <span class=\"hljs-keyword\">else<\/span> {\n\t\t$errors&#91;<span class=\"hljs-string\">'contact'<\/span>] = <span class=\"hljs-string\">'Please select at least an option.'<\/span>;\n\t}\n\n\t<span class=\"hljs-keyword\">if<\/span> (count($errors)) {\n\t\t<span class=\"hljs-keyword\">require<\/span> <span class=\"hljs-keyword\">__DIR__<\/span> .  <span class=\"hljs-string\">'\/get.php'<\/span>;\n\t} <span class=\"hljs-keyword\">else<\/span> {\n\t\t<span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">&lt;&lt;&lt;html\n\t\tYou selected to be contacted via &lt;strong&gt; <span class=\"hljs-subst\">$contact<\/span>&lt;\/strong&gt;.\n\t\t&lt;a href=\"index.php\"&gt;Back to the form&lt;\/a&gt;\n\t\thtml;\n\t}\n} <\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-18\"><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 <code>post.php<\/code> works.<\/p>\n\n\n\n<p>The <code>filter_input()<\/code> function sanitizes the value of the checked radio button. To make sure the submitted value is valid, we check it against the keys of the <code>$contact<\/code> array using the <code><a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-array_key_exists\/\">array_key_exists()<\/a><\/code> function.<\/p>\n\n\n\n<p>If the submitted value matches with one of the array keys, we show a message. Otherwise, we add an error message to the <code>$errors<\/code> array.<\/p>\n\n\n\n<p>In the last <code><a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-if-else\/\">if...else<\/a><\/code> statement, we load the form (<code>get.php<\/code>) that shows an error message if the <code>$errors<\/code> array is not empty. Otherwise, we show a confirmation message.<\/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 class=\"wp-block-list\">\n<li>Use the input with <code>type=\"radio\"<\/code> to create a radio button. <\/li>\n\n\n\n<li>Use the same name for multiple radio buttons to define a radio group.<\/li>\n\n\n\n<li>Get the value of a checked radio via the <code>$_POST<\/code> (or <code>$_GET<\/code>) variable, depending on the request method.<\/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=\"964\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-tutorial\/php-radio-button\/\"\n\t\t\t\tdata-post-title=\"PHP Radio Button\"\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=\"964\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-tutorial\/php-radio-button\/\"\n\t\t\t\tdata-post-title=\"PHP Radio Button\"\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&#8217;ll learn about radio buttons and how to handle radio groups in PHP.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":15,"menu_order":89,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-964","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/964","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=964"}],"version-history":[{"count":5,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/964\/revisions"}],"predecessor-version":[{"id":3286,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/964\/revisions\/3286"}],"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=964"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}