skip to Main Content

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


  1. 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.

    <?php
    if( $_POST ){
       $webmaster_email = "[email protected]";
       $thankyou_page = "thanks.html";
       $error_page = "errors.html";
       $firstname = $_POST['firstname'] ;
       $lastname = $_POST['lastname'] ;
       $email = $_POST['email'] ;
       $message = $_POST['message'] ;
    
       $msg = "rnFirst Name: " . $firstname . "rnrnLast Name: " . $lastname . "rnrnE-mail address: " . $email . "rnrnMessage: " . $message . "rn";
    
       mail( "$webmaster_email", "Message on Contact Form", $msg );
       header( "Location: $thankyou_page" );
    }?>
    
    <form method="post" action="">
       ......
    <button type="submit" tabindex="5">Submit Contact Form</button>
    </form>
    
    Login or Signup to reply.
  2. The entire content in a single file as contact.php. code as follows…

    <form method="post"><!--remove action as it comes back here(this page) only-->
    ......
    <button type="submit" tabindex="5">Submit Contact Form</button>
    </form>
    ......
    
    <!--php part....-->
    
    <?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" );
    }
    ?>
    
    Login or Signup to reply.
  3. 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:

         <html>
    
         <head>
            <title>Contact Form</title>
         </head>
    
         <body>
            <?php
                $webmaster_email = "[email protected]";
                $thankyou_page = "thanks.html";
                $error_page = "errors.html";
                if ($_SERVER['REQUEST_METHOD'] === 'POST') {
                    $firstname = $_POST['firstname'];
                    $lastname = $_POST['lastname'];
                    $email = $_POST['email'];
                    $message = $_POST['message'];
                    $msg = "rnFirst Name: " . $firstname . "rnrnLast Name: " . $lastname . "rnrnE-mail address: " . $email . "rnrnMessage: " . $message . "rn";
                    if (mail($webmaster_email, "Message on Contact Form", $msg)) {
                        header("Location: $thankyou_page");
                        exit();
                    } else {
                        header("Location: $error_page");
                        exit();
                    }
                }
                ?>
            <form method="post" action="">
                <label for="firstname">First Name:</label>
                <input type="text" name="firstname" id="firstname" required>
                .....
                <button type="submit" tabindex="5">Submit Contact Form</button>
            </form>
         </body>
    
         </html>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search