Solved

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

Posted on 2014-01-27
18
464 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
  • 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 500 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 108

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 108

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

914 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now