I am making a simple html form, using the method below to post.
I want all the php in the htm file. (i know you can just rename contact.htm to contact.php but that does not tell me how to change it from external file to internal code and where to put the code)
Any suggestions?
Part of the .html form:
<form method="post" action="forms/contact.php">
......
<button type="submit" tabindex="5">Submit Contact Form</button>
</form>
......
.php file:
<?php
$webmaster_email = "[email protected]";
$thankyou_page = "thanks.html";
$error_page = "errors.html";
$firstname = $_REQUEST['firstname'] ;
$lastname = $_REQUEST['lastname'] ;
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;
$msg = "rnFirst Name: " . $firstname . "rnrnLast Name: " . $lastname . "rnrnE-mail address: " . $email . "rnrnMessage: " . $message . "rn";
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
mail( "$webmaster_email", "Message on Contact Form", $msg );
header( "Location: $thankyou_page" );
}
else {
header( "Location: $error_page" );
}
?>
3
Answers
If I understand correctly, you want to have everything in one file.
You would first need to rename your file to a .php file. Then, if the
action
attribute is empty on the form element the form will submit to the same page (eg: reloads the page with the form data).Then you can have all of the PHP code in the top and just have a check if there is POST data being submitted.
The entire content in a single file as contact.php. code as follows…
To include the PHP code directly within your HTML file, you need to save your HTML file with a .php extension and then embed the PHP code within the HTML using the tags. Here’s how you can do it:
Save your HTML file with a .php extension, for example, contact.php.
Modify your HTML form to include the PHP code within it: