Having trouble with you checkboxes and radio buttons. Checkboxes are mainly use when presenting the user with a number of options, of which, they can select more than one option. For example, a form asking a user what topics they are interested in, obviously the user may be interested in more than one topic. When the user fill the form, the form will be automatically send though to my email but not user's email.
Okay, lets construct a simple form asking the user what topics they like. There will be multiple options of which the user can select none, some or all the options.
Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>Contact Test Form</title>
</head>
<body>
Required fields are marked with an asterisk ( * ).
<form action="mail.php" method="post">
<table width="275" border="0" cellspacing="4" cellpadding="0">
<tr>
<td width="75">Name *</td>
<td width="210"><input name="name" type="text" class="textfield"></td>
</tr>
<tr>
<td valign="top" width="75">Address *<img src="../images/spacer.gif" width="65" height="1"></td>
<td width="210"><textarea name="address" cols="25" rows="3" class="textfield"></textarea></td>
</tr>
<tr>
<td width="40">City *</td>
<td width="210"><input name="city" type="text" class="textfield"></td>
<td width="40">State</td>
<td width="10"><input name="state" type="text" class="textfield" size="4" maxlength="4"></td>
<td width="5"></td>
<td width="65">Postcode</td>
<td width="10"><input name="postcode" type="text" class="textfield" size="4" maxlength="4"></td>
</tr>
<tr>
<td valign="top">Telephone *</td>
<td ><input name="phone" type="text" class="textfield" size="14" maxlength="12"></td>
</tr>
<tr>
<td valign="top" width="75" >Email *</td>
<td width="210"><input name="email" type="text" class="textfield"></td>
</tr>
<tr>
<td valign="top" width="65" >Adviser Group</td>
<td width="210"><input name="advisergroup" type="text" class="textfield"></td>
</tr>
<tr>
<td width="150" valign="top" >Preferred method of contact:</td>
<td valign="top" >
<table border="0" cellpadding="0" cellspacing="0" width="155">
<tbody>
<tr>
<td width="40"><input name="contact" value="Post" type="radio"></td>
<td valign="bottom" width="35">Post </td>
<td width="40"><input name="contact" value="Telephone" type="radio"></td>
<td valign="bottom" width="35">Telephone </td>
<td width="40"><input name="contact" value="Email" type="radio"></td>
<td valign="bottom" width="40">Email</td>
</tr>
</tbody>
</table>
</td>
</tr>
<td width="150" valign="top" >Are you a:</td>
<td valign="top">
<table border="0" cellpadding="0" cellspacing="0" width="155">
<tbody>
<tr>
<td width="40"><input name="are" value="Adviser" type="radio"></td>
<td valign="bottom" width="35">Adviser</td>
<td width="40"><input name="are" value="Account" type="radio"></td>
<td valign="bottom" width="35">Accountant </td>
<td width="40"><input name="are" value="New Investor" type="radio"></td>
<td valign="bottom" width="60">New Investor</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td colspan="2" valign="top" >Please RUSH me Product Disclosure Statement for:</td>
</tr>
<tr>
<td valign="top" > </td>
<td valign="top" ><table border="0" cellpadding="0" cellspacing="0" width="200">
<tbody>
<tr>
<td width="22"><input name="pds[]" type="checkbox" value="AREIT Augusta Development Fund"></td>
<td valign="bottom" width="171">AREIT Augusta Development Fund</td>
</tr>
<tr>
<td width="22"><input name="pds[]" type="checkbox" value="AREIT Diversified Fund"></td>
<td valign="bottom" width="171">AREIT Diversified Fund</td>
</tr>
<tr>
<td width="22"><input name="pds[]" type="checkbox" value="Agricultural Mortgage Trust"></td>
<td valign="bottom" width="171">Agricultural Mortgage Trust</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td valign="top" >Comments *</td>
<td valign="top" ><textarea name="comment" cols="25" rows="10" class="textfield"></textarea></td>
</tr>
<tr>
<td valign="top"> </td>
<td align="right" ><input type="submit" name="submit" value="Submit" /></td>
</tr>
</table>
</form>
</body>
</html>
Code: Select all
<?php
//Headers
$emailFromName = $_POST['name'];
$emailAddress = $_POST['address'];
$emailCity = $_POST['city'];
$emailState = $_POST['state'];
$emailPostcode = $_POST['postcode'];
$emailTelephone = $_POST['phone'];
$emailFrom = $_POST['email'];
$emailAdviser = $_POST['advisergroup'];
$emailContact = $_POST['contact'];
$emailAre = $_POST['are'];
$emailPds = $_POST['pds'];
$emailComment = $_POST['comment'];
$action = (isset($_POST['submit']) ? "submit" : "entry");
//Checkbox
foreach ($emailPds as $val) {
echo "$val\n";
}
//Errors
$error_lines = array();
if ($action == "submit")
{
if (empty($emailFromName)) {
$error_lines['emailFromName'] = 'You forgot to enter your name';
}
if (empty($emailAddress)) {
$error_lines['emailAddress'] = 'You forgot to enter your address';
}
if (empty($emailCity)) {
$error_lines['emailCity'] = 'You forgot to enter your city';
}
if (empty($emailState)) {
$error_lines['emailState'] = 'You forgot to enter your city';
}
if (empty($emailPostcode)) {
$error_lines['emailPostcode'] = 'You forgot to enter your city';
}
if (empty($emailTelephone)) {
$error_lines['emailTelephone'] = 'You forgot to enter your telephone';
}
if (!preg_match('/^([A-Z0-9\.\-_]+)@([A-Z0-9\.\-_]+)?([\.]{1})([A-Z]{2,6})$/i', $emailFrom) || empty($emailFrom)) {
$error_lines['emailFrom'] = 'You forgot to enter a valid email address';
}
if (empty($emailComment)) {
$error_lines['emailComment'] = 'You must enter a message for me';
}
}
if($action == "submit" && count($error_lines) == 0) {
//Send an email
$to = "paul.crawford@austasiagroup.com";
$Subject = "AREIT Form Feedback";
$form_results = "The following enquiry was submitted via the AREIL website on ".date('l, d M Y H:i:s') . "\n\nName: ". $emailFromName ."\n\nAddress: ". $emailAddress ."\nCity: ". $emailCity ."\nState: ". $emailState ."\nPostcode: ". $emailPostcode ."\n\nTelephone: ". $emailTelephone ."\n\nEmail: ". $emailFrom ."\n\nAdviser Group: ". $emailAdviser ."\n\nPreferred method of contact: ". $emailContact ."\n\nAre you a: ". $emailAre ."\n\nPlease rush me Product Disclosure Statement for: ". $val ."\n\nComments: ". $emailComment;
mail($to, "$Subject" ,"$form_results", "From: $emailFrom");
echo "
Thank you for contacting me, ". $emailFromName .". Your mail was sent successfully.
Your Message
<div class=\"quote\"><h1>Name</h1>
$emailFromName
<h1>Address</h1>
$emailAddress
<h1>City</h1>
$emailCity
<h1>State</h1>
$emailState
<h1>Postcode</h1>
$emailPostcode
<h1>Telephone</h1>
$emailTelephone
<h1>Email</h1>
$emailFrom
<h1>Adviser Group</h1>
$emailAdviser
<h1>Preferred method of contact</h1>
$emailContact
<h1>Are you a</h1>
$emailAre
<h1>Please rush me Product Disclosure Statement for</h1>
$val
<h1>Comments</h1>
$emailComment</div>";
} else {
if (count($error_lines) > 0) {
echo "<div class=\"error\"><h1>Error!</h1>You have the following errors:\n\n";
foreach($error_lines as $line) {
echo $line;
}
echo "
Please complete the form and re-send it.</div>\n\n";
}
echo "<form method=\"post\" action=\"mail.php\">
If you would like to contact me regarding work or anything that you have seen on this website please use the form below and I will get back to you as soon as possible.
</p>
Name" . (isset($error_lines["emailFromName"]) ? "*" : "") . "
<INPUT TYPE=\"TEXT\" NAME=\"name\" size=\"40\" value=\"$emailFromName\">
Address " . (isset($error_lines["emailAddress"]) ? "*" : "") . "
<INPUT TYPE=\"TEXT\" NAME=\"name\" cols=\"25\" rows=\"3\" value=\"$emailAddress\">
City " . (isset($error_lines["emailCity"]) ? "*" : "") . "
<INPUT TYPE=\"TEXT\" NAME=\"name\" value=\"$emailCity\">
State " . (isset($error_lines["emailState"]) ? "*" : "") . "
<INPUT TYPE=\"TEXT\" NAME=\"name\" value=\"$emailState\">
Postcode " . (isset($error_lines["emailPostcode"]) ? "*" : "") . "
<INPUT TYPE=\"TEXT\" NAME=\"name\" value=\"$emailPostcode\">
Telephone " . (isset($error_lines["emailTelephone"]) ? "*" : "") . "
<INPUT TYPE=\"TEXT\" NAME=\"name\" value=\"$emailTelephone\">
Email Address" . (isset($error_lines["emailFrom"]) ? "*" : "") . "
<input type=\"text\" name=\"email\" size=\"40\" value=\"$emailFrom\">
Comments " . (isset($error_lines["emailComment"]) ? "*" : "") . "
<textarea rows=\"10\" cols=\"40\" name=\"comment\" value=\"$emailComment\"></textarea>
<input type=\"submit\" name=\"submit\" value=\"Submit\">
</p>
</form>";
}
?>Thanks alot.