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

SOOJIN HA
SOOJIN HA
9,696 Points

Changing $pageTitle 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 statement. But it doesn't seem to work. Can someone help me to find what's wrong with the code?

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"); ?>

Matthew Brock
Matthew Brock
16,791 Points

Making a bit easier to read

<?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="wrapper">
  <h1><?php echo $pageTitle; ?></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="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>

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

1 Answer

Matthew Brock
Matthew Brock
16,791 Points

when you do

header("location:suggest.php?status=thanks"); 

this would reload the page so that there is no longer a "POST" associated with the page. you would have to add this to the top of the page

<?php 
  if (isset($_GET["status"]) && $_GET['status'] == "thanks") {
    $pageTitle =  "Thank you!";
  }
?>

And then in the else statement check to see if $pageTitle has already been set. Usually you would handle the code in that file and not use the header() function.