Managing NULL and worse

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Managing NULL and worse

Post by Ollie Saunders »

I've got this code:

Code: Select all

$in = $this->getInput();
$clean = array('txtTitle'           => $this->_escape($in->getRaw('txtTitle'))           or '',
               'selClient'          => $in->getInt('selClient')                          or 0,
               'selSubclient'       => $in->getInt('selSubclient')                       or 0,
               'fsVideo'            => $in->getInt('fsVideo')                            or true,
               'txtUrlStream'       => $this->_escape($in->getRaw('txtUrlStream'))       or '',
               'txtUrlDownload'     => $this->_escape($in->getRaw('txtUrlDownload'))     or '',
               'txtWidth'           => $this->getInt('txtWidth')                         or 0,
               'txtHeight'          => $this->getInt('txtHeight')                        or 0,
               'txtMsgFeedback'     => $this->_escape($in->getRaw('txtMsgFeedback'))     or '',
               'txtMsgStream'       => $this->_escape($in->getRaw('txtMsgStream'))       or '',
               'txtMsgDownload'     => $this->_escape($in->getRaw('txtMsgDownload'))     or '',
               'fsQuestionnaire'    => $this->getInt('fsQuestionnaire')                  or true,
               'txtMsgOnStart'      => $this->_escape($in->getRaw('txtMsgOnStart'))      or '',
               'txtMsgOnceFinished' => $this->_escape($in->getRaw('txtMsgOnceFinished')) or '');
$clean = (object)$clean;
It's getting posted values from a form or if they aren't present assigning them with a default.

However there are a couple of problems:
  • The first line ($in = $this->getInput();) can fail, as in return NULL, if the form hasn't been posted in which case $in->getRaw('txtTitle') generates an E_FATAL because $in isn't an object.
  • I can't make the assignment of $clean conditional based on $in because then my defaults wouldn't be assigned.
So basically I'm looking for a nice solution for managing default values where form isn't posted or any single field isn't posted.
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

Seems overkill to me how you're managing the form input.

Why not just do this:

Code: Select all

if the form has been submitted
    do whatever with the data
end if
which is usually for me

Code: Select all

if(isset($_POST['submit'])) {
    //escape stuff
}
Post Reply