fork download
  1. <?php
  2.  
  3. define('CONTACT_LIST_FILE', 'contacts.txt'); // we are setting constant var
  4.  
  5.  
  6. if (isset($_GET['name'])) {
  7. $name = $_GET['name'];
  8. $age = $_GET['age'];
  9. $phone = $_GET['tel'];
  10. $email = $_GET['email'];
  11. $error_msg = array();
  12.  
  13. if (!preg_match('/^[a-z]{3,10}$/i', $name)) {
  14. $error_msg[] = 'Name has to be written between 3 - 10 characters';
  15. }
  16. if (!preg_match('/^[0-9]{2}$/i', $age) || $age < 20 || $age > 30) {
  17. $error_msg[] = 'Age has to between 20 and 30';
  18. }
  19. if (!preg_match('/(?<!\w)(\(?(\+|00)?48\)?)?[ -]?\d{3}[ -]?\d{3}[ -]?\d{3}(?!\w)/', $phone)) {
  20. $error_msg[] = 'Only polish numbers are valid';
  21. }
  22.  
  23. if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  24. $error_msg[] = 'Not valid email address';
  25. }
  26.  
  27. // we are adding new record
  28. if (count($error_msg) == 0) {
  29. $row = $_GET['name'] . ',' . $_GET['age'] . ',' . $_GET['tel'] . ',' . $_GET['email'];
  30. // echo 'added' . $row;
  31. $status = file_put_contents(CONTACT_LIST_FILE, $row, FILE_APPEND);
  32. if ($status !== false) {
  33. echo 'Added ' . $row;
  34. } else {
  35. echo 'Problem with saving data to file';
  36. }
  37. } else {
  38. echo '<p style="color:red">' . implode("</br>", $error_msg) . '</p>';
  39. }
  40. }
  41.  
  42. ?>
  43.  
  44. <table border="1" cellpadding="5" style="margin-bottom: 20px">
  45. <tr>
  46. <th>name</th>
  47. <th>age</th>
  48. <th>phone number</th>
  49. <th>email address</th>
  50. </tr>
  51. <tr>
  52. <?php if (file_exists(CONTACT_LIST_FILE)) {
  53. $contacts = file(CONTACT_LIST_FILE);
  54. foreach ($contacts as $index => $contact) {
  55. $data = explode(',', $contact);
  56. print_r($data);
  57. echo "<tr>
  58. <td>$data[0]</td>
  59. <td>$data[1]</td>
  60. <td>$data[2]</td>
  61. <td>$data[3]</td>
  62. </tr>";
  63.  
  64. }
  65. }
  66.  
  67. ?>
  68. </table>
  69.  
  70. <form>
  71. <label for="name">First name</label>
  72. <input name="name" type="text">
  73. <label for="age">Age</label>
  74. <select name="age">
  75. <?php
  76. for ($i = 20; $i <= 30; $i++) {
  77. echo '<option>' . $i . '</option>';
  78. }
  79.  
  80. ?>
  81. </select>
  82. <br>
  83. <label for="tel">Phone (only polish numbers are valid)</label>
  84. <input name="tel"/>
  85. <br>
  86. <label for="email">Email</label>
  87. <input name="email"/>
  88. <br>
  89. <button name="add">ADD</button>
  90.  
  91. </form>
Success #stdin #stdout 0.02s 23804KB
stdin
Standard input is empty
stdout
<table border="1" cellpadding="5" style="margin-bottom: 20px">
    <tr>
        <th>name</th>
        <th>age</th>
        <th>phone number</th>
        <th>email address</th>
    </tr>
    <tr>
        </table>

<form>
    <label for="name">First name</label>
    <input name="name" type="text">
    <label for="age">Age</label>
    <select name="age">
        <option>20</option><option>21</option><option>22</option><option>23</option><option>24</option><option>25</option><option>26</option><option>27</option><option>28</option><option>29</option><option>30</option>    </select>
    <br>
    <label for="tel">Phone (only polish numbers are valid)</label>
    <input name="tel"/>
    <br>
    <label for="email">Email</label>
    <input name="email"/>
    <br>
    <button name="add">ADD</button>

</form>