reset button not function but submit button can use.anybody can help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • isaac27
    New Member
    • Feb 2016
    • 1

    reset button not function but submit button can use.anybody can help

    Code:
    <html> <head> <style>
    .error {color: #FF0000;}
    </style> </head> <body> <?php
    
    $nameErr = $emailErr = $genderErr = $websiteErr = $nokpErr = "";
    $name = $email = $gender = $nokp = $website = "";
    
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
       if (empty($_POST["name"])) {
         $nameErr = "Name is required";
       } else {
         $name = test_input($_POST["name"]);
         // check if name only contains letters and whitespace
         if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
           $nameErr = "Only letters and white space allowed";
         }
       }
      
       if (empty($_POST["email"])) {
         $emailErr = "Email is required";
       } else {
         $email = test_input($_POST["email"]);
         // check if e-mail address is well-formed
         if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
           $emailErr = "Invalid email format";
         }
       }
        
       if (empty($_POST["website"])) {
         $website = "";
       } else {
         $website = test_input($_POST["website"]);
         if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) {
           $websiteErr = "Invalid URL";
         }
       }
    
       if (empty($_POST["nokp"])) {
         $nokpErr = "";
       } else {
         $nokp = test_input($_POST["nokp"]);
    	 if (!preg_match("/^[1-9][0-9]{0-17}$/",$nokp)) {
           $nokpErr = "Only number is allowed";
       }}
    
       if (empty($_POST["gender"])) {
         $genderErr = "Gender is required";
       } else {
         $gender = test_input($_POST["gender"]);
       }
    }
    
    function test_input($data) {
       $data = trim($data);
       $data = stripslashes($data);
       $data = htmlspecialchars($data);
       return $data;
    }
    ?> <h2>PHP Form</h2> <p><span class="error">* required field.</span></p> <form method="post" action="https://bytes.com/<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> <p>Name:
      <input type="text" name="name" value="<?php echo $name;?>"> <span class="error">* <?php echo $nameErr;?></span> </p> <p>No KP:
         <input name="nokp" type="text" id="nokp" value="<?php echo $nokp;?>" size="12"> <span class="error">* <?php echo $nokpErr;?></span></p> <p>
         E-mail: <input type="text" name="email" value="<?php echo $email;?>"> <span class="error">* <?php echo $emailErr;?></span> <br><br>
         Website: <input type="text" name="website" value="<?php echo $website;?>"> <span class="error"><?php echo $websiteErr;?></span><br><br>
         Gender:
         <input type="radio" name="gender" <?php if (isset($gender) && $gender=="female") echo "checked";?>  value="male">Male
         <input type="radio" name="gender" <?php if (isset($gender) && $gender=="male") echo "checked";?>  value="female">Female
         <span class="error">* <?php echo $genderErr;?></span> <br><br> <button type="submit" value="Submit">Submit</button> <button type="reset" value="Reset">Reset</button> </p> </form> <?php
    
    echo "<h2>Your Input:</h2>";
    echo $name;
    echo "<br>";
    echo $nokp;
    echo "<br>";
    echo $email;
    echo "<br>";
    echo $website;
    echo "<br>";
    echo $gender;
    
    ?> </body> </html>
    Last edited by Rabbit; Feb 15 '16, 09:33 PM. Reason: Please use [code] and [/code] tags when posting code or formatted data.
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    note as this might be due to a misunderstandin g: a reset button does not empty a form. it undoes any changes made to the form values applied after loading the form.

    Comment

    • Dormilich
      Recognized Expert Expert
      • Aug 2008
      • 8694

      #3
      moved to HTML as this is not a PHP issue.

      Comment

      Working...