Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <form id="form" name="contactForm" method="post" action="php/formulario_contactos.php">
- <div>
- <label for="name">Your name</label>
- <input type="text" id="name" name="name" maxlength="40" placeholder="Write your Name">
- <span class="error"><?php echo $nameError; ?></span>
- </div>
- <div>
- <label for="email">Your email</label>
- <input type="email" id="email" name="user_mail" placeholder="email@example.com">
- <span class="error"><?php echo $emailError; ?></span>
- </div>
- <div>
- <label for="topic">Select Topic</label>
- <select id="topic" name="topic">
- <option selected disabled hidden value="">Choose a Topic</option>
- <option value="link">Site Link</option>
- <option value="copyright">Copyright</option>
- <option value="errors">Site/Article errors</option>
- <option value="feedback">Feedback</option>
- <option value="other">Other</option>
- </select>
- <span class="error"><?php echo $topicError; ?></span>
- </div>
- <div>
- <label for="msg">Your message</label>
- <textarea id="msg" name="user_message" placeholder="Write your message"></textarea>
- <span class="error"><?php echo $mensagemError; ?></span>
- </div>
- <div class="button">
- <button type="submit" id="submit">Submit</button>
- <span class="success"><?php echo $successMessage; ?></span>
- </div>
- </form>
- <?php
- $servername = "localhost:3306";
- $username = "root";
- $password = "";
- $dbname = "site_comboios";
- //Create connection
- $conn = new mysqli($servername, $username, $password, $dbname);
- // Check connection
- if ($conn->connect_error) {
- die("Connection failed:" . $conn->connect_error);
- }
- $name = mysqli_real_escape_string($conn,$_POST['name']);
- $email = mysqli_real_escape_string($conn,$_POST['user_mail']);
- $topic = mysqli_real_escape_string($conn,$_POST['topic']);
- $mensagem = mysqli_real_escape_string($conn,$_POST['user_message']);
- $nameError = "";
- $emailError = "";
- $topicError = "";
- $mensagemError = "";
- $successMessage = "";
- if( isset($_POST["submit"])) {
- if(empty($_POST['name'])) {
- $nameError = "Name is required";
- } else {
- $name = test_input($_POST['name']);
- if(!preg_match("/^[a-zA-Z ]*$/", $name)) {
- $name = "Only letters and white space allowed";
- }
- }
- if(empty($_POST['user_mail'])) {
- $emailError = "Email is required";
- } else {
- $email = test_input($_POST['user_mail']);
- }
- if(empty($_POST['topic'])) {
- $topicError = "Choose a category";
- } else {
- $topic = test_input($_POST['topic']);
- }
- if(empty($_POST['user_message'])) {
- $mensagemError = "Please, write your feedback";
- } else {
- $mensagem = test_input($_POST['user_message']);
- }
- }
- function test_input($data)
- {
- $data = trim($data);
- $data =stripslashes($data);
- $data =htmlspecialchars($data);
- return $data;
- }
- $sql = "INSERT INTO formulario_contactos (Nome, Email, Topico, Mensagem)
- VALUES ('$name', '$email' , '$topic' , '$mensagem')";
- if ($conn->query($sql) === TRUE) {
- echo 'Your message has been successfully sent';
- } else {
- echo 'Error'. $sql . $conn->error;
- }
- $conn->close();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement