Solved

Confirmation message on same page

Posted on 2007-12-06
7
583 Views
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!
0
Comment
Question by:thephiller
  • 3
  • 2
  • 2
7 Comments
 
LVL 10

Accepted Solution

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

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

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

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

That make sense?
0
 
LVL 1

Expert Comment

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

session_start();  
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
Header("Location:yourmainpage.php");
$_SESSION["confirmation"] = "your confirmation message";
}else{
Header("yourmainpage.php");
$_SESSION["confirmation"] = "error message";
}
0
 
LVL 1

Expert Comment

by:deanmb
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.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 10

Expert Comment

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

Author Comment

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

Author Comment

by:thephiller
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.
0
 
LVL 10

Expert Comment

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

Featured Post

Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to dynamically set the form action using jQuery.
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 …

758 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

21 Experts available now in Live!

Get 1:1 Help Now