Form To DB
From KOP KB
THE DB
in php myadmin or another method of submitting an sql query you would want to use:
THE FORM
<!DOCTYPE html>
<html>
<head>
<script>
function validateForm() {
//make sure the form has the name of myForm
var w = document.forms["myForm"]["cNums"].value;
var x = document.forms["myForm"]["fName"].value;
var y = document.forms["myForm"]["lName"].value;
//check case sensitive email in the name
var z = document.forms["myForm"]["email"].value;
var atpos = z.indexOf("@");
var dotpos = z.lastIndexOf(".");
if (x == null || x == "") {
alert("First name must be filled out");
return false;
}
else if (y==null || y=="") {
alert("Last name must be filled out");
return false;
}
else if (atpos< 1 || dotpos<atpos+2 || dotpos+2>=z.length) {
alert("Not a valid e-mail address");
return false;
}
else if (w==null || w=="") {
alert("Need to put in at least one Number");
return false;
}
}
</script>
</head>
<body>
<?php
// display form if user has not clicked submit
if (!isset($_POST["submit"]))
{
?>
<form name="myForm" method="post" onsubmit="return validateForm()" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<table>
<tr>
<td>First Name:</td><td> <input type="text" name="fName"></td>
</tr><tr>
<td>Last Name: </td><td> <input type="text" name="lName"></td>
</tr><tr>
<td>Billing Address:</td><td> <input type="text" name="bAddress"></td>
</tr><tr>
<td> Street Address:</td><td> <input type="text" name="sAddress"></td>
</tr><tr>
<td>City:</td><td> <input type="text" name="cCity"></td>
</tr><tr>
<td>State:</td><td> <input type="text" name="cState"></td>
</tr><tr>
<td>Zip Code:</td><td> <input type="text" name="zCode"></td>
</tr><tr>
<td>Email Address:</td><td> <input type="text" name="email"></td>
</tr><tr>
<td>Authorization:</td><td> <input type="checkbox" name="cAuthCheck" value="true" checked></td>
</tr><tr>
<td>Five Digit Key:</td><td> <input type="text" name="cFDK"></td>
</tr><tr>
<td>Verizon Wireles Number(s):<br> separate with a comma and no spaces or brackets</td><td> <input type="text" name="cNums"></td>
</tr><tr>
<td></td><td> <input type="submit" name="submit" value="Submit Feedback"></td>
</tr>
</form>
<?php
}
else
// the user has submitted the form
{
// Check if the "from" input field is filled out
if (isset($_POST["email"]))
{
// this is mostly for windows you can comment this line out or remove it completely
$servername = "hostname";
$username = "user";
$password = "pass";
$dbname = "dbname";
$fname = $_POST["fName"];
$lname = $_POST["lName"];
$baddress = $_POST["bAddress"];
$saddress = $_POST["sAddress"];
$ccity = $_POST["cCity"];
$cstate = $_POST["cState"];
$zcode = $_POST["zCode"];
$email = $_POST["email"];
$cauthcheck = $_POST["cAuthCheck"];
$fdk = $_POST["cFDK"];
$cnums = $_POST["cNums"];
if ($cauthcheck != "true"){
$cauthcheck = "false";
}
// The set of if statements below make sure whatever was entered can actually be entered into the database.
// Its not checking if the data is valid but making sure it can be stored into the database without issue.
if(strlen($fname)<31 && strlen($fname)>0){
if(strlen($lname)<31 && strlen($lname)>0){
if(strlen($baddress)<101 && strlen($baddress)>0){
if(strlen($saddress)<51 && strlen($saddress)>0){
if(strlen($ccity)<51 && strlen($ccity)>0){
if(strlen($cstate)<3 && strlen($cstate)>0){
if(strlen($zcode)<6 && strlen($zcode)>0){
if(strlen($email)<101 && strlen($email)>0){
if(strlen($fdk)<6 && strlen($fdk)>0){
if(strlen($cnums)<256 && strlen($cnums)>0){
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO customers (firstname, lastname, bAddress, sAddress, cCity, cState, zCode, email, cAuthCheck, cFDK, cNums)
VALUES ('$fname', '$lname', '$baddress', '$saddress', '$ccity', '$cstate', '$zcode', '$email', '$cauthcheck', '$fdk', '$cnums')";
$conn->query($sql);
$conn->close();
echo "Thanks you can return to the <a href='http://domain.com/pagename.php'>front page<a>";
}else{
echo "To many characters for the Numbers please try again. <a href='http://domain.com/pagename.php'>Return</a>";
}
}else{
echo "To many characters for the Five Digit Key please try again. <a href='http://domain.com/pagename.php'>Return</a>";
}
}else{
echo "To many characters for the email address please try again. <a href='http://domain.com/pagename.php'>Return</a>";
}
}else{
echo "To many characters for the Zip Code please try again. <a href='http://domain.com/pagename.php'>Return</a>";
}
}else{
echo "To many characters for the State name please try again. Only 2 Characters Required <a href='http://domain.com/pagename.php'>Return</a>";
}
}else{
echo "To many characters for the City please try again. <a href='http://domain.com/pagename.php'>Return</a>";
}
}else{
echo "To many characters for the Street Address please try again. <a href='http://domain.com/pagename.php'>Return</a>";
}
}else{
echo "To many characters for the Business Address please try again. <a href='http://domain.com/pagename.php'>Return</a>";
}
}else{
echo "To many characters for the Last name please try again. <a href='http://domain.com/pagename.php'>Return</a>";
}
}else{
echo "To many characters for the first name please try again. <a href='http://domain.com/pagename.php'>Return</a>";
}
echo "<br>";
echo strlen($fname);
}
}
?>
</body>
</html>