Forms can contain various elements – text fields, checkboxes, radio buttons, etc., the processing of which has its own characteristics.
Handling checkboxes
Checkboxes or checkboxes (html-element <input type=”checkbox”/>) can be in two states: checked (checked) and unchecked. For example:
Remember: <input type="checkbox" name="remember" checked="checked" />
Checkbox in PHP
If the checkbox is in an unchecked state, for example:
Remember: <input type="checkbox" name="remember" />
then when submitting the form, the value of this flag is not transmitted to the server.
If the checkbox is checked, then the remembervalue will be passed for the field when sending to the server on:
$remember = $_POST["remember"];
If we are not satisfied with the value on, then using the attribute value can set the value we need:
Remember: <input type="checkbox" name="remember" value="1" />
Sometimes it is necessary to create a set of checkboxes where you can select multiple values. For example:
<!DOCTYPE html> <html lang="en"> <head> <metacharset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>easywptutorials.com</title> </head> <body> <?php if(isset($_POST["technologies"])){ $technologies = $_POST["technologies"]; foreach($technologies as $item) echo "$item<br />"; } ?> <h3>Data Entry Form</h3> <form method="POST"> <p>ASP.NET: <input type="checkbox" name="technologies[]" value="ASP.NET" /></p> <p>PHP: <input type="checkbox" name="technologies[]" value="PHP" /></p> <p>Node.js: <input type="checkbox" name="technologies[]" value="Node.js" /></p> <input type="submit" value="Submit"> </form> </body> </html>
In this case, the value of the name attribute must have square brackets. And then after sending the server will receive an array of marked values:
$technologies = $_POST["technologies"]; foreach($technologies as $item) echo "$item <br />";
In this case, the variable $technologies will represent an array that can be iterated over and perform all other operations on arrays.
Switches
Radio buttons or radio buttons allow you to choose between several mutually exclusive options:
<!DOCTYPE html> <html lang="en"> <head> <metacharset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>easywptutorials.com</title> </head> <body> <?php if(isset($_POST["course"])) { $course = $_POST["course"]; echo $course; } ?> <h3>Data Entry Form</h3> <form method="POST"> <input type="radio" name="course" value="ASP.NET" />ASP.NET <br> <input type="radio" name="course" value="PHP" />PHP <br> <input type="radio" name="course" value="Node.js" />Node.js <br> <input type="submit" value="Submit"> </form> </body> </html>
Radio button in PHP
The attribute value of value the selected radio button is passed to the server. Getting the passed value:
if(isset($_POST["course"])) { $course = $_POST["course"]; echo $course; }
List
A list represents an element select that provides a choice of one or more elements:
<!DOCTYPE html> <html lang="en"> <head> <metacharset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>easywptutorials.com</title> </head> <body> <?php if(isset($_POST["course"])) { $course = $_POST["course"]; echo $course; } ?> <h3>Data Entry Form</h3> <form method="POST"> <select name="course" size="1"> <option value="ASP.NET">ASP.NET</option> <option value="PHP">PHP</option> <option value="Ruby">RUBY</option> <option value="Python">Python</option> </select> <input type="submit" value="Submit"> </form> </body> </html>
The element <select>contains a number of choices in the form of elements <option>:
Select list in PHP
Get the selected element in PHP code like a normal single value:
if(isset($_POST["course"])) { $course = $_POST["course"]; echo $course; }
But the element <select>also allows multiple selections. And in this case, the handling of the selected values changes as the server receives an array of values:
<!DOCTYPE html> <html lang="en"> <head> <metacharset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>easywptutorials.com</title> </head> <body> <?php if(isset($_POST["courses"])) { $courses = $_POST["courses"]; foreach($courses as $item) echo "$item<br>"; } ?> <h3>Data Entry Form</h3> <form method="POST"> <select name="courses[]" size="4" multiple="multiple"> <option value="ASP.NET">ASP.NET</option> <option value="PHP">PHP</option> <option value="Ruby">RUBY</option> <option value="Python">Python</option> </select><br> <input type="submit" value="Submit"> </form> </body> </html>
Such lists have the attribute multiple=”multiple”. To pass an array, name square brackets are also specified in the attribute: name=”courses[]”