Unviewed
Unviewed

Reputation: 1

Display something based off a PHP session

I need some help with displaying a success message on my website.

Whats supposed to happen: You submit a form, it then sends it to my email ( all works flawlessly ) then it takes you to my homepage and displays this

[php]
<html>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script> 
$(document).ready(function(){
  $("#flip").click(function(){
    $("#panel").slideToggle("slow");
  });
});
</script>

<style> 
#panel,#flip
{
padding:2px;
text-align:center;
background-color:#00BFFF;
border:solid 1px #c3c3c3;
}
#panel
{
padding:20px;
display:none;
}
</style>
<body>

<div id="flip"><b>Submitted!</b> (Click for more)</div>
<div id="panel">Your form has been submitted. If your form required being contacted, you will be contacted ASAP.</div>
</body>
</html>
[/php]

The problem is, I need that ^ to only appear when you submit a form. As of now, it appears even if you don't submit a form.

With some help of some people, I was informed to use PHP sessions to display it only when you click the submit button.

After compiling both my code, with theirs we got the code below. The "Home Page:" represents where the submit button will take you, along with where it displays the "success!" form. The "Submit form page:" is the page in which the session is started. Someone please tell me what is wrong here.

Home Page:

[php]
<!-- PHP session beginning -->
<?php
session_start();
if (isset($_SESSION["sent"])) {
 // Start
?>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script> 
$(document).ready(function(){
  $("#flip").click(function(){
    $("#panel").slideToggle("slow");
  });
});
</script>

<style> 
#panel,#flip
{
padding:2px;
text-align:center;
background-color:#00BFFF;
border:solid 1px #c3c3c3;
}
#panel
{
padding:20px;
display:none;
}
</style>
<body>

<div id="flip"><b>Submitted!</b> (Click for more)</div>
<div id="panel">Your form has been submitted. If your form required being contacted, you will be   

contacted ASAP.</div>

</body>
</html>
<?php
session_start();
// terminate the session
session_destroy();
?>
<!-- PHP Session end. Displays sent information only when sent -->[/php]

Contact submit page:

[php]
<?php
session_start();
$_SESSION["sent"] = true;  // or any value really
?> 
[/php]

I really, really appreciate you reading this. You can ignore the [php] brackets,

Contact form (Per request)

    <!--Form start-->
                        <form name="htmlform" method="post" action="html_form_send.php">
<table width="450px">
</tr>
<tr>
 <td valign="top">
  <label for="first_name">Name *</label>
 </td>
 <td valign="top">
  <input  type="text" name="first_name" maxlength="30" size="20">
 </td>
</tr>

<tr>
 <td valign="top"">
  <label for="last_name">Username *</label>
 </td>
 <td valign="top">
  <input  type="text" name="last_name" maxlength="30" size="20">
 </td>
</tr>
<tr>
 <td valign="top">
  <label for="email">Skype Username *</label>
 </td>
 <td valign="top">
  <input  type="text" name="email" maxlength="30" size="20">
 </td>

</tr>
<tr>
 <td valign="top">
  <label for="telephone">Previous Staff Rank</label>
 </td>
 <td valign="top">
  <input  type="text" name="telephone" maxlength="30" size="20">
 </td>
</tr>
<tr>
 <td valign="top">
  <label for="comments">Why should we hire you? *</label>
 </td>
 <td valign="top">
  <textarea  name="comments" maxlength="1000" cols="25" rows="3"></textarea>
 </td>

</tr>
<tr>
 <td colspan="2" style="text-align:center">
  <input type="submit" value="Submit">
 </td>
</tr>
</table>
</form><br><br><br>
<!--Form end-->

The "html_form_send" is the code that handles this, which is where I would start the PHP session.

Upvotes: 0

Views: 80

Answers (2)

emte
emte

Reputation: 647

You can do something like this:

file with form:

<?php
session_start();
if(isset($_POST['email'])){
  //sent e-mail etc.
  $_SESSION['success_message'] = 'Sent!';
}
?>
<form>
  <input type="text" name="email">
 ...
</form>

homepage:

<?php session_start(); ?>
... your content
<?php if(isset($_SESSION['success_message'])): ?>
 <p>Message was sent</p>
 <?php unset($_SESSION['success_message']; ?>
<?php endif; ?>

Upvotes: 0

Verhaeren
Verhaeren

Reputation: 1661

Make sure you are initializing de session before anything. This is wrong:

<!-- PHP session beginning -->
<?php
session_start();

It must be:

<?php
session_start();

Remove even a blank space (if any) between <?php and session_start();. For example, this:

<?php session_start();

wouldn't work either.

Upvotes: 1

Related Questions