Note that there are some explanatory texts on larger screens.

plurals
  1. POhow do I redirect back to a form from a php validation script, within a front controller
    primarykey
    data
    text
    <p>I've read numerous questions about redirecting back to a form after performing server side validation (assuming the form has errors) and displaying the errors, but I haven't found anything related to my specific problem. I have a simple front controller that uses .htaccess to redirect http requests to index.php. That's the setup; here's my situation:</p> <p>register.php is a registration form that posts to validate_registration.php. I have code in the form that displays php variables (which won't be set unless the validation script has run). I use this at the end of validate_registration.php:</p> <pre><code>header("Location: http://www.example.com/register.php"); </code></pre> <p>Unfortunately, the code to display the errors in register.php simply doesn't do anything. Is this getting lost in my front controller? My .htaccess code is this:</p> <pre><code>&lt;IfModule mod_rewrite.c&gt; RewriteEngine On RewriteCond $1 !^(index\.php|ajax|css|error|form|img|js|robots\.txt|securimage|submit) RewriteRule ^(.*)$ index.php?url=$1 [PT,L] &lt;/IfModule&gt; </code></pre> <p>and my front controller is this (mostly, it's <em>extremely</em> simple with a switch statement because I absolutely no need for anything more complex, scalable, etc.):</p> <pre><code>$url = $_GET['url']; require_once("header.php"); echo '&lt;body&gt;'; require_once("banner.php"); require_once("navigation.php"); require_once("login_bar.php"); echo '&lt;div id="main"&gt;'; switch ($url) { case('register.php'): require_once('register.php'); break; default: require_once('error404.php'); break; } echo '&lt;/div&gt;'; //Closing div tag for the id="main" section require_once("$template_directory/general/footer.php"); echo '&lt;/body&gt;'; </code></pre> <p>Sorry about the long question; I can't seem to get this fixed! validate_registration.php redirects to register.php just fine, but the variables don't seem to carry through the front controller. Also, this form needs to work without Javascript enabled, so AJAX isn't an option, unfortunately. </p> <p>EDIT: Here's the code to the form with error messages; it's quite simple right now because I'm only testing it. </p> <pre><code>&lt;?php $allowed_fields = array("reg_username", "reg_password", "reg_confirm_password", "reg_email", "reg_confirm_email", "captcha_code", "reg_disclaimer", ); $db_select_connection = DB::get_admin_connection(); $errors = array(); foreach ($_POST as $key =&gt; $value) { if (in_array($key, $allowed_fields)) { $$key = mysql_real_escape_string($value); if ($value == "") { $errors["$key" . "_blank"] = "The $key field is required."; } } } // Validation section // Username if (ctype_alnum($reg_username)) { $errors["reg_username_invalid"] = "Username can only contain letters and numbers."; } // Password if (strlen($reg_password) &lt; 6) { $errors["reg_password_short"] = "Password must be at least 6 characters long."; } if ($reg_password !== $reg_confirm_password) { $errors["reg_password_match"] = "Passwords do not match."; } $legal_characters = preg_quote("~!@#$%^&amp;*()"); $trimmed_password = preg_replace("/[^a-zA-Z$legal_characters]/", "", $reg_password); if ($trimmed_password !== $reg_password) { $errors["reg_pass_chars"] = "Password can only contain letters, numbers, and " . htmlentities($legal_characters, ENT_QUOTES, 'UTF-8'); } // Email if ($reg_email !== $reg_confirm_email) { $errors["reg_email_match"] = "Email addresses do not match."; } if (!Email::validate_email($reg_email)) { $errors["reg_email_invalid"] = "Email address is invalid."; } //Disclaimer check box if ($reg_disclaimer !== "on") { $errors["reg_disclaimer_accept"] = "You must read and accept the disclaimer to create an account."; } require("http://www.example.com/register.php"); ?&gt; </code></pre> <p>The code for the form simply contains and similar code blocks to display the errors. There's a lot of formatting and styling divs involved so I won't waste space with the entire form. </p> <p>EDIT: Also, how do I format something as code without manually putting 4 spaces in front of <em>every</em> line? I copied code directly from Netbeans, my IDE, into the editing window, highlighted it, and pressed Code, and it still wouldn't let me submit my post. </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.
 

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