The problem is that, to have the data submitted in a 'nice' format to the server, the function returns an array containing all the selected checkbox values. Again, we can make the code a lot simpler in supporting browsers: suffix tells a server side script such as PHP to accept multiple values with that name into an array.Normally you would modify this so that you could submit or not submit the form based on the number of items selected. Note that in this section we've been using the new ES6 syntax which may not be available in all browsers.Most modern browsers now support HTML5 Form Validation making it possible to validate form elements without (or before) any Java Script is triggered.The value of a text input box (or a textarea or password input) is available using the syntax that tells you which option has been selected.We will also do two more things when the user submits the form: The next step is to create a function that will do all the checking for us (which is much more convenient than writing the same code over and over again). Now, we can check each $_POST variable with the test_input() function, and the script looks like this: Notice that at the start of the script, we check whether the form has been submitted using $_SERVER["REQUEST_METHOD"].
You'll see that the all validation scripts presented on this and subsequent pages adhere to the same basic format.
To take things a step further you can even explore Ajax Form Validation which lets you supply real-time feedback using server-side scripts triggered by Java Script events.
The team at have made a video based on this article as a part of their Java Script training lessons which you can view below. Would be great if you could post full examples of code including form and not just pieces that fit into the greater framework of code.
The script works fine even if the user does not enter any data.
The next step is to make input fields required and create error messages if needed.
I tested your first piece of code on a form and it does not work at all.