To make it a bit cleaner, we can trim the input to remove any extra space from the beginning and end of the form.
Here is the PHP script that checks the name for empty input and throws an error if the input is empty.
We need to check if it is equal to null or “” because different browsers act differently so its better to check for both the conditions.
Note that we are using the htmlentities() function to encode any HTML entities like checks whether Round6 was selected.
If so, adds the checked attribute to the radio button.
Check box groups are great when you want to allow multiple options for a field. We need to validate that at least two options are selected.The following function checks whether the texbox with id name1 is empty, if empty it displays a message and highlights that textbox with red color.We use return false; because when this function is called when the form is submitted and if this textbox is found empty the page shouldn’t be submitted so returning the boolean value false with prevent the form from being submitted.When you have fields like name or email as mandatory in the form, it should be checked in the server side script as well. Here is the sample output from that script: Note that the “Filling” checkbox group in the HTML form results in an array in the PHP script The rest of the fields appear as simple name-value pairs To validate mandatory fields, we just have to check the presence of the value in the $_POST array.(note that the form method is post in the HTML form).