Why doesn't this work
| From: | Avi Schwartz | Date: | Sun, 10 Aug 2003 05:19:28 +0000 |
| Subject: | Why doesn't this work | ||
| Groups: | php.smarty.general | ||
| Request: | Send a blank email to php-smarty-general+get-10021@lists.php.net to get a copy of this message | ||
Hi,
I am just starting with both smarty AND php so I assume this is something stupid I am doing but I need help figuring this out.
The problem I am having is that the function process_form() in test.php validates the input fields from the template's form and assigns an error message to $errorMessage.
The popup debug console shows that the $errorMessage variable has the correct text, however when the template displays this variable it has no value so XX is displayed. What I am trying to figure out is what happened to the value of the $errorMessage variable.
Thanks,
Avi
-- setup.php--
// load Smarty library
require_once('smarty/Smarty.class.php');
require_once('DB.php');
class Smarty_test extends Smarty {
var $db;
function Smarty_test() {
// Class Constructor. These automatically get set with each new instance.
$this->Smarty();
$this->template_dir = '/srv/www/smarty_templates/';
$this->compile_dir = '/srv/www/smarty_templates_c/';
$this->config_dir = '/srv/www/smarty_configs/';
$this->cache_dir = '/srv/www/smarty_cache/';
$this->caching = true;
$this->debugging = true;
$this->assign('app_name','Test');
$this->$db = DB::connect("pgsql://user:password@server/database");
if (DB::iserror($this->$db)) {
die($this->$db->getMessage());
}
}
}
-- test.php
require('/srv/www/smarty_includes/setup.php');
$smarty = new smarty_test();
if (isset($_POST['step']) && ('step1' == $_POST['step'])) {
process_form();
}
$smarty->display(test.tpl');
function process_form() {
global $smarty;
$field1 = trim($_POST['field1]);
$field2 = trim($_POST['field2']);
if (strlen($field1) == 0 || strlen($field2) == 0) {
$smarty->assign('errorMessage', 'Both fields are required.');
return;
}
}
-- test.tpl
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Test</title>
</head>
<body>
X{$errorMessage}X
</body>
</html>