473,404 Members | 2,178 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Problem combining validation and templates

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
3 1231
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Stefan Richter | last post by:
Hi, after coding for days on stupid form validations - Like: strings (min / max length), numbers(min / max value), money(min / max value), postcodes(min / max value), telefon numbers, email...
7
by: Hansan | last post by:
Hi all, I hope you have time to help me out a little. My problem is that I want to combine some python code I have made with some html templates, I found a tutorial at dev shed:...
2
by: Laurent R. Therond | last post by:
Hi, I searched for something like this, but could not locate any information. I may have used bad keywords, or it does not make any sense... Anyhow, there it goes... Let's say you overlay...
1
by: lievemario | last post by:
Hey, I have generated following xml file (using perl,dbi), Now I would like to display this with the help of xsl such that I have the following result: A B (XXXX) (YYYY)
8
by: mikea_59 | last post by:
I am having trouble combining similar elements. Elements can be combined if they have the same name and the same attribute values. I can handle single level elements but am having problems with...
7
by: Microsoft News | last post by:
Hi all. Does id() xpath function work in System.XML? I have built dtd, schema, xdr for a simple xml which includes attributes definded as ID and IDREFS. Validation is occurring properly for all...
18
by: jslowery | last post by:
I am not completely knowledgable about the status of lexical scoping in Python, but it was my understanding that this was added in a long time ago around python2.1-python2.2 I am using python2.4...
2
by: Stefan | last post by:
Hi there! I'm very new to XML and XSLT, so while working i encountered the following, probably very easy to solve, problem. i've got 2 XML-files, which look like this: LinesOfCode.xml:...
5
by: garfy | last post by:
Hi i get this error in validation Line 22 column 6: document type does not allow element "title" here. <title>Seo Web Design Los Angeles - Web Design And Search Engine Optimization L ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.