Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,148,906 members, 7,802,941 topics. Date: Saturday, 20 April 2024 at 04:37 AM

How To Create Form To Email? - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / How To Create Form To Email? (1001 Views)

Form Submitting To Email (2) (3) (4)

(1) (Reply)

How To Create Form To Email? by vICADO97(m): 6:31pm On Sep 26, 2017
Tiptoeing outside of the boundaries of client-side coding, this page tackles a commonly requested server-side technique. It outlines a very basic method for taking the contents of a submitted form and sending it to any given email address. Y’know, like in “Contact us” forms.

There are numerous ways of achieving this, with numerous wild and wonderful programming languages, but we’re going to use a scripting language called PHP.

PHP stands for PHP: Hypertext Preprocessor (yeah — it’s recursive — bonkers) and it works hand-in-hand with HMTL. It’s easy and it’s prolific. In all likelihood the web hosting that is home to your HTML pages on the web will be PHP-enabled.

Step 1: Create a PHP page

If you use the extension “.php” instead of “.html” for your web page, the server hosting the page will know to execute any PHP sitting within it. So, to start, simply save an empty HTML page as “contact.php”.

Step 2: Create a form

You can read about the ins and outs of forms in the HTML Beginner Tutorial by sending me an email at [url]vicado91@gmail.com[/url] To jump straight in, let’s use the following super-stripped-down form HTML:


<form method="post" action="contact.php">
<textarea name="message"></textarea>
<input type="submit">
</form>


All quite straightforward, right? The action="contact.php" bit tells the page to send the form’s content to itself when the form is submitted. Crazy, huh? We’re doing everything here with the same page…

Step 3: Send the form’s data in an email

At the very top of the page, even before the Doctype, we are going to enter a smattering of PHP to handle the form data:
<?php
if($_POST["message"]) {
mail("your@email.address", "Form to email message", $_POST["message"], "From: an@email.address"wink;
}
?>


“<?php” marks the start of the PHP and “?>” marks the end. The server will attempt to execute everything in between as PHP.

This code checks if form data has been sent and, if it has, it uses the mail function to send the data as an email to “your@email.address” with the subject “Form to email message” and the message body that is the same as the form field with the name “message”. The email will appear to be from “an@email.address”.

Recap

You can try this out once you upload it (it requires the server to run — you won’t be able to run it locally).

Here’s what’s happening:

1.Once the form has been submitted, the page will send the data to itself.
2.The page will check if data has been sent and, if it has, it will send it on as an email.
3.The browser will load the page’s subsequent HTML, including your form.


Further steps: A basic “Contact us” page email me on vicado91@gmail.com or follow me up on www.talkval.ga and my new blog www.coolforum.tk

1 Like 1 Share

Re: How To Create Form To Email? by Nobody: 8:20pm On Sep 26, 2017
I do not know anything about php but I'm saying this just in case I need it.
Re: How To Create Form To Email? by Nobody: 9:46pm On Oct 06, 2017
It didn't work bro

(1) (Reply)

Option Trading Script With Unique Features Direct To Your Dropbox In A Seconds / Benefits Of PYTHON Programming As Career For Everyone Interested In IT Success / Png, Psd To Css Html Developer

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 11
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.