Submitting Forms

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.

Posted in Uncategorized | Tagged , , , , , , , , , , | 4 Comments

Building a Contact Page Part 2

In part one I showed how to use HTML to build a contact page using division tags and tables. I will today show you how to use JavaScript to validate the information on your contact page. Validating makes sure the boxes on your contact page has the required information in them. Also you want to make sure your phone numbers are actually numbers not letters and visa versa.  JavaScript is a little more complicated that HTML. It actually works inside of HTML and cooperates with CSS.  You can use functions with JavaScript. A function in a programming language such as JavaScript is a piece of code that performs a task and just sits there until it is called upon. So I used the following functions which will be called upon when the boxes are filled in. Here are the functions I used.

<script type=”text/javascript”>
/* <![CDATA[ */

function checkForNumber(fieldValue) {
var numberCheck = isNaN(fieldValue);
if (numberCheck == true) {
window.alert("You must enter a numeric value!");
return false;
}
}
function confirmSubmit(){
if (document.forms[0].first_name.value==”"
||document.forms[0].last_name.value==”"
||document.forms[0].address.value==”"
||document.forms[0].city.value==”"
||document.forms[0].state.value==”"
||document.forms[0].zip.value==”"){
window.alert(“You have forgotten to enter some contact information.”);
return false;
}

else if (document.forms[0].area.value==”"
||document.forms[0].exchange.value==”"
||document.forms[0].phone.value==”") {
window.alert(“You must enter your telephone information.”);
return false;
}
else if (document.forms[0].email.value==”"){
window.alert(“You must enter an email address!”);
return false;
}
var contactSelected = false;
for (var i=0; i<3; ++i) {
if (document.forms[0].contact[i].checked == true){
contactSelected = true;
break;
}
}
if (contactSelected != true){
window.alert(“You must select a method to be contacted.”);
return false;
}

var servicesSelected = false;
for (var i=0; i<5; ++i){
if (document.forms[0].services[i].checked == true){
servicesSelected = true;
break;
}
}
if (servicesSelected == false) {
window.alert(“You must select at least one service!”);
return servicesSelected;
}
else
return servicesSelected;

return true;
}

function confirmReset(){
var resetForm = window.confirm(“Are you sure you want to reset the form?”);
if (resetForm == true)
return true;
return false;

}
/* ]]> */
</script>

First we must open up the function section of the program with a opening JavaScript tag. With JavaScript if you place the code in the head it will be able to operate as the page is loading. Since we are using functions which will not operate until the call comes we can place the functions in the head.

The first function is to see if numbers are in the fields where the numbersw should be, if someone tries to enter something besides numbers a warning message will print out.

The next function activates whenever the submit button is clicked and confirms if the required fields are filled in or not. And if the information is not there the function will return false and activate the window. alert and print out the above message

Next we have two else if statements mainly saying if the code ran properly because the contact information is correct, the program goes on to see if the telephone information is filled out correctly.

Next we have a for statement and if statement which looks to see if the way to be contacted has been selected or not.  If it is filled out correctly the code will let off here notice the break statement. If it is not selected an alert will print which tells the person they must select a method way to be contacted.

The last part of the function checks to see if the desired service is selected or not, if it is the program goes on. If it is not filled out correctly the program will print out a warning.

The last function confirmReset will make sure you want to reset the form if you were to hit the reset button. Then we have the closing JavaScript tag.

Now as I said before a function will just sit there and do nothing until it activated or called. So at this time we will see what calls or activates these functions. I place the function call to make sure the required information in the form is where it should be and the call for the reset in the form load or form action section of the program. Both which will be called when the submit and reset button are clicked.

<form action=”conprocess.php” method=”post” enctype=”application/x-www-form-urlencoded” onsubmit=”return confirmSubmit();” onreset=”return confirmReset();” >
 

The first call I placed in the program is in the zip code field I want to make sure it is all numbers so this how you do you fulfill that job. What we are saying is if anything in this field changes go to the checkForNumber function and make sure what is in this field (in bold).

Zip<span>*</span>
<input type=”text” name=”zip” size=”10″ maxlength=”10″  onchange=”return checkForNumber(this.value);” /> </p>

As you can see I did the same in the telephone section.

<p>Telephone <span>*</span>
<input type=”text” name=”area” size=”3″ maxlength=”3″ onchange=”return checkForNumber(this.value);” />
<input type=”text” name=”exchange” size=”3″ maxlength=”3″ onchange=”return checkForNumber(this.value);” />
<input type=”text” name=”phone” size=”4″ maxlength=”4″ onchange=”return checkForNumber(this.value);”  /></p>

 

Well this is a simple example of how to validate information on a contact page using JavaScript, but since JavaScript is what is called a client side programming language I will be showing the next in the next post how to use PHP  on the server side to validate your information. I suggest you always use both client side validation and server side validation when you create an contact form. I hope you have enjoyed this tutorial and that it helps you in your webdesigns. Later Codin

 

 

 

Posted in Uncategorized | 50 Comments

Building a Contact Page. part 1

Last summer I was asked to build a website for a business. I did not have a problem building the site in my favorite text editor, Crimson Editor. The biggest hurdle to overcome was the contact page. Over the next few post I want to share with you the process I went through to build a productive contact page. When a contact page is put together correctly you will be able to collect all the necessary information to be able to conduct your business with whoever contacts you though your website. First you have to install the text boxes and radio buttons necessary to collect information such as phone numbers first and last names and email addresses and etc.

This is the HTML I used to produce the text boxes and radio buttons everything I needed to collect the information I needed.

I decided it was best to build the main body of the contact page as a table with a number 2 border

<table border=”2″>
<tr>
<td valign=”top”>
<fieldset id=”contact”>
<legend>Contact Information</legend>
<p>First Name<span>*</span>
<input type=”text” name=”first_name” size=”50″ /></p>
<p> Last Name<span>*</span>
<input type=”text” name=”last_name” size=”50″ /></p>
<p>Address<span>*</span>
<input type=”text” name=”address” size=”50″ /></p>
<p>City<span>*</span>
<input type=”text” name=”city” size=”34″ />
State<span>*</span>
<input type=”text” name=”state” size=”2″ maxlength=”2″ />
Zip<span>*</span>
<input type=”text” name=”zip” size=”10″ maxlength=”10″      onchange=”return checkForNumber(this.value);” /> </p>
</fieldset>
I also used fieldsets so I could control where all the fields by having them in little blocks. I also used span so I could insert a red * telling people it was required.
<fieldset id=”telephone”>
<legend>Telephone and Email</legend>

<p>Telephone <span>*</span>
<input type=”text” name=”area” size=”3″ maxlength=”3″ onchange=”return checkForNumber(this.value);” />
<input type=”text” name=”exchange” size=”3″ maxlength=”3″ onchange=”return checkForNumber(this.value);” />
<input type=”text” name=”phone” size=”4″ maxlength=”4″ onchange=”return checkForNumber(this.value);”  /></p>

<p>E-mail address<span>*</span>
<input type=”text” name=”email” size=”50″  onclick=”if(this.value==’Enter your email address’) this.value=”;” /></p>
</fieldset>
I also used legend so I could borders around my text areas.
<fieldset id=”method”>
<legend>Contact Method</legend>
<p>How should we contact you?<span>*</span>
<input type=”radio” name=”contact” value=”Phone” />Phone
<input type=”radio” name=”contact” value=”email” />Email
<input type=”radio” name=”contact” value=”fax” />fax
</p>
</fieldset>

<fieldset id=”comments”>
<legend>Comments</legend>

<textarea name=”comments” rows=”10″ cols=”40″>
</textarea>
</fieldset>

</td>
</tr>
</table>
<div id=”buttons”>
<p><input type=”submit” value=”Submit” /></p>
</div>
I also used a standard submit button to be able to submit the information. And a reset button also if it is needed.
<div id=”reset”>
<p> <input type=”reset” value=”Reset” /></p>
</div>

</form>
</div>

And of course I had to close with the usual tags.

</body>

</html>

And this produces a simple little contact page format.

Contact Information

First Name*

Last Name*

Address*

City*

State*
Zip*
Telephone and Email

Telephone *

E-mail address*

Contact Method

How should we contact you?*

Phone
Email
fax
Comments

I hope you realize the size of the text boxes and color such as background are all controlled by my CSS form.

Next I will show you how to validate your contact page with Javascript. You do not want just any ole thing being submitted to you on your website. And some areas such as phone numbers or emails need to make sure they contain what they should such as numbers. A phone number should be 222-444-5566 not asfdsfsdfasdadsf. So next time I will go through how I laid out the validation code.

If you have any questions please feel free to email me through our contact page and I will be glad to answer them.

 

Posted in Uncategorized | Tagged , , , , , | 11 Comments

Code and Webdesigns Says Hello

What is Code and Webdesigns? Code and Webdesigns is my programming and website designing business. My name is Earl Gile and I just finished my associate’s degree in computer programming and web administration. My desire for this blog is to share what I am learning in my never-ending journey to become the best programmer and web designer I can become. I have been work with websites since 2003, but I felt I was not learning what I needed to know so I went to college. Once I was at college I learned to enjoy programming also, so I branch out into programming as well. I learned some programming languages I may never of known about had I not gone to college such as javascript , or java, mysql and other languages. I thought I could just visit sites that have tutorials on webdesign and learn to be web designer. I admit there are sites with tutorials on web design where I did learn and I still visit them from time to time yet today if I get stuck on a problem I can not resolve. I have even watched tutorials on YouTube to resolve programming and web design problems I have ran into.  So you definitely  learn on different sites with tutorials, and where would we all be without programming and web design forums.  But I felt I could get a better education for my goals as a website designer at college. I also buy books on different programming languages and website design to help me grow. I plan to share different tutorials and thoughts on programming and website design as we go along. I hope you enjoy everything you find here.

Posted in Uncategorized | 4 Comments