By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,426 Members | 3,368 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,426 IT Pros & Developers. It's quick & easy.

Problem combining validation and templates

P: n/a
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>
May 17 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Got it working. Damn obvious to use isSet, can't think why I did not think
of it before. Not exactly happy with the code but at least I can now think
how to do this more efficiently since it works.

Cheers

Phil
<?php
//testpage02.php

$newTextBox = new myTextBox("username", "standardTextBox");
$newSubmitButton = new mySubmitButton("Submit");
if (!isSet($_POST['username']))
{
include ("testpage02.tpl.php");
exit;
}

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."\"/>";
}
}

?>
May 17 '07 #2

P: n/a
Phil Latio wrote:
function getTextBox()
{
$line = "<input type=\"text\" ";
$line.= "name=\"";
$line.= $this->textboxName;
$line.= "\" ";
$line.= "class=\"";
$line.= $this->style;
$line.= "\" />";
return $line;
}
What's all this mess all over the place?!

function getTextBox ()
{
return sprintf('<input type="text" name="%s" class="%s" />',
htmlentities($this->textboxName),
htmlentities($this->style)
);
}

Go and tidy your room Phil!

--
Toby A Inkster BSc (Hons) ARCS
http://tobyinkster.co.uk/
Geek of ~ HTML/SQL/Perl/PHP/Python/Apache/Linux
May 18 '07 #3

P: n/a
What's all this mess all over the place?!
>
function getTextBox ()
{
return sprintf('<input type="text" name="%s" class="%s" />',
htmlentities($this->textboxName),
htmlentities($this->style)
);
}

Go and tidy your room Phil!
Hahaha... thanks Toby :-))

Actually I have recently seen %s but didn't understand it's useage. It was
used in QCodo framework

As for htmlentitities, that's something I've also seen but never really
investigated it's use. I shall wander over to the PHP manual and take a
look. I agree your solution is a much tidier than mine.

Cheers

Phil
May 18 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.