888888.888888.88""Yb..dP"Yb..8888b..Yb..dP.88b.88....db....8b....d8.88..dP""b8..dP"Y8
88__...88__...88__dP.dP...Yb..8I..Yb.YbdP..88Yb88...dPYb...88b..d88.88.dP...`".`Ybo."
88""...88""...88"Yb..Yb...dP..8I..dY..8P...88.Y88..dP__Yb..88YbdP88.88.Yb......o.`Y8b
88.....888888.88..Yb..YbodP..8888Y"..dP....88..Y8.dP""""Yb.88.YY.88.88..YboodP.8bodP'


88b.88.888888.888888.Yb........dP.dP"Yb..88""Yb.88..dP
88Yb88.88__.....88....Yb..db..dP.dP...Yb.88__dP.88odP.
88.Y88.88"".....88.....YbdPYbdP..Yb...dP.88"Yb..88"Yb.
88..Y8.888888...88......YP..YP....YbodP..88..Yb.88..Yb

November 14, 2007

So you’re writing a plugin that uses an object to maintain the state of your application, perhaps the object is used to construct a document, or a menu, or something else entirely, and your application requires multiple form submissions. How do you maintain the state of your object across submissions?

There are lots of ways to do this, use sessions, use hidden fields, recreate the object manually with each postback. Which method proves most obvious depends on your background, and experience. The most usual for PHP, is to use session variables to save the data

For the examples I’m going to create an object with four methods: getters and setters for the internal data, one to save the state and one to reload it again, exactly how and when these are called may differ depending on circumstances.

[php]
class myClass{

//the array the data will be stored in
var $collection = array();

function getVar($name){
if ( isset($this->collection[$name]) ) {
return $this->collection[$name];
}
}

function setVar($name = '', $value = ''){
$this->collection[$name] = $value;
}

function loadState(){
if ( isset( $_SESSION['myClass_state'] ) ) {
$data = $_SESSION['myClass_state'];
$this->collection = (!empty($data['collection'])) ? $data['collection'] : array();
//repeat for any other data maintained
}
}

function saveState(){
$data['collection'] = $this->collection;
//add any other internal data in the same way
$_SESSION['myClass_state'] = $data;
}

}

These methods can then be called after checking to see whether the form is actually being submitted.

[php]

$myClass = new myClass();

if ( isset( $_POST['submit'] ) ) {
$myClass->loadState();

//make any changes dictated by the information that has been posted
$myClass->setVar('post_title',$valueSubmitedInForm);

$myClass->saveState();

}

An interesting alternative to this, which I haven’t used in anger but have considered, is to use hidden forms to contain the same data. Instead of saving the information in a session variable the data is serialized, base64 encoded, and added as the value of a hidden field.

There are more security issues with doing things this way though so it is probably best avoided.

Wordpress 25 Exif Fields
Wordpress exif plugin?
12 comments
page 230
Adding Settings To Admin Pages
Wordpress addsettingsfield?
3 comments
page 793
Updating Code Snippets Here
Fun wordpress plugins?
no comment
page 1338
Html 5 Gallery
Html5 image gallery?
6 comments
page 1305
How To Add Sidebars To A Theme
How to register two sidebars in wp?
10 comments
page 1053
Post Image The Easy Peasy Way
Wordpress theimage?
26 comments
page 1065
Using Your Own Url Shortener
Short url?
4 comments
page 1190
Using Wordpress As A Php Framework
Php framework wordpress?
2 comments
page 335
Html 5 Gallery
Html 5 gallery?
6 comments
page 1305
Wordpress Vs Graffiti
Html5 cms?
8 comments
page 95
Post Image The Easy Peasy Way
Wordpress medium size image link?
26 comments
page 1065
Wordpress Chat
Wordpress chat?
3 comments
page 1308
Html 5 Gallery
Wordpress html5?
6 comments
page 1305
Quick N Dirty Admin Login Screen
Web templte admin login page?
no comment
page 128
Post Image The Easy Peasy Way
Wp get url functions?
26 comments
page 1065
Html 5 Gallery
Wordpress html5 theme?
6 comments
page 1305
Post Image The Easy Peasy Way
Wordpress get first image attached to post?
26 comments
page 1065
Html 5 Gallery
Wordpress html5?
6 comments
page 1305
Post Image The Easy Peasy Way
Wp automatically insert image based on tags?
26 comments
page 1065
What Wordpress Workflow Needs
Word press workflow?
3 comments
page 1226
Quick N Dirty Admin Login Screen
Login screen css?
no comment
page 128
How To Add Sidebars To A Theme
Wordpress thematic modify sidebars?
10 comments
page 1053
Six Million Ways To Die Choose One
Ways to die without noticing?
14 comments
page 1128
Post Image The Easy Peasy Way
Wordpress theimage?
26 comments
page 1065
How To Add Sidebars To A Theme
How to add a sidebar to a theme?
10 comments
page 1053
Using Your Own Url Shortener
Htaccess tiny url shortner?
4 comments
page 1190
Photoshop Design Framework
Photoshop framework?
3 comments
page 296
Creating Custom Urls
Custom url for wordpress page?
6 comments
page 80
Photoshop Design Framework
Framework in photoshop?
3 comments
page 296
Html 5 Gallery
Wordpress html5?
6 comments
page 1305
Using Your Own Url Shortener
Build your own url shortener?
4 comments
page 1190
My Experience Of Flexx
Cant upload image to flexx wordpress theme?
4 comments
page 1026
Post Image The Easy Peasy Way
Wpget post image?
26 comments
page 1065
  1 query every 1604 seconds, updated 1 seconds ago.
Post a comment?