|
Posted by Jon Slaughter on 05/21/07 00:24
I have this idea I've been working on and I have some implementation but
wondering how many of you guys would find it useful. Heres the outline of
how it works.
Essentially one has what are called actions. An action is a block of php
code that is executed after a form as been processed but is defined inline
in the form. The idea is that you insert the actions inline in code but you
can presume that the form has been filled out. Your code, while inline, will
actually be executed after the fact on the next page change(which can be
made to be the same page.
You can see this in action at www.jonslaughter.com/Index.php. Note that All
the code is on the Index page except for a class script that is used for the
cookies and manage the actions.
The main parts are the form and the action handler. The handler executes the
actions after the form has been handled.
If anyone things this thing might be useful then I'm thinking about
rewriting everything and make it a little more clean. What it does allow one
to do is treat forms as inline objects that are not event driven. The
drawback is that the code to handle them must be in a string instead of
actual php code that will be parsed, say, by an IDE. Maybe someone knows of
a way around this or has some idea to make it better(I know there are going
to be the regular nay sayers but who knows).
Again, the main idea is to treat form evaulation as inline instead of on a
seperate page. In this way one encapsulates the function of the form with
the form itself. Its slightly more work but might be worth the effort... and
actually can be less work and ultimately less confusing. Note also that the
action handler is fixed and would just need to be included at the top of
every page.
Thanks,
Jon
<?php
session_start();
require_once($_SERVER['DOCUMENT_ROOT'].'/Scripts/WebLogin.php/');
$login = new WebLogin();
// This code is essentially the actions handler that executes all actions
and in this case sets the cached cookies
if ((!empty($_POST)) & ($login->RunActions()))
{
$url = $_SESSION['URL'];
$login->SetCookies();
session_destroy();
unset($_SESSION);
unset($_COOKIE);
echo '<meta content="0; URL='.$url.'" http-equiv="Refresh" />';
exit();
}
else
{
unset($_SESSION['CALLBACKS']);
unset($_SESSION['ACTIONS']);
unset($_SESSION[$login->cookietable['UserCookieData']]);
}
// Decodes the cookies because they are encrypted when loaded
$login->DecodeCookies();
// Displays the cookies
if (empty($_COOKIE) | !isset($_COOKIE)) { echo "<br />No Cookies<br />\n"; }
else { echo "<br />Displaying Cookies<br />\n"; foreach($_COOKIE as $key =>
$val) { echo " ".$key." =>
".htmlentities(serialize($val))."<br />\n"; }}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<meta http-equiv=Content-Type content="text/html; charset=windows-1252">
</head>
<body>
<form action="" method="post">
<label><?php if (empty($_POST['Cookie_Name'])) { echo '<span
style="color:#FF0000;">*Cookie Name: </span>';} else { echo 'Cookie Name:
';} ?></label><input name="Cookie_Name" value="<?php echo
$_POST["Cookie_Name"]; ?>" type="text" /><br />
<label><?php if (empty($_POST['Name'])) { echo '<span
style="color:#FF0000;">*Name: </span>';} else { echo 'Name: ';}
?></label><input name="Name" value="<?php echo $_POST["Name"]; ?>"
type="text" /><br />
<label><?php if (empty($_POST['Data'])) { echo '<span
style="color:#FF0000;">*Data: </span>';} else { echo 'Data: ';}
?></label><input name="Data" value="<?php echo $_POST["Data"]; ?>"
type="text" /><br />
<input name="FormAction" type="submit" value="Add Cookie">
</form>
<?php
// These handlers just check the forms entries to make sure they are not
empty... if they are then the page is not complete and it should be
re-evaluated...
$login->AddAction('Add Cookie', 'if (empty($_POST["Cookie_Name"])) { return
FALSE; } ');
$login->AddAction('Add Cookie', 'if (empty($_POST["Name"]) |
empty($_POST["Data"])) { return FALSE; } ');
// This adds a cookie with the form data if the actions were all valid and
sets the page to goto... in this case Index.php
$login->AddCookieP('Add Cookie', '$_SESSION["URL"] = "/Index.php/"; return
TRUE;', 'return $_POST["Cookie_Name"];', 'return array("username" =>
$_POST["Name"], "userdata" => $_POST["Data"]);', array('location' => '/'));
?>
<form action="" method="post">
<label><?php if (empty($_POST['Cookie_NameD'])) { echo '<span
style="color:#FF0000;">*Cookie Name: </span>';} else { echo 'Cookie Name:
';} ?></label><input name="Cookie_NameD" value="<?php echo
$_POST["Cookie_NameD"]; ?>" type="text" /><br />
<input name="FormAction" type="submit" value="Delete Cookie">
</form>
<?php
$login->AddAction('Delete Cookie', 'if (empty($_POST["Cookie_NameD"])) {
return FALSE; } ');
$login->DeleteCookieP('Delete Cookie', '$_SESSION["URL"] = "/Index.php/";
return TRUE;', 'return $_POST["Cookie_NameD"];');
?>
</body>
</html>
[Back to original message]
|