Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Sorry to say, but you have to output valid HTML no matter how much of a pain that is. Best suggestion is to wrap it in a helper function and include it. Or even better, go find a good form handling library and use it. This problem has been solved 1000's of times.</p> <p><strong>XSS CONCERN:</strong> Your example code has a number of security flaws. Let me illustrate a simple way to handle a form cleanly. The following code illustrates an error-proof way to handle:</p> <ul> <li>Reading input</li> <li>Validation</li> <li>Handling errors, with nice messages</li> <li>Re-populating the form</li> <li>Etc...</li> </ul> <p>Please note the use of <code>htmlspecialchars</code> and <code>ENT_QUOTES</code> where needed. Also, the use of the <code>cond ? val1 : val2</code> operator ensures that there are no <code>E_STRICT</code> warnings omitted, without the use of <code>@</code> (which can be terrible for performance).</p> <pre><code>&lt;?php $FirstName = trim(isset($_POST['FirstName']) ? $_POST['FirstName'] : ''); $LastName = trim(isset($_POST['LastName']) ? $_POST['LastName'] : ''); $Gender = trim(isset($_POST['Gender']) ? $_POST['Gender'] : ''); $Action = trim(isset($_POST['Action']) ? $_POST['Action'] : ''); $Errors = array(); switch($Action) { case 'Process': // validation code here if(empty($FirstName)) $Errors[] = 'First Name is required.'; if(empty($LastName)) $Errors[] = 'Last Name is required.'; if($Gender != 'Male' and $Gender != 'Female') $Errors[] = 'Gender is required.'; if(count($Errors) &gt; 0) $break; // save data or whatever here // Redirect to next page header('Location: nextpage.php'); exit; } ?&gt; &lt;html&gt; &lt;head&gt; ... &lt;/head&gt; &lt;body&gt; &lt;?php if(count($Errors) &gt; 0) { ?&gt; &lt;div class="error"&gt; &lt;?php foreach($Error as $e) { ?&gt; &lt;p&gt;&lt;?php echo htmlspecialchars($e); ?&gt;&lt;/p&gt; &lt;?php } ?&gt; &lt;/div&gt; &lt;?php } ?&gt; &lt;form method="post" action="&lt;?php echo htmlspecialchars($_SERVER['REQUEST_URI'], ENT_QUOTES); ?&gt;"&gt; &lt;p&gt; First Name: &lt;input type="text" name="FirstName" value="&lt;?php echo htmlspecialchars($FirstName, ENT_QUOTES); ?&gt;" /&gt; &lt;/p&gt; &lt;p&gt; Last Name: &lt;input type="text" name="LastName" value="&lt;?php echo htmlspecialchars($LastName, ENT_QUOTES); ?&gt;" /&gt; &lt;/p&gt; &lt;p&gt; Gender: &lt;select name="Gender"&gt; &lt;option value=""&gt;&lt;/option&gt; &lt;option value="Male" &lt;?php if($Gender == 'Male') echo 'selected="selected"'; ?&gt;&gt;Male&lt;/option&gt; &lt;option value="Female" &lt;?php if($Gender == 'Female') echo 'selected="selected"'; ?&gt;&gt;Female&lt;/option&gt; &lt;/select&gt; &lt;/p&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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