Implementing POSTBACK

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Pretty much just a wrapper yes, but when you're working in a OO environment it just feels more natural. Plus you can make it a container for any data you wish to be available throughout the current request via setAttribute() and getAttribute() methods acting as a sort-of registry.

Code: Select all

if ($this->getRequest()->getMethod() != Request::POST) {
  //whatever
}

Code: Select all

public function validateSomeAction() {
  if (!strpos($this->getRequest()->getParameter("email"), "@")) {
    $this->getRequest()->setError("email", "The email address is not valid.");
    return false;
  }
}

Code: Select all

<?php if ($req->hasErrors()): ?>
  <h3>There was a problem</h3>
  <ul>
  <?php foreach ($req->getErrors() as $error): ?>
    <li><?php echo $error ?></li>
  <?php endforeach ?>
  </ul>
<?php endif ?>
Have a look at other request object implementations.

http://www.symfony-project.com/api/symf ... quest.html
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Hockey wrote:POSTBACK is not a feature exclusive to ASP.NET, PRADO implements a POSTBACK mechanism as well.
That's because PRADO is trying to be .NET ;)
Hockey wrote:Really, it just avoids having to re-POST data when a FORM doesn't validate, how you implement doesn't really matter IMHO.
From my understanding POSTBACK has nothing to do with avoiding reposting form data. Postbacks can be used for all sorts of things in which you want to know if the page that was posted happens to be the same page you are on after the page (in .NET the entire page is one big form) was posted. I know from some of my tests that POSTBACK can maintain state from the original post page as well as state from the new post page.
Hockey wrote:I am now more curious as to what kind of information or purpose a REQUEST object holds. Until now I haven't been able to justify adding an object called request, to simply wrap super globals such as GPC.
Remember the request and response objects from ASP.old (VBScript). Yeah, the request object could be something like that.
Hockey wrote:Any examples or detailed reasons for using REQUEST?
If you happen to touch REQUEST data more than once, and you are building an OOP application, it makes sense to put the REQUEST specific data into request object, much like it makes sense to put the RESPONSE type stuff into a RESPONSE object. No?
wei
Forum Contributor
Posts: 140
Joined: Wed Jul 12, 2006 12:18 am

Post by wei »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Postback is actually what Hockey envisaged, it repopulates the form that was posted. That's all there is to it. To detect a post back, you just need to detect if a http POST submission was performed. Without "control components" you would have to do what thinsoldier suggested e.g.

Code: Select all

<input type="text" name="color" value="<?= post_value('color', 'default colour') ?>" />
where post_value is a function that may look like (similar to what thinsoldier says)

Code: Select all

function post_value($element_id, $default_value=null)
{
    if(determine if post back)
        return $_POST[$element_id]; 
    else
       return $default_value;
}
the next step you may want is to check if the value was changed, then after may be determine the validity of the value and so on. Eventually, you probably want to abstract and encapsulate this process. Hence, the design will most likely lead to a sort of "control components" ;) The viewstate thing (built upon this postback) in ASP.NET is a different matter and has a major application design impact (all design has trade-offs)


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Post Reply