Confirmation message on same page

Posted on 2007-12-06
Last Modified: 2012-05-05
I've been wondering how I can display confirmation or error messages inside the same page, kind of like what the admin area of wordpress does.

For example if a user updates a database table through a form field and it's a success then the user would be returned back to the original page (using the php header function) with the confirmation echoed inside a <div> tag or something similar.

Or how about when a user tries to log in and enters the wrong username or password. How would I then display a message like "Sorry, try again" on the same page as the form?

Anyone got any ideas how to do this? it would help me a lot for my project!
Question by:thephiller
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
  • 3
  • 2
  • 2
LVL 10

Accepted Solution

wildzero earned 500 total points
ID: 20422573
You could use sessions to do this which would mean it wouldn't effect your URL.
Lets say they update there table, at the top of the php make sure to have

then have like

If (do this operation) {
  //-- do some fancy stuff.
  $_SESSION['message'] = 'Operation was complete';

now all we have to do is call out the message when we want to display it, if it's set. So on your original page, make sure to have session_start(); at the very very top then have like

If (isset($_SESSION['message'])) {
  echo "<div>".$_SESSION['message']."</div>";

Notice the unset there - this means if they refresh the page the message wont show again.

That make sense?

Expert Comment

ID: 20422652
here is a very simple way..  on the main main page put sessions like
start the session on both pages with

header("Cache-control: private");

then on the main page put sessions like

echo $_SESSION["confirmation"]; // this echos the session
unset($_SESSION['confirmation']); //this unsets it so if they hit back or forward its no longer there

then on the page that does the login or whatever put

if($password == $dbpassword){ //or whatever your if statement is
$_SESSION["confirmation"] = "your confirmation message";
$_SESSION["confirmation"] = "error message";

Expert Comment

ID: 20422673
Very sorry wildzero i was typing my message when you submitted yours, funny we had the same idea and i thought mine was original haha.  Sorry again.
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

LVL 10

Expert Comment

ID: 20422680
hehe yea it sucks to get pipped at the post :-) happens alot lol

Author Comment

ID: 20422757
Hahaha, thanks for the enthusiastic reaction guys. I'll try it out now and report back ;)

Author Comment

ID: 20423161
Thanks both!

I did give the points to wildzero since he replied first. I hope this is the OK way to do this? (i'm fairly new).
Thanks again, solution worked like a charm.
LVL 10

Expert Comment

ID: 20423172
Sure, but I didn't mind a split since it was so close :-)

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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 i…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…
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.

751 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