Contact form with required fields, email check and thank you page

Hello,

  1. I want to add required fields to my contact form for “first name”, “last name”, “email address”, “type”, “subject” and “message”. 2) Furthermore, a check that email is typed in correctly with an @. 3) Also is there a way to design the “thank you” page or remain on the contact form page after message sent with a confirmation given there?

I don’t know much about PHP, and it took me more than one week to get the form below working. Therefore, I would so much appreciate the help if somebody may just add the required code.

In advance thank you so much.

PHP code:

[php]<?php
$companyname = $_POST[‘companyname’];
$firstname = $_POST[‘firstname’];
$lastname = $_POST[‘lastname’];
$email = $_POST[‘email’];
$phone = $_POST[‘phone’];
$website = $_POST[‘website’];
$type = $_POST[‘type’];
$subject = $_POST[‘subject’];
$message = $_POST[‘message’];
$formcontent=" Company name: $companyname \n First name: $firstname \n Last name: $lastname \n Phone: $phone \n Website: $website \n Type: $type \n Subject: $subject \n Message: $message";
$recipient = "[email protected]";
$subject = “x.com Contact Us”;
$mailheader = “From: $email \r\n”;
mail($recipient, $subject, $formcontent, $mailheader) or die(“Error!”);
echo “Thank you. Your e-mail has been sent.” . " -" . " Return Home";
?>
[/php]

HTML code:

[php]*
Required fields



Company name





First name*





Last name*





Email address*





Phone





Website





Type*





Customer
Media
Investor
Distributor
Wholesaler
Retailer
Other




Your message relates to*





Brand enquiry
Company information
Media enquiry
Investor enquiry
Buy products
Sell products
Website feedback
Other




Message










[/php]

http://www.w3schools.com/tags/att_input_required.asp

Hi Kevin,

I tried it, but it doesn’t work. Any other advise or may you modify the code above?

Good example what I mean: Unilever . com Contact Form

Also any chance to integrate a captcha to block robots?

Thank you,

Marcus

How does it not work? Show me what you did.

First name*

Thats what I did and it didn’t work. As mentioned i never really used PHP before and the script i have took me several days to get it working.

Many thanks.

Where is the rest of the form?

The rest is all the same as above.

mail() doesn’t error… so,
[php]mail($recipient, $subject, $formcontent, $mailheader) or die(“Error!”);[/php]
is pointless.

You can validate inputs two ways, through javascript, which is not reliable, and through the server. The server is the way to go, it is just a matter of what your requirements are for validation.

[php]
if ( isValid( $_POST ) {
if ( mail() )
// sent
else
// not sent
}[/php]

Figure out what to put in the isValid function

Thank you for your advise. I tried. Unfortunately I don’t know how to integrate this. Can you please give me an example based on my script above? Again, thank you

What did you try to do? Post it.

I added for php:

<?php if (isset($_POST['email'])) { and for html: The form can be still send without email address input. Did I forgot anything?

Post the entire thing. You need a function that validated the fields, isset($_POST[‘email’])) does not validate anything, not does it tell the function what should be returned if there is a problem.

Hello,

this is the entire code for php and html. The form works, but it can be send without completing the required fields such as email… As mentioned, I am new to PHP and HTML just trying to help a friend with the website since I understand Dreamweaver a bit using the design tools only… Thank you for your help.

PHP:

[php]<?php
if (isset($_POST[‘email’]))
{
$companyname = $_POST[‘companyname’];
$firstname = $_POST[‘firstname’];
$lastname = $_POST[‘lastname’];
$email = $_POST[‘email’];
$phone = $_POST[‘phone’];
$website = $_POST[‘website’];
$type = $_POST[‘type’];
$subject = $_POST[‘subject’];
$message = $_POST[‘message’];
$formcontent=" Company name: $companyname \n First name: $firstname \n Last name: $lastname \n Phone: $phone \n Website: $website \n Type: $type \n Subject: $subject \n Message: $message";
$recipient = "[email protected]";
$subject = “XXX.com Contact Us”;
$mailheader = “From: $email \r\n”;
mail($recipient, $subject, $formcontent, $mailheader) or die(“Error!”);
echo “Thank you. Your e-mail has been sent.” . " -" . " Return Home";
}
?>[/php]

HTML:

[php]

Company name

First name*

Last name*

Email address*

Phone

Website

Type*

Consumer Media Investor Distributor Wholesaler Retailer Other

Your message relates to*

Brand enquiry Company information Media enquiry Investor enquiry Buy products Sell products Website feedback Other

Message


[/php]

You don’t need to do this,
[php] $companyname = $_POST[‘companyname’];
$firstname = $_POST[‘firstname’];[/php]

But something like this validates.

[php]function isValid()
{
$isValid = true;
$requiredFields = array(
‘email’
);

foreach ($_POST as $name => $value) {
    if (in_array($name, $requiredFields)) {
        if ($name == 'email') {
            // validating a could be email address
            if (! filter_var($value, FILTER_VALIDATE_EMAIL)) {
                $isValid = false;
            }
        }
    }
}
return $isValid;

}

if ($_SERVER[‘REQUEST_METHOD’] == ‘POST’) {

if (isValid()) {
    
    if (mail())
        echo "Mail sent";
    else
        echo "An error occured sending your form.";
}

}[/php]

Hello, thank you. Unfortunately, I don’t know how to integrate this. As mentioned several times I am complete new to PHP and just want to help a friend. I am not here to learn coding. If you send me only a code piece I really don’t know what to do with it. I would really appreciate some help to finalise this form. Thank you so much Marcus

You are trying to help, but don’t know how. And you have no intention of learning to do it in your own. So, you want us to do it for you, so you can say, look what I did?

If you’re not here to learn then you should just hire somebody.

Sponsor our Newsletter | Privacy Policy | Terms of Service