Reply to Re: [PHP] recommended way for server side validation - include in same file or have a different file

Your name:

Reply:


Posted by Brent Baisley on 03/08/05 17:09

The way I do it is to have display, edit, validation and save all in
the same "file". I have an "op" variable that tracks whether the
operation is an edit or reedit (reuse submitted data) because of
validation failure. Another variable would track if it's a new entry
(blank fields) or editing an existing entry (retrieve data). For
clarity, I break out pieces into separate files and just include them
if needed. This keeps the file smaller and easier to traverse and
debug.

if ($op=='save') {
validate
if validated
include('saveFile.php')
op = 'view'
else
error message stuff
op = 'reedit'
}

if ($op=='reedit') {
prep submitted data for reediting, include error message(s)
} elseif ($id=='new') {
prep 'blank' data, set defaults
} elseif ($id>0) {
retrieve data for display or editing
}

if ($op=='edit' || $op=='reedit') {
create form field for data entry
} else {
massage data for display
}
Merge data with HTML template file
Display final page

That's basically my template. The "core" file is really just the logic,
data retrieval is obviously done through a function call. The display
piece, the html, is in a separate file and is structured for handling
simple data display or data entry form fields. The logic dictates the
role of the template, data entry or display. It's also then very simple
to create a display for cellphones, just create a different html
template. I've tried to follow the MVC (Model View Controller) design
pattern concept, which has worked very well for me.

Hope that helps.


On Mar 8, 2005, at 6:11 AM, Vinayakam Murugan wrote:

> Hi
>
> I have always felt for reasons for cleanliness and clarity, it is
> better to have the validation and insert / update statements in a
> seperate file rather than have it in the same script.
>
> Case 1
> ----------
> //file:form.php
> if $_POST
> then validate
> Insert or update on success
> else
> if $_GET['primarykey'] then
> show form for editing
> else
> show form for adding
> fi
> <form action="form.php" method=post>
> Show form
> </form>
>
> Case 2
> ----------
> //file:form1.php
> if $_GET['primarykey'] then
> show form for editing
> else
> show form for adding
> fi
> <form action="formvalidate.php" method=post>
> Show form
> </form>
>
>
>
> However in the second case, in case of an error, displaying the data
> becomes an issue.
>
> Any recommended ways for server side validation
>
> --
> Warm Regards
> ~~~~~~~~~~~~
> Vinayak
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
--
Brent Baisley
Systems Architect
Landover Associates, Inc.
Search & Advisory Services for Advanced Technology Environments
p: 212.759.6400/800.759.0577

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация