homepage Welcome to WebmasterWorld Guest from 54.161.175.231
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Basics of Submitting and Emailing Forms with PHP
A reliable simple order form
smukcreative




msg:1296411
 2:54 pm on Oct 28, 2004 (gmt 0)

Hi, can anyone help me regarding a simple order form script? I have been a print and web designer for 6 years and have just started teaching myself to learn programming.

I am still very new and I need some really useful advice or tips on how to go about creating a reliable order form script which allows the user to select the product from a drop down list, key in their personal information and then hit submit.

The receiver will receive the information in an email format. Do you think this is very difficult for someone who is inexperienced in the programming environment?

Thank you for your time reading this. I appreciate it.

 

eggy ricardo




msg:1296412
 3:57 pm on Oct 28, 2004 (gmt 0)

It's a fairly simple thing to do, getting more complex depending on whether you want to check the inputted data and how much HTML you know.

I'm sure there are a lot of online tutorials and probably pre-made scripts for this purpose, or you could get yourself one of those quickstart sort of books which should give you the basics you need. Alternatively you could find someone who is willing to do it for you (free or paid - shouldn't be a big job).

Hope this helps
Cheers
Richard

jatar_k




msg:1296413
 8:18 pm on Oct 28, 2004 (gmt 0)

Welcome to WebmasterWorld smukcreative,

I don't know whether you have done html or not but I am assuming yes. Web Designer is almost completely undefined these days but we can go for a very simple form with only a couple of fields in it.

Basics of Submitting and Emailing Forms with PHP

form.html

<p>Please fill in the following Information
<p><form name="greatpurchase" method="post" action="sendmyorder.php">
<p>First Name <input type="text" name="firstname" size="20">
<p>Last Name <input type="text" name="lastname" size="20">
<p>Email Address <input type="text" name="email" size="50">
<p>Product <select name="product">
<option value="none">Select a Product</option>
<option value="product1">Product 1</option>
<option value="product2">Product 2</option>
<option value="product3">Product 3</option>
</select>
<p>Quantity <input type="text" name="qty" size="3">
<input type="submit" name="sendme" value="Send Order">
</form>

So a simple form. When someone fills it out and hits the submit button the information in this form will be POSTed (sent) to the script in the form action. The receiving script will have access to the information in the $_POST [php.net] superglobal array [php.net].

First thing we will need to do is check our required fields (in this case all of them) to ensure that they have been filled out.

sendmyorder.php

we are going to initialize a variable to use for our errors. This means we are setting it to an empty string for later use. All variables are preceded by a $ and we try to give them names that have something to with what they will be used for

$errmsg = "";

we have 5 fields to check firstname, lastname, email, product and qty.

if [php.net] (!isset [php.net]($_POST['firstname']) empty [php.net]($_POST['firstname'])) $errmsg .= [php.net] "<p>Please enter your first name";

seems like a bunch of junk but it reads fairly well in english

if the variable $_POST['firstname'] is not set or it is empty then add the error message to our error message string

if the variable is set and has a value in it then the script continues. We would need one of these for each textbox we are checking. so we will need to add these 3

if (!isset($_POST['lastname']) empty($_POST['lastname'])) $errmsg .= "<p>Please enter your last name";
if (!isset($_POST['email']) empty($_POST['email'])) $errmsg .= "<p>Please enter your email address";
if (!isset($_POST['qty']) empty($_POST['qty'])) $errmsg .= "<p>Please enter the qantity you wish to order";

as you can see, the only thing that changes is the name of the variable which has to be the same as the name from the actual form. We also, obviously, change what the error message says to reflect the bad field.

now we have to check our drop down. We set up a default value in the form ( <option value="none"> ) so we need to test for that value, if it isn't there then they changed the selection.

if ($_POST['product'] == [php.net] "none") $errmsg .= "<p>Please select the product you wish to order";

that says
if the value of the variable $_POST['product'] is equal to "none" then add the error message to our error message string

so we now have checked all of our variables and

at this point, if there was a problem with any of the input, we will have an error message in $errmsg so we need to test it and end processing if there is anything in the variable.

if ($errmsg!= "") {
echo [php.net] $errmsg;
echo "<a href=\"javascript:history.back();\">Please go back and fill out the missing fields</a>";
exit [php.net];
}

that will show the error message on the screen and stop this script from doing anything else since we can't continue with out all of the required information.

so far we have this, we'll add a line at the end that will show a success message for now if there are no error messages.

<?php
$errmsg = "";
if (!isset($_POST['firstname']) empty($_POST['firstname'])) $errmsg .= "<p>Please enter your first name";
if (!isset($_POST['lastname']) empty($_POST['lastname'])) $errmsg .= "<p>Please enter your last name";
if (!isset($_POST['email']) empty($_POST['email'])) $errmsg .= "<p>Please enter your email address";
if (!isset($_POST['qty']) empty($_POST['qty'])) $errmsg .= "<p>Please enter the qantity you wish to order";
if ($_POST['product'] == "none") $errmsg .= "<p>Please select the product you wish to order";
if ($errmsg!= "") {
echo $errmsg;
echo "<a href=\"javascript:history.back();\">Please go back and fill out the missing fields</a>";
exit;
} else [php.net] {
echo "<p>success: all fields were filled out";
}
?>

perfect, now we need to email the results if all fields pass. We will be using the mail [php.net] function for this. There are some settings that effect mail, they are referenced on this page PHP Mail Functions [php.net]

we will be calling the mail function like so
bool mail ( string to, string subject, string message , string headers)

we just need to construct those variables before we can pass them into the function. We need the address to send email to, the subject line for the email, the email headers and I will add a cc line to go to you.

email note: always keep in mind that email is a serious pain. I use the headers that I do because over the last few years they have been the most reliable for me. Everyone has there own set of headers that they use but these are the ones that work most often for me.

we put the email address entered from the form into the $to variable
$to = $_POST['email'];

we then create a subject
$subject = "Order from www.example.com";

then we build our headers
$headers .= "From: MySite Orders <orders@example.com>\r\n";
$headers .= "Cc: orders@example.com\r\n";
$headers .= "Return-Path: orders@example.com\r\n";
$headers .= "Reply-To: orders@example.com\r\n";

now these are stripped headers from what I use and I put \r\n but I normally only use \n, again depends on too many things to squabble about. You may have problems getting the Return-Path to show properly but that could be a configuration thing as well. there are just too many things that effect email to explain them all.

we now need to make our message
$message = "Your order was successfully sent to www.example.com\n\n";
$message .= "First Name: " . $_POST['firstname'] . "\n";
$message .= "Last Name: " . $_POST['lastname'] . "\n";
$message .= "Email: " . $_POST['email'] . "\n";
$message .= "Product Requested: " . $_POST['product'] . "\n";
$message .= "Quantity: " . $_POST['qty'] . "\n\n";
$message .= "Thank you for using www.example.com\n\n";

we then pass that information to the mail function and see if it succeeded.

if (mail($to,$subject,$message,$headers)) {
echo "<p>thank you for your order!";
} else {
echo "<p>email could not be sent";
}

that pretty much sums up the basics I think. You can do tons more in the error checking like checking the email address has the proper format, checking to make sure qty is a number, checking field lengths etc.

that leaves us with this as the final code for sendmyorder.php

<?php 
$errmsg = "";
if (!isset($_POST['firstname']) empty($_POST['firstname'])) $errmsg .= "<p>Please enter your first name";
if (!isset($_POST['lastname']) empty($_POST['lastname'])) $errmsg .= "<p>Please enter your last name";
if (!isset($_POST['email']) empty($_POST['email'])) $errmsg .= "<p>Please enter your email address";
if (!isset($_POST['qty']) empty($_POST['qty'])) $errmsg .= "<p>Please enter the qantity you wish to order";
if ($_POST['product'] == "none") $errmsg .= "<p>Please select the product you wish to order";
if ($errmsg == "") {
echo $errmsg;
echo "<a href=\"javascript:history.back();\">Please go back and fill out the missing fields</a>";
exit;
} else {
$to = $_POST['email'];
$subject = "Order from www.example.com";
$headers .= "From: MySite Orders <orders@example.com>\r\n";
$headers .= "Cc: orders@example.com\r\n";
$headers .= "Return-Path: orders@example.com\r\n";
$headers .= "Reply-To: orders@example.com\r\n";
$message = "Your order was successfully sent to www.example.com\n\n";
$message .= "First Name: " . $_POST['firstname'] . "\n";
$message .= "Last Name: " . $_POST['lastname'] . "\n";
$message .= "Email: " . $_POST['email'] . "\n";
$message .= "Product Requested: " . $_POST['product'] . "\n";
$message .= "Quantity: " . $_POST['qty'] . "\n\n";
$message .= "Thank you for using www.example.com\n\n";
if (mail($to,$subject,$message,$headers)) {
echo "<p>thank you for your order!";
} else {
echo "<p>email could not be sent";
}
}
?>

no problem, now you have somewhere to start ;)

Always remember that WebmasterWorld breaks pipe characters replace all with a real pipe char

[edited by: jatar_k at 6:26 am (utc) on Feb. 9, 2005]

eggy ricardo




msg:1296414
 8:37 pm on Oct 28, 2004 (gmt 0)

Excellent post jatar_k! You really did cover the basics well there - hopefully this will suffice the needs of smukcreative.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved