Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Build a Basic PHP Website (2018) Adding a Basic Form Checking the Request Method

SOOJIN HA
SOOJIN HA
9,696 Points

How to change $page title when the form is submitted..?

Hi,

I found that when I submit the form, the h1 tag ("suggest a media item") remains the same. So I tried to change it to "thank you!" making $pageTitle variable to vary based on the conditional. But it doesn't seem to work.

Below is the code I worked.

<?php if ($_SERVER["REQUEST_METHOD"] == "POST") { $name = $_POST["name"]; $email = $_POST["email"]; $details = $_POST["details"]; $pageTitle = "Thank you!"; $section = null; //To Do: Send Email header("location:suggest.php?status=thanks"); } else { $pageTitle = "Suggest a Media Item"; $section = "suggest"; } include("inc/header.php"); ?>

<div class="section page"> <div class="wrapper"> <h1><?php echo $pageTitle; ?></h1> <?php if (isset($_GET["status"]) && $_GET['status'] == "thanks") { echo "<p>Thanks for the email! I’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="suggest.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>
        </table>
        <input type="submit" value="Send" />
    </form>
    <?php } ?>
</div>

</div> </div>

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

2 Answers

Lucas Santos
Lucas Santos
19,315 Points
<?php

if ($_SERVER['REQUEST_METHOD'] == 'POST'){    //when the form is submitted
   $pageTitle = "My New Title!";   //do this
}
?>

couldn't get it to work with request_method, but checking GET['status'] worked:

<?php
if (isset($_GET['status'])) {
  $pageTitle = "Suggestion Recieved!";
}
else {
  $pageTitle = "Suggest a Media Item";
}
?>
Kyle Sullivan
Kyle Sullivan
15,150 Points

You also need to change the <h1> tag from "Suggest a media item" to $pageTitle.

From:

<h1>Suggest a Media Item</h1>

To this:

<?php echo "<h1>$pageTitle</h1>" ?>