0

I created a Contact form using HTML and PHP.

i managed to make the fields mandatory using "reguired" in the HTML. however, right now even when i add anything in the Email fild, the submit button works.

i prefer to do this with HTML rather than PHP.

  • my skills in coding is very limited.

HTML Code:

<div class="container contactform center">
<h2 class="text-center  wowload fadeInUp">Get in touch</h2>
  <div class="row wowload fadeInLeftBig">
      <div class="col-sm-6 col-sm-offset-3 col-xs-12">
        <form action="/assets/bootstrap/php/emailscript.php" method="post">
          <input type="text" placeholder="Name" name="Name" required >
          <input type="text" placeholder="Company" name="Company" required >
          <input type="text" placeholder="Email" name="Email" required >
          <input type="text" placeholder="Subject" name="Subject" required >
          <textarea rows="5" placeholder="Message" name="Message" required ></textarea>
          <button type="submit" class="btn btn-primary"><i class="fa fa-paper-plane"></i> Send</button>
        </form>

      </div>
  </div>
</div>

PHP Code:

<?php
if(isset($_POST['Name']))
{
    $name=$_POST['Name'] ;
    $company=$_POST['Company'] ;
    $from=$_POST['Email'] ;
    $message=$_POST['Message'] ;
    $to="contact@ananasmedia.com" ;
    $subject="Website Contact Form" ;

    mail ($to, $subject, $message, "From: " . $from . $name .  $company ) ;

    echo "Sent" ;

}
?>

I want to submit only with valid email form (email@domain.TLD)

Balagosh
  • 41
  • 1
  • 6
  • Do you want to do this on client-side (html) or server-side (php)? – rbr94 Nov 02 '16 at 13:25
  • Welcome to SO. You don't specify what have you tried so far. Post the code you have tried and the errors you have received. Be as specific as possible as it will lead to better answers. – José Luis Nov 02 '16 at 13:26
  • validate from where client/server – Karthi Nov 02 '16 at 13:26
  • What for a validatioon? It the email contains a @ and stuff like that? For that you can use REGEX. Just Google for it, there are tousands of examples. – Twinfriends Nov 02 '16 at 13:26
  • Have you not searched for this first and to resolve it yourself? There is a lot of stuff out there, believe me. Your question shows no effort on your part whatsoever. – Funk Forty Niner Nov 02 '16 at 13:28
  • my coding skills is very limited, i tried nothing yet, i want a simple email validation, i'll try what you suggested here and update – Balagosh Nov 02 '16 at 13:51
  • [UPDATE] finally i managed to validate the email through HTML 'code' – Balagosh Nov 03 '16 at 14:47

3 Answers3

1

You can use for email :

$email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);

and for other input fields use

if(!isset($_POST['fieldName'])) {
    // throw exception or return error
}
Lovepreet Singh
  • 4,792
  • 1
  • 18
  • 36
  • 2
    `isset()` will return true even if it's empty. It only checks if the variable is set, which is always the case upon form submit. `empty()` also checks if there's actually a value in it. – icecub Nov 02 '16 at 13:28
  • Yes `empty()` function can be used to avoid blank or null value – Lovepreet Singh Nov 02 '16 at 15:39
1

In your PHP code:

<?php
if(isset($_POST['Email']) && $_POST['Email']!='')
{
    $name=$_POST['Name'] ;
    $company=$_POST['Company'] ;
    $from=$_POST['Email'] ;
    $message=$_POST['Message'] ;
    $to="contact@website.com" ;
    $subject="Website Contact Form" ;

    mail ($to, $subject, $message, "From: " . $name . $company . $email) ;
    echo "Sent" ;
  }
  else
  {
    echo "Please insert email address."; 
  }
?>
Anshul
  • 116
  • 1
  • 10
0

Can try

  1. required in html

    <input .... name="Name" required>
    
  2. empty in PHP

    if (empty($name) || empty($company) || empty($from) ||empty($message)) {
        # empty
        echo "Empty....";
    } else {
        # send mail
        mail ($to, $subject, $message, "From: " . $name . $company . $email) ;
    }
    

Or can use validation or JavaScript too

beware about SQL Injection

Example of contact form validation

Community
  • 1
  • 1
Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85