Hi guys!
I have worked on some php code on making my contact form work...
I stumbled on a few questions:

  • I can't figure out why the contact form doesn't send me a notification email telling me someone submitted a form.

  • I don't know how to send a user a message through their email saying that we got their form submitted.

  • I can't figure out how to put a message in the same window saying "Thanks for Submitting Form" without putting this on another page.

  • I can't figure out how to make fields required.

  • I can't figure out how to allow user's to only send form once.

  • I can't figure out a solution in putting ways in checking for real emails, so I don't get spammed by users sending fake emails (the email form has to have the @ symbol in order for it to work).

I know I am asking a lot of ridiculous questions... but I really need someone to help me here... I will paste my code here if you need it...

Please update me if you guys don't get my questions

Please respond a.s.a.p!
Thanks!

I can't figure out why the contact form doesn't send me a notification email telling me someone submitted a form.

You need to call in (include/require_once) when the form is submitted, which triggers an email

I don't know how to send a user a message through their email saying that we got their form submitted.

As above

I can't figure out how to put a message in the same window saying "Thanks for Submitting Form" without putting this on another page.

You could use if(!isset()) so if the form is not submitted you display the form, else you show the "Thanks", with a timed re direct(?).

I can't figure out how to make fields required.

Basic HTML fields????

I can't figure out a solution in putting ways in checking for real emails, so I don't get spammed by users sending fake emails (the email form has to have the @ symbol in order for it to work).

Click Here

I can't figure out how to allow user's to only send form once.

Login some kind of DB table

If you need help to develop this drop me a PM with further details

figure out how to put a message in the same window saying "Thanks for Submitting Form" without putting this on another page.

I can't figure out how to make fields required.

Hi,
You can use php mail function for sending emails and for validation the fields you can use php validation or jquery validation. In jquery validation you can set the fields that are required as well as email validation of the form.You can see the jquery validation link . For the message displaying on the same page you can set arguments when the form is submitted. For example you can redirect your page like on success

 header('location:contact.php?message=success')

.
You can put a condition on the page that

if(isset($_GET['message'])=='success'){
echo 'Your email has been successfully send';
}

You can also put a check for sending the email only once that if success message is set then dont send the email.

Thanks squidge, but can you write a snippet of code so I can see what the code looks like on each one... I'm still new to php, so it's kinda hard to adapt to.

saadi, I am still trying out your solution... I will notify you when I get it sorted out:)

Saadi.. is there a way in not using JQUERY? just php only?? and here is my code:

<?php
       $name = $_POST['name'];
       $email = $_POST['email'];
       $comment = $_POST['comment'];
            $from = 'From: My Contact Form';
            $to = 'me@email.com';
       $subject = 'Hi?';

                $body = "From: $name\n E-Mail: $email\n Comment:\n $comment";

   if ($_POST['submit']) {
             if (mail ($to, $subject, $body, $from)) {
                    echo '<p>Message Sent Successfully!</p>';
                        } else {
                    echo '<p>Try again, please?</p>';
                        }
                }
?>

where would I place the code?

Yes there are ways.You have to make a code like this

if(isset($_POST['submit']))
{
  $error = '';
  if($_POST['name']=='')
  {

   $error .= 'Please enter your name';
  }
  and so on for other validations.
}

for email validation you can use this function

function valid_email($email) {
  $regexp="/^[a-z0-9]+([_\\.-][a-z0-9]+)*@([a-z0-9]+([\.-][a-z0-9]+)*)+\\.[a-z]{2,}$/i";
  if ( !preg_match($regexp, $email) ) {
       $_obweb->addErr("Email address is not correct\n");
       return false;
  }
  return true;
}

Now you can check if the error variable is empty you can send the email else you can send the email

Can you explain to me what that second line does in email validation?

saadi, the 2 snippets of code don't work... Shall I post what my code looks like?

The second line of code compares the email paatern like abc@xyz.com and please paste your code so I can tell you where you are wrong

Here is my code

<?php
       $name = $_POST['name'];
       $email = $_POST['email'];
       $comment = $_POST['comment'];
       $phone = $_POST['phone'];

            $from = 'From: Form Submission';
            $to = 'email@gmail/yahoo/etc.com';
       $subject = 'Form Has Been Submitted - FRONT PAGE';

       $body = " Form is Submitted by $name\n E-Mail: $email\n Phone: $phone\n Comments:\n $comment";

   if ($_POST['submit']) {
             if (mail ($to, $subject, $body, $from)) {
                    echo '<p>Message Sent Successfully!</p>';
                        } else {
                    echo '<p>Your Message was not submitted -  Please Try again!</p>';
                        }
                }
?>

<?php
//validation
$name = $_POST['name'];
$email = $_POST['email'];


//Filter Name
if(!filter_var($name, FILTER_DEFAULT))
    {
        echo "Name is not valid";
        }

//Filter Email
if(!filter_var($email, FILTER_VALIDATE_EMAIL))
  {
  echo "E-mail is not valid";
  }

//Filter Phone
if(!filter_var($email, FILTER_DEFAULT))
{
    echo "Phone is not valid";
    }

?>

The following is my suggestion (maybe not perfect but works):

// first check if the form was submitted
if(isset($_POST['submit'])) {

    // initialize the array that will hold potential error messages
    $errors = array();

    // initialize variables (for filling-in fields in case of errors)
    $name = $_POST['name'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];

    // if the form was submitted do the validation, assign variables and prepare
    // potential error messages

    // Filter Email
    // you can also use the function from the saadi06 post above
    if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {

        $errors[] = 'E-mail is not valid';
    }

    // Filter name
    if(!filter_var($name, FILTER_DEFAULT)) {

        $errors[] = 'Name is not valid';
    }

    // Filter Phone (I doubt this is an appropriate filter for phone numbers)
    // you better make up a function for checking phone numbers in your country
    if(!filter_var($phone, FILTER_DEFAULT)) {

        $errors[] = 'Phone is not valid';
    }

    // comment text 
    $comment = $_POST['comment'];

    // if there were no errors (the errors array is empty) send the mail,
    // display the successful message and stop the script
    if(empty($errors)) {

        $from = 'From: Form Submission';
        $to = 'email@gmail/yahoo/etc.com';
        $subject = 'Form Has Been Submitted - FRONT PAGE';
        $body = " Form is Submitted by $name\n E-Mail: $email\n Phone: $phone\n Comments:\n $comment";

        // mail($to, $subject, $body, $from);

        die('<p>Message Sent Successfully!</p>');
    }
}

// start the form, method is post, action is # (same page)
echo '<form method="post" action="#">';

// add the input element for name
echo 'Name: <input type="text" name="name" value="' . $name . '" /><br />';

// add the input element for email
echo 'Email: <input type="text" name="email" value="' . $email . '" /><br />';

// add the input element for phone
echo 'Phone: <input type="text" name="phone" value="' . $phone . '" /><br />';

// add the textarea element for comment
echo 'Comment: <textarea name="comment">' . $comment . '</textarea><br />';

// add the input element for submitting
echo '<input type="submit" name="submit" value="Submit" />';

// end the form
echo '</form>';

// if there were errors, display them below the form
if(!empty($errors)) {

    echo '<p>There were the following errors:</p>';

    foreach($errors as $error_message) {

        echo "<p>$error_message</p>";
    }

    echo '<p>Your Message was not submitted -  Please Try again!</p>';
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.