PHP error handling, form is submitting anyway? -


i have following code, below. stop script , display errors next 3 selected input fields. however, when leave 1 of required fields blank , hit submit, form processes anyway. if replace $error[]'s in beginning die() stops properly, when try display them on form page doesn't work , submits/runs query. appreciated!

<?php     if(!empty($_post))  {     // if there $error, keep submitted values      $submitted_firstname = htmlentities($_post['firstname'], ent_quotes, 'utf-8');      $submitted_lastname = htmlentities($_post['lastname'], ent_quotes, 'utf-8');     $submitted_phone1 = htmlentities($_post['phone1'], ent_quotes, 'utf-8');      $submitted_phone2 = htmlentities($_post['phone2'], ent_quotes, 'utf-8');      $submitted_ext1 = htmlentities($_post['ext1'], ent_quotes, 'utf-8');      $submitted_ext2 = htmlentities($_post['ext2'], ent_quotes, 'utf-8');      $submitted_email = htmlentities($_post['email'], ent_quotes, 'utf-8');      $submitted_street = htmlentities($_post['street'], ent_quotes, 'utf-8');      $submitted_city = htmlentities($_post['city'], ent_quotes, 'utf-8');      $submitted_zip = htmlentities($_post['zip'], ent_quotes, 'utf-8');    // ensure user has entered non-empty firstname, lastname, , 1 phone if(empty($_post['firstname']))  {      $errors['addcust_fname'] = '* first name required'; }  if(empty($_post['lastname']))  {      $errors['addcust_lname'] = '* last name required'; }  if(empty($_post['phone1']))  {      $errors['addcust_phone'] = '* 1 phone required'; }         $state = $_post['state'];      $query = "          insert customers (              firstname,              lastname,             phone1,             phone2,             ext1,             ext2,             email,             street,             city,             state,             zip,             joindate          ) values (              :firstname,              :lastname,              :phone1,              :phone2,              :ext1,              :ext2,              :email,              :street,              :city,              :state,              :zip,              now()          )      ";       $query_params = array(          ':firstname' => $_post['firstname'],          ':lastname' => $_post['lastname'],          ':phone1' => $_post['phone1'],          ':phone2' => $_post['phone2'],          ':ext1' => $_post['ext1'],          ':ext2' => $_post['ext2'],          ':email' => $_post['email'],          ':street' => $_post['street'],          ':city' => $_post['city'],          ':state' => $_post['state'],          ':zip' => $_post['zip']      );       try      {          // execute query create user          $stmt = $db->prepare($query);          $result = $stmt->execute($query_params);      }      catch(pdoexception $ex)      {          die("failed run query: " . $ex->getmessage());      }       // redirects user login page after register      header("location: main.php");      die("redirecting main.php");       }    ?>  <?php include('common/header2.php'); ?> <body align="center"> <div align="center"> <div id="header"> <div id="logo"><h1><span><?php echo($company); ?></span><em>/</em><strong>portal</strong></h1></div> <div><span id="tick2"></span></div> <div><?php echo date("y/m/d"); ?></div> </div> <?php  // top menu require("common/top_menu.php"); ?> <div style="height: auto" id="main-content-section"> <h2>add new customer</h2>  <div align="center" id="addcustdiv"> <form action="add_customer.php" method="post">  first name: <input class="search-input" type="text" name="firstname" value="    <?php echo $submitted_firstname; ?>" /> <?php if($_post &&     isset($errors['addcust_firstname'])) { echo $errors['addcust_firstname'] ; } ?> <br><br>last name: <input class="search-input" type="text" name="lastname"     value="<?php echo $submitted_lastname; ?>" /> <?php if($_post &&     isset($errors['addcust_lastname'])) { echo $errors['addcust_lastname'] ; } ?> <br><br>phone 1: <input class="search-input" size="10" type="text"     name="phone1" value="<?php echo $submitted_phone1; ?>" /> ext: <input     class="search-input" size="3" type="text" name="ext1" value="<?php echo     $submitted_ext1; ?>" /> <?php if($_post && isset($errors['addcust_phone'])) {     echo $errors['addcust_phone'] ; } ?> <br><br>phone 2: <input class="search-input" size="10" type="text"     name="phone2" value="<?php echo $submitted_phone2; ?>" /> ext: <input     class="search-input" size="3" type="text" name="ext2" value="<?php echo     $submitted_ext2; ?>" />  <br><br>e-mail: <input class="search-input" type="text" name="email" value="    <?php echo $submitted_email; ?>" />  <br><br>street: <input class="search-input" type="text" name="street"     value="<?php echo $submitted_street; ?>" />  <br><br>city: <input class="search-input" type="text" name="city" value="<?    php echo $submitted_city; ?>" /> state: <select class="search-input"     name="state">   <option value="ct">ct</option>   <option value="al">al</option>   <option value="ak">ak</option>   <option value="az">az</option>   <option value="ar">ar</option>   <option value="ca">ca</option>   <option value="co">co</option>   <option value="ct">ct</option>   <option value="de">de</option>   <option value="fl">fl</option>   <option value="ga">ga</option>   <option value="hi">hi</option>   <option value="id">id</option>   <option value="il">il</option>   <option value="in">in</option>   <option value="ia">ia</option>   <option value="ks">ks</option>   <option value="ky">ky</option>   <option value="la">la</option>   <option value="me">me</option>   <option value="md">md</option>   <option value="ma">ma</option>   <option value="mi">mi</option>   <option value="mn">mn</option>   <option value="ms">ms</option>   <option value="mo">mo</option>   <option value="mt">mt</option>   <option value="ne">ne</option>   <option value="nv">nv</option>   <option value="nh">nh</option>   <option value="nj">nj</option>   <option value="nm">nm</option>   <option value="ny">ny</option>   <option value="nc">nc</option>   <option value="nd">nd</option>   <option value="oh">oh</option>   <option value="ok">ok</option>   <option value="or">or</option>   <option value="pa">pa</option>   <option value="ri">ri</option>   <option value="sc">sc</option>   <option value="sd">sd</option>   <option value="tn">tn</option>   <option value="tx">tx</option>   <option value="ut">ut</option>   <option value="vt">vt</option>   <option value="va">va</option>   <option value="wa">wa</option>   <option value="wv">wv</option>   <option value="wi">wi</option>   <option value="wy">wy</option>   <option value="dc">dc</option> </select> <br><br>zip: <input class="search-input" type="text" name="zip" value="<?php      echo $submitted_zip; ?>" />  <br><br><input type="submit" width="20" class="login-submit" value="add         customer" />  </form> </div> </div> <?php include('common/footer.php') ?> 

put form processing in if condition that'll check errors array length:

if(!count($errors)) {     // form processing      // redirects user login page after register      header("location: main.php");      die("redirecting main.php"); } 

be sure initialize $errors variable setting array before that, you'll error in case there isn't otherwise ($errors = array()).


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -