drlnsli18864734
2015-12-14 11:50 阅读 151
已采纳

单击提交按钮时,如何使我的所有文本框都不为空

I'm new to PHP and I want to know How can i make my all my textbox not empty when i click the submit button. When i enter special characters like space,/;>< and etc all the textbox becomes empty. I only want that textbox that i input special characters to be empty not all the textbox.

HERE is my code

function.php

<html>
<head>
    <style>
        .error {color: #FF0000;}
    </style>
</head>
<body> 
    <h2>PHP Form Validation Example</h2>
    <p><span class="error">* required field.</span></p>
    <form method="post" action="1.php"> 
         <label>Name:</label> <input type="text" name="name"> <span class="error">* <?php echo $_SESSION['name'];?></span>
         <br><br>
         <label>E-mail:</label> <input type="text" name="email"> <span class="error">* <?php echo $_SESSION['email'];?></span>
         <br><br>
         <label>Website:</label> <input type="text" name="website"> <span class="error"><?php echo $_SESSION['website'];?></span>
         <br><br>
         <label>Comment:</label> <input type="text" name="comment">
         <br><br>
         <label>Gender:</label>
         <input type="radio" name="gender" value="female">Female
         <input type="radio" name="gender" value="male">Male
         <span class="error">* <?php echo $_SESSION['gender'];?></span>
         <br><br>
         <input type="submit" name="submit" value="Submit"> 
    </form>
</body>
</html>
<?php 
unset($_SESSION['name']);
unset($_SESSION['email']);
unset($_SESSION['website']);
unset($_SESSION['comment']);
unset($_SESSION['gender']);
?>

1.php

<?php
session_start();
$nameErr = $emailErr = $genderErr = $websiteErr = "";
$name = $email = $gender = $comment = $website = "";


if ($_SERVER["REQUEST_METHOD"] == "POST") {

     $name = test_input($_POST["name"]);
     // check if name only contains letters and whitespace
     if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
          {$_SESSION['name']= "Name is requiredassssssssssssssss";}
     }

     $email = test_input($_POST["email"]);
     // check if name only contains letters and whitespace
     if (!preg_match("/^[a-zA-Z ]*$/",$email)) {
          {$_SESSION['email']= "email is requiredassssssssssssssss";}
     }

     $website = test_input($_POST["website"]);
     // check if name only contains letters and whitespace
     if (!preg_match("/^[a-zA-Z ]*$/",$website)) {
          {$_SESSION['website']= "website is requiredassssssssssssssss";}
     }
}


function test_input($data) {
   $data = trim($data);
   $data = stripslashes($data);
   $data = htmlspecialchars($data);
   return $data;
}

if ((!preg_match("/^[a-zA-Z ]*$/",$name)) ||  (!preg_match("/^[a-zA-Z ]*$/",$email)) || (!preg_match("/^[a-zA-Z ]*$/",$website)))
{
    header("Location: function.php");
}

echo $_POST['name'];
echo $_POST['email'];
echo $_POST['website'];
?>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    donglu7286 donglu7286 2015-12-14 12:22

    Try like this..

    function.php

            <html>
            <head>
            <style>
            .error {color: #FF0000;}
            </style>
            </head>
            <body> 
    
    
            <h2>PHP Form Validation Example</h2>
            <p><span class="error">* required field.</span></p>
            <form method="post" action="1.php"> 
               <label>Name:</label> <input type="text" name="name" value="<?php echo $_SESSION['name_value'];?>" > <span class="error">* <?php echo $_SESSION['name'];?></span>
               <br><br>
               <label>E-mail:</label> <input type="text" name="email" value="<?php echo $_SESSION['email_value'];?>"> <span class="error">* <?php echo $_SESSION['email'];?></span>
               <br><br>
               <label>Website:</label> <input type="text" name="website" value="<?php echo $_SESSION['website_value'];?>"> <span class="error"><?php echo $_SESSION['website'];?></span>
               <br><br>
               <label>Comment:</label> <input type="text" name="comment">
               <br><br>
               <label>Gender:</label>
               <input type="radio" name="gender" value="female">Female
               <input type="radio" name="gender" value="male">Male
               <span class="error">* <?php echo $_SESSION['gender'];?></span>
               <br><br>
               <input type="submit" name="submit" value="Submit"> 
            </form>
            </body>
            </html>
            <?php 
                unset($_SESSION['name']);
                unset($_SESSION['email']);
                unset($_SESSION['website']);
                unset($_SESSION['comment']);
                unset($_SESSION['gender']);
                unset($_SESSION['name_value']);
                unset($_SESSION['email_value']);
                unset($_SESSION['website_value']);
            ?>
    

    1.php

        <?php
        session_start();
        $nameErr = $emailErr = $genderErr = $websiteErr = "";
        $name = $email = $gender = $comment = $website = "";
    
    
        if ($_SERVER["REQUEST_METHOD"] == "POST") {
    
             $name = test_input($_POST["name"]);
             $_SESSION['name_value'] = $name;
             // check if name only contains letters and whitespace
             if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
                  {
                     $_SESSION['name']= "Name is requiredassssssssssssssss";
                     $_SESSION['name_value']= "";
                  }
             }
    
    
             $email = test_input($_POST["email"]);
             $_SESSION['email_value'] = $email;
             // check if name only contains letters and whitespace
             if (!preg_match("/^[a-zA-Z ]*$/",$email)) {
                  {
                    $_SESSION['email']= "email is requiredassssssssssssssss";
                    $_SESSION['email_value']= "";
                  }
             }
    
             $website = test_input($_POST["website"]);
             $_SESSION['website_value'] = $website;
             // check if name only contains letters and whitespace
             if (!preg_match("/^[a-zA-Z ]*$/",$website)) {
                  {
                    $_SESSION['website']= "website is requiredassssssssssssssss";
                    $_SESSION['website_value']= "";                 
                 }
             }
    
    
           }
    
    
        function test_input($data) {
           $data = trim($data);
           $data = stripslashes($data);
           $data = htmlspecialchars($data);
           return $data;
        }
         if ((!preg_match("/^[a-zA-Z ]*$/",$name)) ||  (!preg_match("/^[a-zA-Z ]*$/",$email)) || (!preg_match("/^[a-zA-Z ]*$/",$website)))
        {
            header("Location: function.php");
        }
    
        echo $_POST['name'];
        echo $_POST['email'];
        echo $_POST['website'];
        ?>
    

    Hope this will help.

    点赞 评论 复制链接分享

相关推荐