Ví dụ dưới đây sẽ nhận input field dạng text, radio button, dropdown menu, và checked box.

Ví dụ

<html>
   
   <head>
      <style>
         .error {color: #FF0000;}
      </style>
   </head>
   
   <body> 
      <?php
         // định nghĩa các biến và gán giá trị rỗng cho biến
         $nameErr = $emailErr = $genderErr = $websiteErr = "";
         $name = $email = $gender = $class = $course = $subject = "";
         
         if ($_SERVER["REQUEST_METHOD"] == "POST") {
            if (empty($_POST["name"])) {
               $nameErr = "Name is required";
            }
            else 
            {
               $name = test_input($_POST["name"]);
            }
            
            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["course"])) {
               $course = "";
            }
            else
            {
               $course = test_input($_POST["course"]);
            }
            
            if (empty($_POST["class"])) {
               $class = "";
            }
            else 
            {
               $class = test_input($_POST["class"]);
            }
            
            if (empty($_POST["gender"])) {
               $genderErr = "Gender is required";
            }
            else
            {
               $gender = test_input($_POST["gender"]);
            }
            
            if (empty($_POST["subject"])) {
               $subjectErr = "You must select 1 or more";
            }
            else 
            {
               $subject = $_POST["subject"];	
            }
         }
         
         function test_input($data) {
            $data = trim($data);
            $data = stripslashes($data);
            $data = htmlspecialchars($data);
            return $data;
         }
      ?>
		
      <h2>Mẫu đăng ký lớp học lập trình</h2>
      
      <p><span class="error">* các trường bắt buộc.</span></p>
      
      <form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
         <table>
            <tr>
               <td>Họ tên:</td>
               <td><input type="text" name="name">
                  <span class="error">* <?php echo $nameErr;?></span>
               </td>
            </tr>
            
            <tr>
               <td>E-mail: </td>
               <td><input type="text" name="email">
                  <span class="error">* <?php echo $emailErr;?></span>
               </td>
            </tr>
            
            <tr>
               <td>Thời gian học:</td>
               <td> <input type="text" name="course">
                  <span class="error"><?php echo $websiteErr;?></span>
               </td>
            </tr>
            
            <tr>
               <td>Tên lớp:</td>
               <td> <textarea name="class" rows="5" cols="40"></textarea></td>
            </tr>
            
            <tr>
               <td>Giới tính:</td>
               <td>
                  <input type="radio" name="gender" value="female">Nữ
                  <input type="radio" name="gender" value="male">Nam
                  <span class="error">* <?php echo $genderErr;?></span>
               </td>
            </tr>
            
            <tr>
               <td>Lựa chọn:</td>
               <td>
                  <select name="subject[]" size="4" multiple>
                     <option value="Android">Android</option>
                     <option value="Java">Java</option>
                     <option value="C#">C#</option>
                     <option value="Python">Python</option>
                     <option value="C++">C++</option>
                     <option value="PHP">PHP</option>
                  </select>
               </td>
            </tr>
            
            <tr>
               <td>Tôi đồng ý</td>
               <td><input type="checkbox" name="checked" value="1"></td>
               <?php if(!isset($_POST['checked'])){ ?>
               <span class="error">* <?php echo "Bạn phải đồng ý";?></span>
               <?php } ?> 
            </tr>
            
            <tr>
               <td>
                  <input type="submit" name="submit" value="Submit"> 
               </td>
            </tr>
            
         </table>
      </form>
      
      <?php
         echo "<h2>Thông tin bạn đã cung cấp là:</h2>";
         echo ("<p>Họ tên bạn là $name</p>");
         echo ("<p>Địa chỉ email là $email</p>");
         echo ("<p>Thời gian học là $course</p>");
         echo ("<p>Tên lớp là $class </p>");
         echo ("<p>Bạn là $gender</p>");
         
         for($i=0; $i < count($subject); $i++)
         {
            echo($subject[$i] . " ");
         }
      ?>
      
   </body>
</html>

Lưu chương trình trên trong một file có tên là test.php trong htdocs, sau đó mở trình duyệt và gõ địa chỉ http://localhost:8080/test.php sẽ cho kết quả:

Complete Form trong PHP

Các bài học PHP phổ biến khác tại s2sontech:




Bình luận (0)

Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Learning English Everyday