You are here: Problem combining validation and templates « PHP Programming Language « IT news, forums, messages
Problem combining validation and templates

Posted by Phil Latio on 05/17/07 01:00

Feelin' a bit guilty about monopolising the group (3 posts in 3 days) but my
question and it's solution could interest others.

Anyhow I am having great difficulty getting my head around the below code.
Basically I want to display the form and if the username textbox is then
submitted empty, I want the form redisplayed but this time textbox
background color to be red and not yellow.

The code is something I simply knocked up so I could see how I can use
stylesheet settings to highlight boxes (or anything really) which have
failed validation. I am just wondering if I have approached this from
completely the wrong angle? I used to create forms within the page itself
but specifically want to use templates now.

Be grateful if something could have a look and give me some pointers. I know
the code below is incorrect (that's the problem).

Cheers

Phil

------- testpage02.php ---------
<?php
//testpage02.php

$newTextBox = new myTextBox("username", "standardTextBox");
$newSubmitButton = new mySubmitButton("Submit");
include ("testpage02.tpl.php");

if (!empty($_POST['username']))
{

echo "Success";
exit;
}
else
{
$newTextBox->setStyle("errorTextBox");
include ("testpage02.tpl.php");
exit;
}


class myTextBox
{
private $textboxName;
private $style;

function __construct($textboxName, $style)
{
$this->textboxName = $textboxName;
$this->style= $style;
}

function setStyle($style)
{
$this->style= $style;
}

function getTextBox()
{
$line = "<input type=\"text\" ";
$line.= "name=\"";
$line.= $this->textboxName;
$line.= "\" ";
$line.= "class=\"";
$line.= $this->style;
$line.= "\" />";
return $line;
}
}

class mySubmitButton
{
private $value;

function __construct($value)
{
$this->value = $value;
}

function getSubmitButton()
{
return "<input type=\"submit\" value=\"".$this->value."\"/>";
}
}

?>

------- testpage02.tpl.php ---------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<!-- testpage02.tpl.php -->
<title>Test Page 02</title>

<style type="text/css">

..standardTextBox
{
background-color: yellow;
}

..errorTextBox
{
background-color: red;
}
</style>
</head>

<body>
<form action="<?php print $PHP_SELF ?>" method="POST">

<?php echo $newTextBox->getTextBox() ?>
<br />
<?php echo $newSubmitButton->getSubmitButton() ?>

</form>

</body>
</html>

 

Navigation:

[Reply to this 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

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