Sunday, September 6, 2015

Membuat multi Text Field Sederhana Dengan PHP | Tutorial PHP


Berikut ini skrip bagaimana membuat mutlitextfield sederhana dengan php menggunakan pengulangan for(). Dimana user dapat memilih berapa jumlah data yang akan dimasukkan melalui sebuah list menu. Kemudian dengan menggunakan foreach(), data tersebut kita tampilkan. Berikut skripnya :

<?php 
   if(isset($_POST["button2"]) and $_POST["button2"] == "Proses"){ 
      unset($_POST["jml"]); 
   } 
?> 
<html> 
<head> 
<title>Multi TextField</title> 
</head> 

<body> 
<form id="form1" name="form1" method="post" action=""> 
<table width="100%" border="0" cellpadding="2"> 
   <tr> 
      <td>Berapa Jumlah Text field 
      <select name="jml" id="jml"> 
      <?php for ($jml=1; $jml < 30; $jml++){ ?> 
         <option value="<?php echo $jml; ?>"><?php echo $jml; ?></option> 
      <?php } ?> 
      </select> 
      <input type="submit" name="button" id="button" value="Add Text Field" /> 
      </td> 
   </tr> 
   <tr> 
      <td><hr /></td> 
   </tr> 
   <?php for ($text=0; $text < $_POST["jml"]; $text++){ ?> 
   <tr> 
      <td>Text Field: <?php echo ($text+1); ?> 
         <input type="text" name="nama[]" id="nama[]"></td> 
   </tr> 
   <?php } ?> 
   <?php if(isset($_POST["jml"])){ ?> 
   <tr> 
      <td> 
         <input type="submit" name="button2" id="button2" value="Proses"> 
      </td> 
   </tr> 
   <?php } ?> 
   <tr> 
      <td> 
      <?php 
      if (isset($_POST["button2"]) and $_POST["button2"] == "Proses"){ 
         if (is_array($_POST["nama"])){ 
            foreach ($_POST["nama"] as $value){ 
               if (!empty($value)){ 
                  echo "Teman anda, "; 
                  echo $value; 
                  echo "<br />"; 
               } 
            } 
         } 
      } 
      ?> 
      </td> 
   </tr> 
</table> 
</form> 
</body> 
</html>

Previous Post
Next Post

post written by:

0 komentar: