Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2014-01-27
18
Medium Priority
?
479 Views
Last Modified: 2014-02-02
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
Comment
Question by:centem
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
  • 4
  • +1
18 Comments
 
LVL 29

Expert Comment

by:becraig
ID: 39812228
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
 

Author Comment

by:centem
ID: 39812348
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
 
LVL 29

Expert Comment

by:becraig
ID: 39812369
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39812371
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
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39812381
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
 
LVL 29

Expert Comment

by:becraig
ID: 39812383
Try this

$url = "contact.php?status=thanks"";
header("Location: ".$url);
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39812415
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
 

Author Comment

by:centem
ID: 39812818
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
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39812832
status="Thanks" is not there.
Have you used an absolute url as I suggested above?
0
 
LVL 29

Expert Comment

by:becraig
ID: 39812833
There you go, use Marco's idea for the complete URL

 header("Location: http://localhost/shirts4mike/contact.php?status=Thanks");
0
 
LVL 29

Expert Comment

by:becraig
ID: 39812865
Another option would be:

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

Author Comment

by:centem
ID: 39817955
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
 
LVL 31

Accepted Solution

by:
Marco Gasi earned 1500 total points
ID: 39818195
Then I suggest you to use $_SESSION: it's the most powerful toolPhp give to us to pass variable through pages. :)
0
 

Author Comment

by:centem
ID: 39818580
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
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39818709
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
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39819154
@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
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39828040
@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
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39828303
@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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question