centem
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_METHO D"] == $_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’ll be in touch shortly.</p>
<?php } else { ?>
<p>I’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'); ?>
<?php
if($_SERVER["REQUEST_METHO
$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’ll be in touch shortly.</p>
<?php } else { ?>
<p>I’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');
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.
<?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:
Then in the contact.php
<?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);
}
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’ll be in touch shortly.</p>
<?php } else { ?>
<p>I’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'); ?>
Try this
$url = "contact.php?status=thanks "";
header("Location: ".$url);
$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");
header("Location: http://www.yourdomain.com/contact.php?status=Thanks");
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/xhtm l+xml,appl ication/xm l;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:\WINDO WS\system3 2;C:\WINDO WS;C:\WIND OWS\System 32\Wbem;C: \WINDOWS\S ystem32\Wi ndowsPower Shell\v1.0 \;C:\Progr am Files\ActivIdentity\ActivC lient\;C:\ Program Files (x86)\ActivIdentity\ActivC lient\;C:\ Program Files\Tumbleweed\Desktop Validator\;C:\Program Files (x86)\ApproveIt\ThirdParty \Bin\;C:\P rogram Files (x86)\ApproveIt\;C:\Progra m 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:\Progr am 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:\Pr ogram Files (x86)\Microsoft SQL Server\100\Tools\Binn\VSSh ell\Common 7\IDE\;C:\ Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\PrivateAss emblies\;C :\Program Files (x86)\Microsoft SQL Server\100\DTS\Binn\;C:\pr ogra~1\JAV A\JRE7; [SystemRoot] => C:\WINDOWS [COMSPEC] => C:\WINDOWS\system32\cmd.ex e [PATHEXT] => .COM;.EXE;.BAT;.CMD;.VBS;. VBE;.JS;.J SE;.WSF;.W SH;.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/co ntact.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 ) )
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/xhtm
status="Thanks" is not there.
Have you used an absolute url as I suggested above?
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");
header("Location: http://localhost/shirts4mike/contact.php?status=Thanks");
Another option would be:
header("Location: http://localhost/shirts4mike/contact.php?status".urlencode("Thanks"));
header("Location: http://localhost/shirts4mike/contact.php?status".urlencode("Thanks"));
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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_METHO D"] == $_POST) {
header("Location: page2.php");
exit;
}
?>
<html>
<form action="page1.php" action="post">
<input type="submit" value="go to page two">
</form>
</html>
<?php
if($_SERVER["REQUEST_METHO
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 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
if($_SERVER["REQUEST_METHOD"] == 'POST') {
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
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
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!
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!
<?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