PHP Contact form

I need help setting this contact form up.

The HTML markup is

[code]










Sign in
[/code] PHP [php]<?php

$name = $_POST[‘name’];
$email = $_POST[‘email’];
$phone = $_POST[‘phone’];
$address = $_POST[‘address’];
$company = $_POST[‘company’];
$message = $_POST[‘message’];

// echo “name: $name
email: $email
message: $message”;

$to = "[email protected]";
$subject = “Web contact form”;

if (filter_var($email, FILTER_VALIDATE_EMAIL)) {

$from = "From: <$email>";

if(mail($to, $subject, $message, $from)) {
	header("location: success.html");
} else {
	header("location: failure.html");
}

} else {
header(“location: bad_email.html”);
}
?>[/php]

First, make your life easier by using PHPMailer https://github.com/Synchro/PHPMailer, for it’s easy to setup and it’s secure. Why re-invent the wheel.

Second, unless you’re using JavaScript (or a JavaScript Library (Framework)) then don’t use the tag use the tag.

I disagree with Strider on the tags. They are very useful if graphics are placed into them.

But, please place your code inside the tags using the button above. It makes it much
easier for us to place in our apps for testing.

Now, I do not use PHPMailer because it is a higher-end system and is not needed for most of my small
email uses. I think Strider is correct in suggesting it if you plan to do a lot of emails and have a formal
server set up. Anyway, you have set up an email to work in a simple form. No need for complex libraries
to send that email out.

We can only guess of your error as you did not tell us what the problem is. A few ideas to start with:
First, was the [email protected] a real address or did you stick that in the code to hide your email
address. Next, you have tried to debug it with the “echo” line that is commented out. Did that show
the correct data being retrieved from the form info? Lastly, are you running these pages from a server
set up with PHP on it and with the mailing options turned on? (Not all PHP servers have emails set up
by default.) Give us a little more info for us to help you.

He’s missing the headers. Without them, most servers will reject them before its even sent.

As for the button thing, I find most forms won’t even use it for submission, unless you’re using jquery to post to a database or something. I think you just solved an issue I’ve been trying to figure out though, thanks :slight_smile:

richei, I used buttons on many sites without issues. You have to make sure they are typed as submit’s.
Of course, now with most browser’s use of HTML5, you can just use some nice CSS to spruce up most
anything to use as a submit button…

Hi, Thank you for the link and help. I am unsure how to place the PHPMailer to work.

Oh, and YES, he did not show any headers or doctypes, etc. I assumed this was just a sample of what
he was really using.

So, Grazen, was this all of your code? Are they two separate files?
Give us a little more info on your project. Thanks!

to ErnieAlex

All I want is the contact form to have

-Name
[email protected]
-telephone
-address
-company
-business inquiry (which is just a textarea)

I would like help for this to work.

Thanks

the lack of the type was probably my issue then.

Richei, was a long time ago I used the button tag. I looked at the code.
So, the problem using button tags is that they do not have any value.
This means that they can be used as a posted item using a name.
But, you have to code the value of it in a hidden field. Silly way to have to do it.

The nice thing is that you can put a lot into the button tag using CSS to animate or whatever you
would like for a button… Nice option… Off topic for this thread, but, here is a sample of it…

Submit!

Check for “ButtonPressed” value or isset(submitvalue) not button’s info…

I don’t know if there’s more code on the bottom of the php, but the ?> is also missing.

Grazen? Gra? Same person, I suspect you logged in as a guest both times under two names…

So, a simple contact form is basically what you had at the beginning.
Since you did not show a full HTML page for the first part, we were not sure
what was not working. Also, you use three output pages depending on the results.
This is not the normal way you would want to handle this.

First, a couple of questions and then we will help you. Is this for a classroom project?
Next, is it a site with a lot of pages and you are just showing us part of them?
Lastly, do you want a solution or want to learn how to do it? If you want to learn, then
I will show you some sites to explain each step of the code. You actually have most of it
already in place.

From your posted code, you appear to be new to PHP. Let us know where this project is heading
as we will help you get it working…

he was a guest in the first post, then had to create an accout to respond to our answers. why he didnt use the same name idk.

Yep, I was pointing that out to him more than you… LOL I don’t mind helping him, but, want
to know where he is heading with it. Would make a difference if he is just learning a one page
contact form or if he needs it to be part of a more complex site…

Yes, I would like to learn. And yes it’s for a class project

OK

First things first, I had some problems with the signing up, so I have 2 names because GRA didn’t think it was working. Don’t ask me why, but I am interesting in learning PHP, I am interested in it.

Like I said before I wish to know what to do for the contact form to work for my class project.

I was assigned to have

Name
Email
Address
Company
Textarea

Okay, since it is for a class, I will let you do the work. But, I will help you.
First, there are simply two parts to this project.

The first is a simple forms page. The forms page holds your input fields as I think you already know
about. In that part, there is a form and the fields placed inside the form.

The next part is the PHP programming that reads the form fields and load them into the email.
And, of course sends it out. So, it is very simple to do this. You can place the PHP code “inline”
with the HTML code and have it post to itself or you can use a second page.

To make it simpler for you to understand, we will use two pages. the first we can call contact.html
or if you wish, contact.php. (Even though there will be no PHP code in that page.

The second we can call sendmail.php and it will take the data and send it out.

Not really much to this system, just two pages. Lets just get the first page completed first.
Make a simple form with input fields of the items you want to view. Here is a site that explains
it somewhat. Should help… http://www.w3schools.com/html/html_forms.asp
The action field for the form will be “sendmail.php” as a name which is our second page.

So, get that page finished and post it to see what it looks like. (Use the PHP tags button!)

Now onto the second page…
This is just the processing part of the code. It will pull the data from the forms page. It will take
the data and use it in a simple contact form. Therefore, there will be two parts in this. First, grab
the posted data from the form. Next, you should validate the data to make sure it is correct and
no programming was inserted by the user. Lastly, you send an email from the “safe” data.

Actually, here is a link that explains how to do this and is very thorough. No need to rehash it when
this site will show you everything in detail.
http://www.w3schools.com/php/php_form_validation.asp

So, that should be enough info to set up the two pages needed. Once you get the two pages set up
and tested, you can post them here and we can help you spruce them up if needed. Good luck!

Grazen, I reread your first posts with the code. Both are nearly good to go.

I would suggest that you debug the first page to see how it works. Make sure all of the data
is sent to the second page. You can do this by just typing in your test data and having the
second page echo out the data. Like: echo $_POST[“email”]; etc for all the fields. Make sure
you are able to get all of the fields in the second page.

You will also have to make some minor changes in your if-then’s for the email to make sure it gets
sent out. Not hard to do. Once you make sure your data is passing from the first page into the
second page, we will help you with the rest…

Sponsor our Newsletter | Privacy Policy | Terms of Service