Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In my opinion empty() is the best way to check input in general</p> <pre><code> if( empty( $_POST[ 'username' ] ) ) { $error = 'Please enter a user name'; } </code></pre> <p>In the best case you do this</p> <pre><code> $username = !empty( $_POST[ 'username' ] ) ? trim( $_POST[ 'username' ] ) : ''; if( empty( $username ) ) $error = 'Please insert a user name'; if( strlen( $username ) &lt; 3 ) $error = 'Username needs 3 characters'; //etc... </code></pre> <p>This will make sure that the user doesn't just provide white-spaces to feed your requested input</p> <p>There are several methods to handle the input.</p> <p>1) Redirect your form to the same page and handle the input above the output</p> <pre><code> $error = ''; if( strtolower( $_SERVER[ 'REQUEST_METHOD' ] ) === 'post' ) { //handle user input and fill $error if needed if( empty( $error ) ) { //success } } ?&gt; &lt;form action="thissite.php"&gt; &lt;?php if( !empty( $error ) ) :?&gt;&lt;span class="error"&gt;&lt;?=$error?&gt;&lt;/span&gt;&lt;?php endif; ?&gt; &lt;...rest of the form...&gt; &lt;/form&gt; </code></pre> <p>2) You can handle it with AJAX</p> <p>Submit your Form via AJAX (see jQuery's <a href="http://api.jquery.com/jQuery.ajax/" rel="nofollow">$.ajax</a> and <a href="http://api.jquery.com/serialize/" rel="nofollow">$.serialize</a>, this well help you a lot), receive a JSON string that contains either a success state or the error. Parse this and insert error elements in your form accordingly</p> <p>3) (Worst way imo) Go to a new page, show the errors and redirect to the form again if there are errors</p> <p>Submit the form to a new page that handles the input, output styled error messages and put a link that either proceeds you (on success) or brings you back to the form (on error)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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