Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 484
  • Last Modified:

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'); ?>
0
centem
Asked:
centem
  • 7
  • 5
  • 4
  • +1
1 Solution
 
becraigCommented:
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
0
 
centemAuthor Commented:
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.
0
 
becraigCommented:
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.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Marco GasiFreelancerCommented:
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.
0
 
Marco GasiFreelancerCommented:
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

0
 
becraigCommented:
Try this

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

 header("Location: http://www.yourdomain.com/contact.php?status=Thanks");
0
 
centemAuthor Commented:
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 ) )
0
 
Marco GasiFreelancerCommented:
status="Thanks" is not there.
Have you used an absolute url as I suggested above?
0
 
becraigCommented:
There you go, use Marco's idea for the complete URL

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

header("Location: http://localhost/shirts4mike/contact.php?status".urlencode("Thanks"));
0
 
centemAuthor Commented:
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.
0
 
Marco GasiFreelancerCommented:
Then I suggest you to use $_SESSION: it's the most powerful toolPhp give to us to pass variable through pages. :)
0
 
centemAuthor Commented:
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>
0
 
Marco GasiFreelancerCommented:
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
0
 
Ray PaseurCommented:
@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.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html
0
 
Marco GasiFreelancerCommented:
@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
0
 
Ray PaseurCommented:
@centem: I hope you will take to heart the guidance in this article.
http://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!
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 7
  • 5
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now