|
Posted by Umberto Salsi on 05/17/05 21:38
"Angelos" <angelos@redcatmedia.net> wrote:
> Ok... I have to make this administation area where I have multiple Contents
> to add edit delete publish .
> The problem is that I don't know what is the best way of making the forms.
> At the moment I create a new PHP file for add_page.php, add_student.php,
> add_news.php, edit_page.php list_page.php and so on.
>
> This way doesn't seem very good to me and I am wondering if I want to put
> them in one file or in some more generic files, how am I going to do that.
>
> For start created the content.php and did a switch $_GET['category'] and a
> nested $_GET['action'] but I don't know what each case: should contain.
>
> I don't want to put the whole html code for the forms in each case....
> I have tried puting each form in a function but I had some stupid errors and
> that also doesn't sound to me a good solution.
>
> So if someone has previous experience pls Suggest me something...
This is the basic schema I use: every GET/POST request should return
two parameters: the name of a function (func) and the argument of that
function (arg). The argument is serialized and URL-encoded.
<?
function add_page($form)
/* Show the FORM. $form may be null or may be the data to be
modified or to be corrected. */
{
echo "<html><body><h1>Add page</h1>"
. "<form method=post action=" . $_SERVER['PHP_SELF'] . ">"
. "<input type=hidden name=func value=POST_add_page>"
. "<input type=hidden name=arg value=>"
. "<input type=text name=field1 value=" . enc_val($form->field1) .">"
#...
. "<input type=text name=fieldn value=" . enc_val($form->fieldn) .">"
. "<input type=submit name=b value=Save>"
. "</form>"
. "</body></html>";
}
function POST_add_page($arg_not_used)
/* Take the POST request from add_page() */
{
/* Acquire POST into an object: */
$form->field1 = $_POST['field1'];
...
$form->fieldn = $_POST['fieldn'];
/* Validate all the data: */
$err = "";
if( $form->field1 is not valid )
$err .= "Data1 not valid because so and so...";
...
if( $form->field1 is not valid )
$err .= "Data1 not valid because so and so...";
if( strlen($err) == 0 ){
# store data in the DB.
# show next page - for example another empty form:
add_page(null);
} else {
echo "<html><body>Invalid: $err<p>"
. "<a href=$_SERVER['PHP_SELF']?func=add_page&arg="
. urlencode( serialize( $form ) ) . "RETRY</a>"
. "</body></html>";
}
}
/* More functions here. */
/* Request handler: */
$func = $_REQUEST['func'];
$arg = $_REQUEST['arg'];
if( strlen($arg) == 0 )
$arg = null;
else
$arg = unserialize($arg);
switch( $func ){
case 'add_page': add_page($arg); break;
case 'POST_add_page': POST_add_page($arg); break;
# more functions here.
default: something_maybe_add_page($arg);
}
?>
All the code may be semplified if you implement these functions;
function Form($func, $arg)
# Open the form that send the POST to the function $func with
# argument $arg; set the hidden fields accordingly.
function Anchor($text, $func, $arg)
# Print an anchor to call the function $func with argument $arg
# and clickable text $text.
As an evolution of these concepts, you may allow two or more buttons in
each form, associating each button to a different function/argument of
the module. This is coerent with the concept that every "click" on your
WEB application may call a different, specific function:
function Button($text, $func, $arg)
The correctness of the parameters returned by the client may be ensured
adding a MAC (message authentication code) computed over the $func and
the $arg variables. This MAC is generated by the functions Anchor() and
Button() and must be checked by the "request handler" on every request.
Another and even more interesting feature to explore is the introduction
of a stack of $func/$arg values, so to implement the "return" instruction
commonly available on every programming language. The availability of
a stack open the way to the modularization of the WEB applications.
Ciao,
___
/_|_\ Umberto Salsi
\/_\/ www.icosaedro.it
Navigation:
[Reply to this message]
|