PDA

View Full Version : Simple Form



iLogan
03-12-2010, 05:36 PM
Hey,

Real nooby question... but, I need to make a simple competition page, where a user can type their answer and email address and it gets stored into a database or emailed to me,

really don't want to use an online form builder, so something own server please

Thanks!

iLogan
04-12-2010, 10:32 PM
Any ideas?

Calvin
04-12-2010, 11:00 PM
Customize this to your needs, should be pretty easy. Thanks to Moh for releasing this.

<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Contact Form</title>

<style type="text/css" media="screen">
fieldset {
margin-bottom: 3px;
}

legend {
font-weight: bold;
}

.good, .bad {
padding: 5px;
margin-bottom: 5px;
border: 1px solid;
}

.good {
background-color: #d9ffcf;
border-color: #ade5a3;
color: #1b801b;
}

.bad {
background-color: #ffcfcf;
border-color: #e5a3a3;
color: #801b1b;
}
</style>
</head>

<body>

<h1>Contact Form</h1>

<?php

$to_email = "removed";
$subjects = array( "Question", "Complaint", "Spotted an Error", "Idea" );
$hide_form = false;

if( isset( $_POST['submit'] ) ) {

$name = stripslashes( $_POST['name'] );
$subject = stripslashes( $_POST['subject'] );
$email = stripslashes( $_POST['email'] );
$message = stripslashes( $_POST['message'] );
$ip = $_SERVER['REMOTE_ADDR'];
$error = 0;
$error_body = "";


if( !$name ) {
$error_body .= "<li>You did not enter your name.</il>\n";
$error++;
}
if( !in_array( $subject, $subjects ) ) {
$error_body .= "<li>Invalid Subject.</il>\n";
$error++;
}
if( !$email ) {
$error_body .= "<li>You did not enter an email address.</il>\n";
$error++;
}
if( !preg_match("^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$^", $email ) and $email ) {
$error_body .= "<li>You entered an invalid email address.</il>\n";
$error++;
}
if( !$message ) {
$error_body .= "<li>You did not enter a message.</il>\n";
$error++;
}

if( $error ) {

echo "<div class=\"bad\">\n";
echo "<strong>The following " . ( ( $error > 1 ) ? "errors" : "error" ) . " occurred</strong>:\n";
echo "<ul>\n";
echo $error_body;
echo "</ul>\n";
echo "</div>\n";

} else {

echo "<div class=\"good\">\n";
echo "Thank you {$name} for your message.\n";
echo "</div>\n";

$message = nl2br( $message );

$body .= "----------------------------------\n";
$body .= "<p>\n";
$body .= "<strong>Name</strong>: {$name}<br />\n";
$body .= "<strong>Subject</strong>: {$subject}<br />\n";
$body .= "<strong>Email Address</strong>: {$email}<br />\n";
$body .= "<strong>Message</strong>: {$message}<br />\n";
$body .= "<strong>IP Address</strong>: {$ip}<br />\n";
$body .= "</p>\n";
$body .= "----------------------------------\n";

$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
$headers .= "From: {$name} <{$email}>" . "\r\n";

mail( $to_email, "Contact Form: {$subject}", $body, $headers );

$hide_form = true;

}

}

?>

<?php
if( !$hide_form ) {
?>
<form method="post">

<fieldset>
<legend>Name</legend>
<input type="text" name="name" value="<?php echo $name; ?>" />
</fieldset>

<fieldset>
<legend>Email Address</legend>
<input type="text" name="email" value="<?php echo $email; ?>" />
</fieldset>

<fieldset>
<legend>Subject</legend>
<select name="subject">
<?php

foreach( $subjects as $i ) {
if( $i == $subject ) {
echo "<option selected=\"selected\">{$i}</option>\n";
} else {
echo "<option>{$i}</option>\n";
}
}

?>
</select>
</fieldset>

<fieldset>
<legend>Message</legend>
<textarea name="message" rows="10" cols="50"><?php echo $message; ?></textarea>
</fieldset>

<input type="submit" name="submit" value="Send" />

</form>
<?php
}
?>


</body>
</html>

iLogan
04-12-2010, 11:24 PM
tysm!!! xox

Want to hide these adverts? Register an account for free!