PHP Build a Basic PHP Website (2018) Adding a Basic Form Utilizing Object Properties and Methods

Brian Patterson
Brian Patterson
19,588 Points

Not sure why the suggestion page is not showing

Not sure why the suggest page is not showing. Also, does this work on a local server?

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = trim(filter_input(INPUT_POST,"name",FILTER_SANITIZE_STRING));
    $email = trim(filter_input(INPUT_POST,"email",FILTER_SANITIZE_EMAIL));
    $details = trim(filter_input(INPUT_POST,"details",FILTER_SANITIZE_SPECIAL_CHARS));

    if ($name == "" || $email == "" || $details == "") {
        echo "Please fill in the required fields: Name, Email and Details";
        exit;
    }
    if ($_POST["address"] != "") {
        echo "Bad form input";
        exit;
    }


    $email_body = "";
    $email_body .= "Name " . $name . "\n";
    $email_body .= "Email " . $email . "\n";
    $email_body .= "Details " . $details . "\n";

    $mail->setFrom($email, $name);
    $mail->addAddress('brian@localhost', 'Brian');     // Add a recipient
     // Optional name
    $mail->isHTML(false);                                  // Set email format to HTML

    $mail->Subject = 'Personal Media' . $name;
    $mail->Body    = $email_body;

    if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
    exit;
    }
    //To Do: Send Email
    header("location:suggest.php?status=thanks");
}
require("inc/phpmailer/class.phpmailer.php");

$mail = new PHPMailer;

if (!$mail->ValidateAddress($email)) {
  echo "Invalid Email Address"
}

$pageTitle = "Suggest a Media Item";
$section = "suggest";

include("inc/header.php"); ?>

<div class="section page">
    <div class="wrapper">
        <h1>Suggest a Media Item</h1>
        <?php if (isset($_GET["status"]) && $_GET['status'] == "thanks") {
            echo "<p>Thanks for the email! I&rsquo;ll check out your suggestion shortly!</p>";
        } else {
        ?>
        <p>If you think there is something I&rsquo;m missing, let me know! Complete the form to send me an email.</p>
        <form method="post" action="process.php">
            <table>
            <tr>
                <th><label for="name">Name</label></th>
                <td><input type="text" id="name" name="name" /></td>
            </tr>
            <tr>
                <th><label for="email">Email</label></th>
                <td><input type="text" id="email" name="email" /></td>
            </tr>
            <tr>
                <th><label for="name">Suggest Item Details</label></th>
                <td><textarea name="details" id="details"></textarea></td>
            </tr>
            <tr style="display:none">
                <th><label for="address">Address</label></th>
                <td><input type="text" id="address" name="address" />
                <p>Please leave this field blank</p></td>
            </tr>
            </table>
            <input type="submit" value="Send" />
        </form>
        <?php } ?>
    </div>
</div>

<?php include("inc/footer.php"); ?>

2 Answers

you need to change form action="process.php" to suggest.php because we don't have that page anymore and instead we have our PHP process code inside suggest.php;

Brian Patterson
Brian Patterson
19,588 Points

Thanks Hosien. Will this work on a local server as I have it running with MAMP?

Hi Brian, you are missing a semi-column (;) after your statement here : if (!$mail->ValidateAddress($email)) { echo "Invalid Email Address" <- }

also, you are validating $mail object outside of your first if statement where you have defined $email, it makes an undefined variable notice because you haven't defined it yet (you have to move it inside that if statement after that address if statement)

Brian Patterson
Brian Patterson
19,588 Points

Although when I input the details into the suggestion page and press submit I get the following error.

Not Found

The requested URL /process.php was not found on this server.