The first time I had to build a contact page I felt like an idiot. I had built a contact page in JavaScript class but it did not post anywhere. So I did not have a clue on how I was going to get the information from my contact page into storage so I could know what was on it.
I had also taken a class on MySql database management. And at the very end of the book we built a couple of forms and used something called PHP to submit them but they did not go any where. So back to my opening statement I had a client and I could build the contact page for them in HTML and JavaScript but beyond that I was lost.
So I don’t know about you but I get a lot of great information from You-tube. I searched the internet feverously to find someone to tell me how to submit the information on a contact page, and where it should go. What I found was either no one else knew or they just were not talking about it. So I landed on You-tube and found out you need to use both a database and PHP to submit the information on a contact page. The reason for the database is so you can save the information and go back to review it anytime you wish. And PHP which is a server side language which I believe would make it more secure to submit personal information. I also wanted to go one step beyond and have an email sent to me telling me when some one filled out my contact page.
The first thing I did was to install Xampp on my computer so I could write and test my PHP programs, also Xampp comes with PHPMyAdmin a PHP driven control panel so you can create and control databases, remember I wanted a place to store my contact information whenever someone filled out my contact page. You can download Xampp by clicking on the word Xampp. Once I had everything installed I opened Crimson Editor and opened my contact page. I went just before the form part of my HTML and inserted this code. <form action =” contact.php” method=”post”> what this line does is to tell the contact page to post the contact information in the fields to contact.php which is another page which will take the information and submit it to my database and email me the information also.
So let us look at the contact. PHP page and see what that looks like to get an idea how to lay out such a page.
<?php
$con = mysql_connect(“localhost”,”your db name”,”your db password”);
if (!$con)
{
die(‘Could not connect: ‘ . mysql_error());
}
The very first line tells the browser that the program is written in PHP.
This first part allows you to connect to the database and if you cant not it will print an error message and stop trying to connect.
This next part if we are connected will go to the databases table and insert the information into the database.
mysql_select_db(“whatever_db”, $con);
As you can see here I am telling the program to insert into the following columns the information from the matching text fields on the contact page.
$sql=”INSERT INTO contact (id, first_name, last_name, address, city, state, zip, area, exchange, phone, email_info, contact, services, comments)
This next line post the values from the contact page into the proper columns of the database.
VALUES
(‘$_POST[id]‘,’$_POST[first_name]‘,’$_POST[last_name]‘,’$_POST[address]‘,’$_POST[city]‘,’$_POST[state]‘,’$_POST[zip]‘,’$_POST[area]‘,’$_POST[exchange]‘,’$_POST[phone]‘,’$_POST[email_info]‘,’$_POST[contact]‘,’$_POST[services]‘,’$_POST[comments]‘ )”;
I then tell the program if there is problem to just stop the program and send an error message.
if (!mysql_query($sql,$con))
{
die(‘Error: ‘ . mysql_error());
}
But if every thing went fine I tell the program to close my database so no one can get into it.
mysql_close($con);
I did not want the information just sent to me through regular email so I downloaded an library called PHPMailer. By using PHPMailer I am able to send the contact information to me in a more secure email system. Just upload PHPMailer to your hosting and place it the same folder as your website and use programming like what I am going to show you and it will do the rest for you.
include_once(“phpmailer/class.phpmailer.php”);
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
I used Gmail as my smtp server.
$mail->Host = “smtp.gmail.com”;
$mail->SMTPSecure = “ssl”;
Port 465 is the one used for gmail.
$mail->Port = 465;
Next enter your email user name and password.
$mail->Username = “whatever@gmail.com”;
$mail->Password = “whatever@gmail.com password”;
$mail->AddAddress(“address of you you want to say it is from”);
This is the page the mail is coming from.
$mail->From = “Whatever.Page@gmail.com”;
$mail->FromName = $_POST['name'];
I use the subject line to let me know some one filled out my contact page.
$mail->Subject = “Someone has filled out your contact page!”;
$mail->IsHTML(true);
I then tell the program to insert the information from the database into the email.
$mail->Body = ‘First Name: ‘. $_POST['first_name' ] . “\n” . “—” . ‘Last Name: ‘. $_POST['last_name']. “\n” . “—” . ‘Address: ‘.$_POST['address'] . “\n” . “—” . ‘City: ‘.$_POST['city'] . “\n” . “—” . ‘State: ‘.$_POST['state'] . “\n” . “—” . ‘Zip: ‘.$_POST['zip'] . “\n” . “—” . ‘Area: ‘.$_POST['area'] . “\n” . “—” . ‘Exchange: ‘.$_POST['exchange'] . “\n” . “—” . ‘Phone: ‘.$_POST['phone'] . “\n” . “—” . ‘Email Info: ‘.$_POST['email_info'] . “\n” . “—” . ‘Contact: ‘.$_POST['contact'] . “\n” . “—” . ‘Services: ‘.$_POST['services'] . “\n” . “—” . ‘Comments: ‘. $_POST['comments'];
if($mail->Send()) {
echo “Thank you for contacting us! We will get back to you as soon as possible!”;
}
Oh and do not forget to close the program with the closing php tag and your closing ; at the end of the PHP lines.
?>
There that should give you some idea on how to make your post page so you can save your contact information. And of course you are always free to email me and ask me questions by using my contact page.