php - error for displayed even it has not been submitted yet -


i load page yet error has been displayed

<?php    $nameerr = $emailerr = $iderr = $passworderr = "";  $name = $email = $id = $password = "";    if ($_server["request_method"] == "post") {     if (empty($_post["name"])) {       $nameerr = "name required";     } else {       $name = test_input($_post["name"]);       // check if name contains letters , whitespace       if (!preg_match("/^[a-za-z ]*$/",$name)) {         $nameerr = "only letters , white space allowed";        }     }          if (empty($_post["email"])) {       $emailerr = "email required";     } else {       $email = test_input($_post["email"]);       // check if e-mail address well-formed       if (!filter_var($email, filter_validate_email)) {         $emailerr = "invalid email format";        }     }            if (empty($_post["id"])) {        $gendererr = "matric number required";     } else {       $website = test_input($_post["id"]);             if (!preg_match("/^[0-9].*$/",$id)){         $iderr = "only numbers allowed"; }       }     }       if (empty($_post["password"])) {       $passworderr = "password required";     } else {       $password = test_input($_post["password"]);       // check if name contains letters , whitespace       if (!preg_match("/^[a-za-z0-9]+$/",$password)) {         $nameerr = "special characters not allowed";        }     }    function test_input($data) {     $data = trim($data);     $data = stripslashes($data);     $data = htmlspecialchars($data);     return $data;  }  ?>    <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">  <html xmlns="http://www.w3.org/1999/xhtml">  <head>  <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />  <title>pendaftaran kali pertama</title>  <style type="text/css">  <!--  .style1 {  	color: #ffcc00;  	font-weight: bold;  	   }  .error{ color:#ffcc00}  -->  </style>    </head>    <body>  <div align="center">      <form  method="post" action="<?php echo htmlspecialchars($_server["php_self"]);?>">                <table width="62%" border="0" cellpadding="2" cellspacing="2">          <tr>            <td height="35" colspan="4" bgcolor="#660066"><div align="center" class="style1">pendaftaran kali pertama </div></td>          </tr>          <tr>            <td width="33%"><div align="right"><strong>nama </strong></div></td>            <td width="5%"><div align="center"><strong>:</strong></div></td>            <td width="62%"><input name="name" type="text" size="50"  /><span class="error">* <?php echo $nameerr;?></span></td>                      </tr>          <tr>            <td><div align="right"><strong>no. pelajar </strong></div></td>            <td><div align="center"><strong>:</strong></div></td>            <td><input name="id" type="text" size="50" maxlength="10"  /><span class="error">* <?php echo $iderr;?></span></td>                    </tr>          <tr>            <td><div align="right"><strong>kata laluan(8 aksara sahaja)</strong></div></td>            <td><div align="center"><strong>:</strong></div></td>            <td><input name="password" type="text" size="50" maxlength="8"  /><span class="error">* <?php echo $passworderr;?></span></td>                 </tr>          <tr>            <td><div align="right"><strong>emel</strong></div></td>            <td><div align="center"><strong>:</strong></div></td>            <td><input name="email" type="text" size="50"  /><span class="error">* <?php echo $emailerr;?></span></td>                    </tr>          <tr>            <td>&nbsp;</td>            <td>&nbsp;</td>            <td><input type="submit" value="submit"/></td>                      </tr>        </table>      </form>  </div>  </body>  </html>

it no error, when load page(the submit button not click) password error has been displayed. other field error work fine.

your password block outside of post block , why $passworderr set before form submitted.

if (empty($_post["password"])) {      $passworderr = "password required";    } else {      $password = test_input($_post["password"]);      // check if name contains letters , whitespace      if (!preg_match("/^[a-za-z0-9]+$/",$password)) {        $passworderr = "special characters not allowed";       }    } 

put inside block if ($_server["request_method"] == "post") {

also in code $nameerr = "special characters not allowed"; wrong. must assign error $passworderr have done in code above.


Comments

Popular posts from this blog

javascript - oscilloscope of speaker input stops rendering after a few seconds -

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -