Search
 
SCRIPT & CODE EXAMPLE
 

PHP

Secured PHP Contact Form

<?php
  if(isset($_POST['submit'])){
    $name = htmlspecialchars(stripslashes(trim($_POST['name'])));
    $subject = htmlspecialchars(stripslashes(trim($_POST['subject'])));
    $email = htmlspecialchars(stripslashes(trim($_POST['email'])));
    $message = htmlspecialchars(stripslashes(trim($_POST['message'])));
    if(!preg_match("/^[A-Za-z .'-]+$/", $name)){
      $name_error = 'Invalid name';
    }
    if(!preg_match("/^[A-Za-z .'-]+$/", $subject)){
      $subject_error = 'Invalid subject';
    }
    if(!preg_match("/^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+.[A-Za-z]{2,4}$/", $email)){
      $email_error = 'Invalid email';
    }
    if(strlen($message) === 0){
      $message_error = 'Your message should not be empty';
    }
  }
?>

<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST">
  <label for="name">Name:</label><br>
  <input type="text" name="name">
  <p><?php if(isset($name_error)) echo $name_error; ?></p>
  <label for="subject">Subject:</label><br>
  <input type="text" name="subject">
  <p><?php if(isset($subject_error)) echo $subject_error; ?></p>
  <label for="email">Email:</label><br>
  <input type="text" name="email">
  <p><?php if(isset($email_error)) echo $email_error; ?></p>
  <label for="message">Message:</label><br>
  <textarea name="message"></textarea>
  <p><?php if(isset($message_error)) echo $message_error; ?></p>
  <input type="submit" name="submit" value="Submit">
  <?php 
    if(isset($_POST['submit']) && !isset($name_error) && !isset($subject_error) && !isset($email_error) && !isset($message_error)){
      $to = 'youremail@addres.com'; // edit here
      $body = " Name: $name
 E-mail: $email
 Message:
 $message";
      if(mail($to, $subject, $body)){
        echo '<p style="color: green">Message sent</p>';
      }else{
        echo '<p>Error occurred, please try again later</p>';
      }
    }
  ?>
</form>
Comment

PREVIOUS NEXT
Code Example
Php :: how to type casting and overriding in php 
Php :: amazon ami 2 php ini 
Php :: php type generic object 
Php :: php populate select from array 
Php :: Définir un nombre maximum de mots sur les titres des publications WordPress 
Php :: wc php get shipping address street 
Php :: install php56-php-ldap on ubuntu 20.04 
Php :: php collapse common columns in associative array 
Php :: download yii 1.1 
Php :: PHP how to skip file upload if file already exist 
Php :: query for current editing post id 
Php :: afiseaza id-ul sesiunii php 
Php :: get one random post wp 
Php :: 0 == "string" php 
Php :: dump request in ci 
Php :: PHP 7 PDF page group - sizeof(): Parameter must be an array or an object that implements Countable 
Php :: wp register_setting access saved value 
Php :: image upload in cake 2 
Php :: BelongsToMany relations pivot fields are null in Livewire refresh 
Php :: php validation form 
Php :: php random number routing 
Php :: PHP SimpleXML - Get Node Values 
Php :: laravel retain old value 
Php :: laravel view-model 
Php :: php tree function parrent_id 
Php :: Laravel - How to create custom configuration variables and access 
Php :: php endif endforeach endwhile 
Php :: Extract all audio tracks / streams 
Php :: create new laravel project 
Php :: laravel file permission denied 
ADD CONTENT
Topic
Content
Source link
Name
7+1 =