PHP: Simple Contact Form with Archive Text

You have hosting and you want to make simple web for fast loading instead of full graphic. And web without contact form is like food without salt :D. And here I tried to combine between simple contact form with archive text.

Why should create archive text, it’s already sent email?

Well… some people lazy to open again in email client moreover you have millions email in your inbox.

Use database instead text archive!

Well… if you want to be simple, this is fit for you. When you think the text size is big then you can rename it and then the code will create new output again. If you well know in web and coding then it’s not quite fit, because you will use newsletter service instead, right? 😀

Well… it’s no harm for me to learn 😀

Code

HTML Form

Copy the HTML code below and save as contact.html

<html>
<body>

<p>Required fields are <b>bold</b></p>

<form action="contact.php" method="post">
<p><b>Your Name:</b> <input type="text" name="yourname" /><br />
<b>Subject:</b> <input type="text" name="subject" /><br />
<b>E-mail:</b> <input type="text" name="email" /><br />
Website: <input type="text" name="website"></p>

<p>Do you like this website?
<input type="radio" name="likeit" value="Yes" checked="checked" /> Yes
<input type="radio" name="likeit" value="No" /> No
<input type="radio" name="likeit" value="Not sure" /> Not sure</p>

<p>How did you find us?
<select name="how">
<option value=""> -- Please select -- </option>
<option>Google</option>
<option>Yahoo</option>
<option>Link from a website</option>
<option>Word of mouth</option>
<option>Other</option>
</select>

<p><b>Your comments:</b><br />
<textarea name="comments" rows="10" cols="40"></textarea></p>

<p><input type="submit" value="Send it!"></p>

<p> </p>
<p>Powered by <a href="http://myphpform.com">PHP form</a></p>

</form>

</body>
</html>

 Thank you Page

We could include the response in the PHP script, but keeping it in an outside file makes the script itself less complicated and the response page easier to edit and customize.

Copy the HTML code below and save as thanks.html

<html>
<body>

<p><b>Your message was sent</b></p>

<p>Your message was successfully sent!
Thank you for contacting us, we will reply
to your inquiry as soon as possible!</p>

</body>
</html>

PHP Form Code

<?php
/* Set e-mail recipient */
$myemail  = "your@email.com";

###################### Set up the following variables ######################
#                                                                          #
$filename = "output.txt"; #CHMOD to 666
#                                                                          #
##################### No need to edit below this line ######################

## set time up ##

$date = date ("l, F jS, Y");
$time = date ("h:i A"); 

/* Check all form inputs using check_input function */
$yourname = check_input($_POST['yourname'], "Enter your name");
$subject  = check_input($_POST['subject'], "Write a subject");
$email    = check_input($_POST['email']);
$website  = check_input($_POST['website']);
$likeit   = check_input($_POST['likeit']);
$how_find = check_input($_POST['how']);
$comments = check_input($_POST['comments'], "Write your comments");

/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
    show_error("E-mail address not valid");
}

/* If URL is not valid set $website to empty */
if (!preg_match("/^(https?:\/\/+[\w\-]+\.[\w\-]+)/i", $website))
{
    $website = '';
}

## mail message ##

$msg = "Below is the result of your feedback form.  It was submitted  on $date at $time.\n\n";
    
foreach ($_POST as $key => $value)
{
    $msg .= ucfirst ($key) ." : ". $value . "\n";
}

$msg .= "-----------\n\n";

$fp = fopen ($filename, "a"); # w = write to the file only, create file if it does not exist, discard existing contents
if ($fp) {
    fwrite ($fp, $msg);
    fclose ($fp);
}


/* Let's prepare the message for the e-mail */
$message = "Hello!

Your contact form has been submitted by:

Name: $yourname
E-mail: $email
URL: $website

Like the website? $likeit
How did he/she find it? $how_find

Comments:
$comments

End of message
";

/* Send the message using mail() function */
mail($myemail, $subject, $message);

/* Redirect visitor to the thank you page */
header('Location: thanks.html');
exit();

/* Functions we used */
function check_input($data, $problem='')
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    if ($problem && strlen($data) == 0)
    {
        show_error($problem);
    }
    return $data;
}

function show_error($myError)
{
?>
    <html>
    <body>

    <b>Please correct the following error:</b><br />
    <?php echo $myError; ?>

    </body>
    </html>
<?php
exit();
}
?>

 

Result of output.txt

Below is the result of your feedback form.  It was submitted  on Tuesday, November 4th, 2014 at 10:31 PM.

Yourname : Darto KLoning
Subject : Warning: Unknown:Unable to allocate memory for pol
Email : darto@gmail.com
Website : https://www.kloningspoon.com
Likeit : Yes
How : Word of mouth
Comments : yeah...
-----------

Below is the result of your feedback form.  It was submitted  on Tuesday, November 4th, 2014 at 10:33 PM.

Yourname : DUDUNG
Subject : DADA
Email : darto@kloning.com
Website : del3art.com
Likeit : No
How : Yahoo
Comments : yes indeed
-----------

source: myphpform & webdeveloper

Artikel terkait:   WordPress: Menambahkan Shortcode di dalam Widget

Isi Komentar

Alamat email Anda tidak akan dipublikasikan. Ruas yang wajib ditandai *

five + 5 =

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top