Advertisement
Guest User

Untitled

a guest
Dec 27th, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. <form id="form" name="contactForm" method="post" action="php/formulario_contactos.php">
  2. <div>
  3. <label for="name">Your name</label>
  4. <input type="text" id="name" name="name" maxlength="40" placeholder="Write your Name">
  5. <span class="error"><?php echo $nameError; ?></span>
  6. </div>
  7. <div>
  8. <label for="email">Your email</label>
  9. <input type="email" id="email" name="user_mail" placeholder="email@example.com">
  10. <span class="error"><?php echo $emailError; ?></span>
  11. </div>
  12. <div>
  13. <label for="topic">Select Topic</label>
  14. <select id="topic" name="topic">
  15. <option selected disabled hidden value="">Choose a Topic</option>
  16. <option value="link">Site Link</option>
  17. <option value="copyright">Copyright</option>
  18. <option value="errors">Site/Article errors</option>
  19. <option value="feedback">Feedback</option>
  20. <option value="other">Other</option>
  21. </select>
  22. <span class="error"><?php echo $topicError; ?></span>
  23. </div>
  24. <div>
  25. <label for="msg">Your message</label>
  26. <textarea id="msg" name="user_message" placeholder="Write your message"></textarea>
  27. <span class="error"><?php echo $mensagemError; ?></span>
  28. </div>
  29. <div class="button">
  30. <button type="submit" id="submit">Submit</button>
  31. <span class="success"><?php echo $successMessage; ?></span>
  32. </div>
  33. </form>
  34.  
  35. <?php
  36.  
  37. $servername = "localhost:3306";
  38. $username = "root";
  39. $password = "";
  40. $dbname = "site_comboios";
  41.  
  42. //Create connection
  43. $conn = new mysqli($servername, $username, $password, $dbname);
  44. // Check connection
  45. if ($conn->connect_error) {
  46. die("Connection failed:" . $conn->connect_error);
  47. }
  48.  
  49. $name = mysqli_real_escape_string($conn,$_POST['name']);
  50. $email = mysqli_real_escape_string($conn,$_POST['user_mail']);
  51. $topic = mysqli_real_escape_string($conn,$_POST['topic']);
  52. $mensagem = mysqli_real_escape_string($conn,$_POST['user_message']);
  53.  
  54.  
  55. $nameError = "";
  56. $emailError = "";
  57. $topicError = "";
  58. $mensagemError = "";
  59. $successMessage = "";
  60.  
  61. if( isset($_POST["submit"])) {
  62. if(empty($_POST['name'])) {
  63. $nameError = "Name is required";
  64. } else {
  65. $name = test_input($_POST['name']);
  66. if(!preg_match("/^[a-zA-Z ]*$/", $name)) {
  67. $name = "Only letters and white space allowed";
  68. }
  69. }
  70.  
  71. if(empty($_POST['user_mail'])) {
  72. $emailError = "Email is required";
  73. } else {
  74. $email = test_input($_POST['user_mail']);
  75. }
  76.  
  77. if(empty($_POST['topic'])) {
  78. $topicError = "Choose a category";
  79. } else {
  80. $topic = test_input($_POST['topic']);
  81. }
  82.  
  83. if(empty($_POST['user_message'])) {
  84. $mensagemError = "Please, write your feedback";
  85. } else {
  86. $mensagem = test_input($_POST['user_message']);
  87. }
  88. }
  89.  
  90. function test_input($data)
  91. {
  92. $data = trim($data);
  93. $data =stripslashes($data);
  94. $data =htmlspecialchars($data);
  95. return $data;
  96. }
  97.  
  98. $sql = "INSERT INTO formulario_contactos (Nome, Email, Topico, Mensagem)
  99. VALUES ('$name', '$email' , '$topic' , '$mensagem')";
  100.  
  101. if ($conn->query($sql) === TRUE) {
  102. echo 'Your message has been successfully sent';
  103. } else {
  104. echo 'Error'. $sql . $conn->error;
  105. }
  106.  
  107. $conn->close();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement