php contact form help

Discuss how to use and promote Web standards with the Mozilla Gecko engine.
Post Reply
Bethrezen
Posts: 445
Joined: September 13th, 2003, 11:56 am

php contact form help

Post by Bethrezen »

Hi all

Ok so I’m just learning php and I though it would be a good idea to see if I can create a contact form and php processor script, as this is something that a lot of people ask for however it’s not something I was shown how to do during my HND so I need a little help as my knowledge of php is limited.

Ok so here is the code for the form.

Code: Select all

<form action="test.php" method="post" name="contact">

<p>Name:
<!-- Prevent the browser from deleting the contents of the field if the form didn’t send due to an error. -->
<!-- strip_tags attempts to remove any HTML or PHP tags from the string, before repopulating the field -->
<br><input type="text" name="name" <?php if(isset($_POST['name']) == true && $sent == false){echo'value="', strip_tags($_POST['name']), '"';} ?>></p>

<p>Email Address:
<!-- Prevent the browser from deleting the contents of the field if the form didn’t send due to an error. -->
<!-- strip_tags attempts to remove any HTML or PHP tags from the string, before repopulating the field -->
<br><input type="text" name="email" <?php if(isset($_POST['email']) == true && $sent == false){echo'value="', strip_tags($_POST['email']), '"';} ?>></p>

<p>Subject:
<!-- Prevent the browser from deleting the contents of the field if the form didn’t send due to an error. -->
<!-- strip_tags attempts to remove any HTML or PHP tags from the string, before repopulating the field -->
<br><input type="text" name="subject" <?php if(isset($_POST['subject']) == true && $sent == false){echo'value="', strip_tags($_POST['subject']), '"';} ?>></p>

<p>Comments:
<!-- Prevent the browser from deleting the contents of the field if the form didn’t send due to an error. -->
<!-- strip_tags attempts to remove any HTML or PHP tags from the string, before repopulating the field -->
<br><textarea name="comments"><?php if(isset($_POST['comments']) == true && $sent == false){echo strip_tags($_POST['comments']);} ?></textarea></p>

<p>What is 2+2:
<br><input type="text" name="antispam"></p>

<p><input type="submit" name="submit" value="Submit"></p>

<?php 

//Display Feedback

if ($empty_error == true)
{
  echo '<p>Error: All fields are requierd.</p>';
}

if($antispam_error == true)
{
  echo '<p>Error: You answered the anti-spam question incorrectly!</p>';
}

if($sent == true)
{
  echo '<p>Your message has been sent!</p>';
}

?>

</form>
And here is what I have for the PHP so far

Code: Select all

<?php
  
  //define and set variables
  $empty_error = false;
  $antispam_error = false;
  $sent = false;
  
  $name = $_POST['name'];
  $email_adress = $_POST['email'];
  $subject = $_POST['subject'];
  $comments = $_POST['comments'];
  $antispam = $_POST['antispam'];
  
  $to = 'your email address';
  $from = "From: " . $email_adress;

  $message = "From: $name \r\n E-Mail: $email_adress \r\n Subject: $subject \r\n Message: \r\n $comments";

  //check if the submit button has been pressed.
  if(isset($_POST['submit']))
  {
    //check if any of the input fields are empty.
    if(empty($_POST['name'])
    || empty($_POST['email'])
    || empty($_POST['subject'])
    || empty($_POST['comments'])
    || empty($_POST['antispam']))
    {
      $empty_error = true;
    }

    else
    {  
      //check if the anti-spam question has been answered correctly.
      if($antispam != '4')
      {
        $antispam_error = true;
      }
      
      else
      {
        //If form is filled out correctly send message.
        mail ($to, $subject, $message, $from);
        $sent = true;
      }
    }
  }

?>
Now as you can see from this code I’ve made a reasonable start, but what I’m unsure of is how to validate the users input, from each of the fields, because obviously whenever you are dealing with user input it’s simply good practice to validate the input as it helps to prevent a whole bunch issues.

The problem I’ve ran across is trying to find a good tutorial that explains how to do this correctly and not only explains how to do it, but also explains why and what the code is doing.

Another issue I encountered is that when I uploaded this and change

$to = 'your email adress';

to my email address and I try to send a message, the script seems to execute correctly and I get the message sent conformation but the email doesn’t arrive in my inbox and I’m wondering if anyone might know why? Because I have no idea what’s causing the problem nor how to find out what’s causing it, which makes fixing the problem extremely difficult.

I don’t think the problem is my code because when I do

echo $message;

I get From: test E-Mail: test@test.com Subject: test Message: test

So it’s grabbing the contents of the form ok, and I'm not getting any error messages when I run the script, so why its not arriving in my inbox is a mystery.
User avatar
Frenzie
Posts: 2135
Joined: May 5th, 2004, 10:40 am
Location: Belgium
Contact:

Re: php contact form help

Post by Frenzie »

Depending on your use case certain characters have to be escaped to prevent them from doing things.
Intelligent alien life does exist, otherwise they would have contacted us.
Post Reply