Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can do this in either JavaScript or PHP. JS is more user friendly and easier to code, but can be bypassed by determined users. It also may not function on some browsers or with some settings allowing users to continue as if there were no validation, but those cases are usually rare. I would recommend a JS solution unless this is a corporate website or has no room for error.</p> <p>You can do this by modifying your form tag with an onSubmit function:</p> <pre><code> &lt;form action="whatever" method="post" onSubmit="checkStuff();"&gt; &lt;input id="field_1" name="field_1"...&gt; </code></pre> <p>You then need to create that function and place it in the head of your page. It should read something like:</p> <pre><code> function checkStuff() { // By default, we plan to submit the form. var formOkay = 1; // Check to see if field_1 has a value. If not, we note that by changing our variable. if (document.getElementById('field_1').value == '') formOkay = 0; ... // Let the user know something is wrong somehow. An alert is easiest. alert('fill out everything, ya goof!'); // If you return true the form will submit. If you return false it will not. if (formOkay == 1) { return true; } else { return false; } } </code></pre> <p>Note that your inputs must have an id attribute for this approach to work (though it's possible to modify my code to work with names too). I would make the id the same value as the field name. You can add additional checks for more fields where I placed the ellipsis. This code could be written more efficiently and cleanly, but I thought this approach would be easiest to understand and modify.</p> <p>This is off the top of my head and hasn't been tested, but should get you working down the right track. If you have additional questions, please let me know.</p> <p>EDIT: I just wanted to follow up to agree with others that if you have the time and inclination, or this is a work related issue, you should validate both ways. JS provides a better, more user friendly method, while PHP insures nobody can just circumvent the JS to break your rules.</p> <p>I don't know PHP, but your pseudo code would be something like this:</p> <pre><code> if field_1 = "" then // Option 1 Print("Please press back and fill out field 1!") AbortPage() // Option 2 Redirect("form.php?error=Please fill out field 1&amp;[other form values]") end if </code></pre> <p>In the case of option 2 you would modify the form page to detect url variables and place them into the inputs. You would also modify it to look for a url variable called 'error' and display the contents if found.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload