<?php
//This is a data input form, but it also contains the php script to process the
//data. The first part activates if data is being posted. It adds the data to a
//MYSQL database and sends a confirming message to the user.
//Some of the more elaborate code is designed to improve its appearance in
//browsers like Mozilla and Firefox.
//
//Code Copyright 2005 by Computeledge, Inc.
//GPL License
//

if ( $_POST['company'] )
{
$company = $_POST['company'];
$contact = $_POST['contact'];
$address1 = $_POST['address1'];
$address2 = $_POST['address2'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$phone = $_POST['phone'];
$fax = $_POST['fax'];
$email = $_POST['email'];
//link to database
$mydbname = "
<Your Database>";
$link = mysql_connect ("localhost", "
<Your MySQL Internet User>", "<User's Password>");
mysql_select_db ($mydbname, $link)
or die ("Could not open database: ".mysql_error() );
//run a MYSQL INSERT query ( Table name -> sample )
mysql_query ("INSERT INTO sample
( company,
contact,
address1,
address2,
city,
state,
zip,
phone,
fax,
email )
VALUES
('$company',
'$contact',
'$address1',
'$address2',
'$city',
'$state',
'$zip',
'$phone',
'$fax',
'$email')")
or die ("Could not perform operation: ".mysql_error() );
// Close database
mysql_close ($link);
print '<?xml version="1.0" encoding="UTF-8" ?>';

?>
<!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; encoding=UTF-8"/>
<title>Data Received</title>
</head>
<body style="text-align:center">
<div style="margin-left:auto;margin-right:auto;width:500px;background:#f5f5ff;
height:100px;padding:15px;color:#000000;font-family:arial,helvetica,sans-serif;
font-size:16pt;font-weight:bold">Your information was written to our Database.<br />
Thank You!<br /><br /><a href="/scripts.html">EXIT</a>
</div>
</body>
</html>

<?php
//The form part below runs if nothing is being posted.
}else{
print '<?xml version="1.0" encoding="UTF-8" ?>';
?>

<!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; encoding=UTF-8"/>
<title>Input Form</title>
<style type="text/css">
div.title{
font-family:arial,helvetica,sans-serif;
font-size:16pt;
color:#ffffff;
background:#000074;
width:452px;
height:40px;
margin-left:auto;
margin-right:auto;
border:solid;
border-width:1px;
border-color:#000000;
}
div.main{
height:350px;
width:450px;
background:#f5f5ff;
margin-left:auto;
margin-right:auto;
border:solid;
border-width:1px;
}
.fieldname{
width:120px;
text-align:right;
padding-right:5px;
}
.field{
width:330px;
text-align:left;
}
.input-txt{
background:#fffff5;
font-family:courier;
font-size:10pt;
font-weight:normal;
height:20px;
padding-left:5px;
padding-top:2px;
border:1px solid #aaaacc;
}
.input-txt2{
background:#fffff5;
font-family:courier;
font-size:10pt;
font-weight:normal;
padding-left:5px;
border:2px inset #aaaacc;
}
</style>

<script type="text/javascript">

function mozClass()
{
document.getElementById('company').focus();
if( navigator.appName == "Netscape" ){
document.getElementById('company').className = "input-txt2";
document.getElementById('contact').className = "input-txt2";
document.getElementById('address1').className = "input-txt2";
document.getElementById('address2').className = "input-txt2";
document.getElementById('city').className = "input-txt2";
document.getElementById('zip').className = "input-txt2";
document.getElementById('phone').className = "input-txt2";
document.getElementById('fax').className = "input-txt2";
document.getElementById('email').className = "input-txt2";
}
return true;
}

function validate()
{
var company = document.getElementById('company').value;
var contact = document.getElementById('contact').value;
var address1 = document.getElementById('address1').value;
var city = document.getElementById('city').value;
var state = document.getElementById('state').value;
var zip = document.getElementById('zip').value;
var email = document.getElementById('email').value;
if ( company == "") {
alert("Please enter your Company Name.");
document.getElementById('company').focus();
return false;
}
if ( contact == "") {
alert("Please enter your Contact Name.");
document.getElementById('contact').focus();
return false;
}
if ( address1 == "") {
alert("Please enter your Address.");
document.getElementById('address1').focus();
return false;
}
if ( city == "") {
alert("Please enter your City.");
document.getElementById('city').focus();
return false;
}
if ( state == "XX") {
alert("Please select your State.");
document.getElementById('state').focus();
return false;
}
if ( zip == "") {
alert("Please enter your zipcode.");
document.getElementById('zip').focus();
return false;
}
if ( email == "") {
alert("Please enter your Email.");
document.getElementById('email').focus();
return false;
}

}

</script>
</head>
<body style="text-align:center;margin-top:20px;" onload="mozClass()">
<div class="title"><div style="padding-top:7px">Sample Data Entry Form</div></div>

<div class="main"><div style="padding-left:0px;margin-top:20px;">
<form method="post" action="inputform.php" onsubmit="return validate();">
<table style="width:450px;border:0px;font-family:arial,helvetica,sans-serif;font-size:10pt;">
<tr><td class="fieldname" ><span style="color:red"> *</span>Company Name</td>
<td class="field"><input type="text" id="company" name="company" size="35"
class="input-txt" /></td></tr>
<tr><td class="fieldname"><span style="color:red"> *</span>Contact Name</td>
<td class="field" width="400px"><input type="text" id="contact" name="contact" size="35"
class="input-txt" /></td></tr>
<tr><td class="fieldname"><span style="color:red"> *</span>Address1</td>
<td class="field"><input type="text" id="address1" name="address1" size="35"
class="input-txt" /></td></tr>
<tr><td class="fieldname">Address2</td>
<td class="field"><input type="text" id="address2" name="address2" size="35"
class="input-txt" /></td></tr>
<tr><td class="fieldname"><span style="color:red"> *</span>City</td>
<td class="field"><input type="text" id="city" name="city" size="35"
class="input-txt" /></td></tr>
<tr><td class="fieldname"><span style="color:red"> *</span>State</td>
<td class="field">
<select id="state" name="state" style="background:#fffff5;font-family:courier;
font-size:10pt;font-weight:normal;width:200px;border:1px solid #aaaacc">
<?php
// This section reads file states.txt and creates the Select/Option html data.
// The state name is viewed but only the abbreviation is saved.
print "<option value='XX'></option>";
$fp = fopen( "states.txt", "r" );
while ( !feof($fp) )
{
$line = fgets( $fp, 4096 );
$state_name = strtok( $line, ";" );
$state= strtok(";");
print "<option value='$state'>".$state_name."</option>";
}
fclose( $fp );
?>
</select></td></tr>
<tr><td class="fieldname"><span style="color:red"> *</span>Zip</td>
<td class="field"><input type="text" id="zip" name="zip" size="10"
class="input-txt" /></td></tr>
<tr><td class="fieldname">Phone</td>
<td class="field"><input type="text" id="phone" name="phone" size="14"
class="input-txt" /></td></tr>
<tr><td class="fieldname">Fax</td>
<td class="field"><input type="text" id="fax" name="fax" size="14"
class="input-txt" /></td></tr>
<tr><td class="fieldname"><span style="color:red"> *</span>E-mail</td>
<td class="field"><input type="text" id="email" name="email" size="35"
class="input-txt" /></td></tr>
<tr><td colspan="2" height="20px"><p> </p></td></tr>
</table>
<div style="background:#f5f5ff;"><span><input type="submit" id="bt1" value="Submit"
style="font-weight:bold;background:#fff0f0;width:100px;" /></span>
<span><input type="reset" id="bt2" value="Reset"
style="font-weight:bold;background:#fff0f0;width:100px;" /></span></div>
</form>
</div></div>
<div style="margin-left:auto;margin-right:auto;width:450px;height:75px;margin-top:20px;
border:1px solid #000000;background:#fff0f0;">
<div style="margin-top:15px;margin-left:20px;margin-right:20px;font-family:arial,helvetica,sans-serif;
text-align:left;font-size:10pt">This page is a php script that contains the
code that actually submits your data to a MYSQL database.&nbsp;&nbsp;
<span style="color:#ff0000;">* </span>
<span style="font-weight:bold">Represents required fields.</span></div></div>
</body>
</html>
<?php
}
?>