Link to home
Start Free TrialLog in
Avatar of centem
centemFlag for United States of America

asked on

contact.php not displaying "Thanks for the email!" message

Greetings, I am working on the "Working with Get Variables" PHP module and have been following along but my code is not working as it is in the video. My form should redirect to the same page but show the thank you message. Instead it simply shows the blank form again. Below is my contact.php code. Thanks.

<?php

if($_SERVER["REQUEST_METHOD"] == $_POST) {
            
      $name = $_POST["name"];
      $email = $_POST["email"];
      $message = $_POST["message"];
      $email_body = "";
      $email_body  = $email_body . "Name: " . $name . "\n";
      $email_body = $email_body . "Email: " . $email . "\n";
      $email_body = $email_body . "Message: " . $message;
            
      // TODO: Send Email

      header("Location: contact.php?status=thanks");
}
?>

<?php
$pageTitle = "Contact Mike";
$section = "contact";
include('inc/header.php'); ?>

      <div class="section page">
            <div class="wrapper">
            
                  <h1>Contact</h1>
                  
                  <?php if (isset($_GET["status"]) AND $_GET["status"] == "Thanks") { ?>
                        <p>Thanks for the email! I&rsquo;ll be in touch shortly.</p>
                  <?php } else { ?>
                  
                        <p>I&rsquo;d love to hear from you! Complete the form to send me an email.</p>
                        
                        <form method="post" action="contact.php">
                              
                              <table>
                                    <tr>
                                          <th>
                                                <label for="name">Name</label>
                                          </th>
                                          <td>
                                                <input type="text" name="name" id="name">
                                          </td>
                                    </tr>
                                          <tr>
                                          <th>
                                                <label for="name">Email</label>
                                          </th>
                                          <td>
                                                <input type="text" name="email" id="email">
                                          </td>
                                    </tr>
                                          <tr>
                                          <th>
                                                <label for="name">Message</label>
                                          </th>
                                          <td>
                                                <textarea name="message" id="message"></textarea>
                                          </td>
                                    </tr>
                              </table>
                              <input type="submit" value="Send">
                        </form>
                  <?php } ?>
            </div>
      </div>
      
      
<?php include('inc/footer.php'); ?>
Avatar of becraig
becraig
Flag of United States of America image

It looks like case sensitivity to me.


                  <?php if (isset($_GET["status"]) AND $_GET["status"] == "Thanks") { ?>
      header("Location: contact.php?status=thanks");

Change to :

      header("Location: contact.php?status=Thanks");

All else looks good to me
Avatar of centem

ASKER

Thanks for the response but it still does not work. It just redirects to the blank form and not executing the "Thank for the email" message.
Can you please as a test add this to your code:

<?php
$arr = get_defined_vars();
print_r($arr);

Right at the top of your page ( <?php) , so when it reloads we will know if the variable is actually defined.
With header function you can't send parameters, so the contact.php page doesn't receive the string status="Thanks" a it doesn't display the message.
You have to store the status in a session variable:

<?php
session_start();
if($_SERVER["REQUEST_METHOD"] == $_POST) {
            
      $name = $_POST["name"];
      $email = $_POST["email"];
      $message = $_POST["message"];
      $email_body = "";
      $email_body  = $email_body . "Name: " . $name . "\n";
      $email_body = $email_body . "Email: " . $email . "\n";
      $email_body = $email_body . "Message: " . $message;

      $_SESSION[ìstatus'] = "Thanks";
            
      // TODO: Send Email

      header("Location: contact.php);
}

Open in new window


Then in the contact.php

<?php
session_start();
$pageTitle = "Contact Mike";
$section = "contact";
include('inc/header.php'); ?>

      <div class="section page">
            <div class="wrapper">
            
                  <h1>Contact</h1>
                  
                  <?php if (isset($_SESSION["status"]) AND $_SESSION["status"] == "Thanks") { ?>
                        <p>Thanks for the email! I&rsquo;ll be in touch shortly.</p>
                  <?php } else { ?>
                  
                        <p>I&rsquo;d love to hear from you! Complete the form to send me an email.</p>
                        
                        <form method="post" action="contact.php">
                              
                              <table>
                                    <tr>
                                          <th>
                                                <label for="name">Name</label>
                                          </th>
                                          <td>
                                                <input type="text" name="name" id="name">
                                          </td>
                                    </tr>
                                          <tr>
                                          <th>
                                                <label for="name">Email</label>
                                          </th>
                                          <td>
                                                <input type="text" name="email" id="email">
                                          </td>
                                    </tr>
                                          <tr>
                                          <th>
                                                <label for="name">Message</label>
                                          </th>
                                          <td>
                                                <textarea name="message" id="message"></textarea>
                                          </td>
                                    </tr>
                              </table>
                              <input type="submit" value="Send">
                        </form>
                  <?php } ?>
            </div>
      </div>
      
      
<?php include('inc/footer.php'); ?> 

Open in new window

Try this

$url = "contact.php?status=thanks"";
header("Location: ".$url);
Or, if you don't want use session, you have to use absolute url:

 header("Location: http://www.yourdomain.com/contact.php?status=Thanks");
Avatar of centem

ASKER

Becraig,
as soon as refreshing the page after inserting the following onto the contact.php
$arr = get_defined_vars();
print_r($arr);

I get this at the top of the page.

Array ( [_GET] => Array ( ) [_POST] => Array ( ) [_COOKIE] => Array ( ) [_FILES] => Array ( ) [_ENV] => Array ( ) [_REQUEST] => Array ( ) [_SERVER] => Array ( [HTTP_HOST] => localhost [HTTP_USER_AGENT] => Mozilla/5.0 (Windows NT 6.1; WOW64; rv:26.0) Gecko/20100101 Firefox/26.0 [HTTP_ACCEPT] => text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 [HTTP_ACCEPT_LANGUAGE] => en-US,en;q=0.5 [HTTP_ACCEPT_ENCODING] => gzip, deflate [HTTP_REFERER] => http://localhost/shirts4mike/ [HTTP_CONNECTION] => keep-alive [PATH] => C:\Program Files (x86)\AMD APP\bin\x86_64;C:\Program Files (x86)\AMD APP\bin\x86;C:\Program Files (x86)\Gradkell Systems, Inc\DBsign Data Security Suite\Common\Lib\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files\ActivIdentity\ActivClient\;C:\Program Files (x86)\ActivIdentity\ActivClient\;C:\Program Files\Tumbleweed\Desktop Validator\;C:\Program Files (x86)\ApproveIt\ThirdParty\Bin\;C:\Program Files (x86)\ApproveIt\;C:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static;C:\Program Files (x86)\Microsoft Application Virtualization Client;C:\Program Files\Microsoft\Web Platform Installer\;C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET Web Pages\v1.0\;C:\Program Files\Microsoft SQL Server\110\Tools\Binn\;C:\Program Files (x86)\Common Files\Roxio Shared\DLLShared\;C:\Program Files (x86)\Common Files\Roxio Shared\10.0\DLLShared\;C:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\;C:\Program Files\Microsoft SQL Server\100\Tools\Binn\;C:\Program Files\Microsoft SQL Server\100\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\VSShell\Common7\IDE\;C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\PrivateAssemblies\;C:\Program Files (x86)\Microsoft SQL Server\100\DTS\Binn\;C:\progra~1\JAVA\JRE7; [SystemRoot] => C:\WINDOWS [COMSPEC] => C:\WINDOWS\system32\cmd.exe [PATHEXT] => .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC [WINDIR] => C:\WINDOWS [SERVER_SIGNATURE] => [SERVER_SOFTWARE] => Apache/2.4.4 (Win32) PHP/5.4.16 [SERVER_NAME] => localhost [SERVER_ADDR] => 127.0.0.1 [SERVER_PORT] => 80 [REMOTE_ADDR] => 127.0.0.1 [DOCUMENT_ROOT] => C:/wamp/www [REQUEST_SCHEME] => http [CONTEXT_PREFIX] => [CONTEXT_DOCUMENT_ROOT] => C:/wamp/www [SERVER_ADMIN] => admin@example.com [SCRIPT_FILENAME] => C:/wamp/www/shirts4mike/contact.php [REMOTE_PORT] => 62839 [GATEWAY_INTERFACE] => CGI/1.1 [SERVER_PROTOCOL] => HTTP/1.1 [REQUEST_METHOD] => GET [QUERY_STRING] => [REQUEST_URI] => /shirts4mike/contact.php [SCRIPT_NAME] => /shirts4mike/contact.php [PHP_SELF] => /shirts4mike/contact.php [REQUEST_TIME_FLOAT] => 1390846631.631 [REQUEST_TIME] => 1390846631 ) )
status="Thanks" is not there.
Have you used an absolute url as I suggested above?
There you go, use Marco's idea for the complete URL

 header("Location: http://localhost/shirts4mike/contact.php?status=Thanks");
Another option would be:

header("Location: http://localhost/shirts4mike/contact.php?status".urlencode("Thanks"));
Avatar of centem

ASKER

BTW,
I used the complete url which is http://localhost/shirts4mike/contact.php

Still does not work. It simply refresh the page with blank form and no "Thanks for the email!" message.
ASKER CERTIFIED SOLUTION
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of centem

ASKER

Can you give example how I use the $_SESSION variable? In the meantime here is an even simpler code that still doesn't work. Page1.php and page2.php. Page1 code below. Page1 will not redirect to page2 for some reason. What is wrong? Thanks.

<?php
      if($_SERVER["REQUEST_METHOD"] == $_POST) {
            header("Location: page2.php");      
            exit;
}
?>

<html>

      <form action="page1.php" action="post">
            <input type="submit" value="go to page two">
      </form>

</html>
Use this

if($_SERVER["REQUEST_METHOD"] == 'POST') {

Open in new window


if you want ensure data have been posted (but in some php+apache2 installations it can return post even for $_GET).

About example of use of session I gave you one above
@centem, as I asked in the other question, Why do you want to do this header() command to redirect the client browser?  It's just not a very common design pattern (in fact I would go so far as to say it's way out on the edge of instability) and there are probably much better ways for us to guide you if you can explain what you're trying to achieve (in plain, non-technical terms).

You might also want to get a better foundation in how PHP works, what learning resources are dependable, etc.  This article can give you some ideas and links, and perhaps more importantly it can steer you away from the many bad PHP examples that litter the internet.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html
@centem, can you explain the reason of the downgrade? The default value is A and another choice means that the answer was seriously deficient. I immediately indicated the use of session as the best solution and any other solution simply doesn't work: so why my answer should be considered deficient?

Learn here about the grading rules: http://support.experts-exchange.com/customer/portal/articles/481419
@centem: I hope you will take to heart the guidance in this article.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html

And I agree with Marco.  You gave the wrong grade and you should change it!