PHP Form in HTML Page

I have a web page that is written in HTML and uses CSS Style Sheats.  What i need todo is have the data that is entered into the form sent as an email.  The page is saved as a .html and not a .php will this effect me being able use php on the page.  If so will I have to re-write the page?  I can post the code if needed.

 

Thanks in advance!!!

Nope. You are allowed to have php inside of an .html page. Add this line to a .htaccess file in the root of the folder containing the .html page(s):

 

AddType application/x-httpd-php .html

 

You may also want to allow php in one specific .html page:

 

<Files yourpage.html>
AddType application/x-httpd-php .html
</Files>

Thank You so much.

No sweat

First off, unless you're hosting the html pages on a server running php, you won't be able to use php in your html files. You could download a WAMP/LAMP/MAMP stack (depending on which OS you run) and run a server on your local machine if you don't have a remote server to store your files on.

Also, changing the file extension from .html to .php won't change how the page loads in your browser. A properly configured php install should be able to interpret php whether it's an html or a php file.

Once you do have a server running php, your basic workflow would be something like the following.

  1. Construct you html form with all the required fields.
  2. The "action" attribute of your <form> tag will be a php file that lives on your server somewhere (you should also include the "method" attribute which should be GET or POST depending on your security needs - POST is your best bet if you're unsure)
  3. When the form is submitted, it will send all of the data in the form to the php script
  4. In your php script, you'll need to decide how you want to handle the data. If you specified POST as the method for your form, you can access the data through the $_POST global ($_GET would be the other option). So if you have a form field named first_name, you could get the data from that form field by using $_POST['first_name']
  5. You then want to construct a message for the email, inserting the data from the form fields as necessary.
  6. You can then use php's mail function to send the message.

A quick and dirty method would be to use a mailto: for the action attribute in your <form> tag, which ideally wouldn't require any php. A word of caution though, because it's not a feature that's supported by a lot of browsers.

http://www.w3schools.com/html/tryit.asp?filename=tryhtml_form_mail

http://stackoverflow.com/questions/5773174/html-button-to-send-email

Anything you need to know about php you can look up at http://php.net/manual/en/

w3schools should have plenty of information about forms and basic php scripts as well: http://www.w3schools.com/

EDIT: damn was i slow typer

 

Im asuming you already have a HTML <form> with the inputs in.

What you will need to do, is set the "action" of the form to something like "submit.php" and make sure that method is "post" (it can be GET as well, but you will be limited by URL char limit).
So the HTML for the form tag will look something like:

<form action="submit.php" method="post" ...other-attributes...>

You will also need to make sure you give your inputs a name, example:

<input type="text" name="email" />

 

Create a new file called submit.php. I will just post the php code which you can have anywhere within page. I would recomend at the top.

<?

//im declaring variables for easy use, you can use $_POST directly.

$email  = $_POST['email'];

$subject = "Email from website"; // you could also have a $_POST here

$message = $_POST['message'];

$sendTo = "[email protected]";

$headers = "From:" . $from; // this is the headers of the email, i never really researched how emails work, so i dont know exactly how it works, but its basically declaring who the email is from. And im sure you can CCs and other things as well if you want.

mail($sendTo,$subject,$message,$headers);

?>

 

I wrote this code from scratch and have not tested it, but hopefully it will give you an idea of how you can do it. It can also be done in one page (as opose to having the form page and the submit.php page), but this would be slightly easier.