Posts Tagged HTML

NaggieCreating Complex, Secure Web Forms with PHP and HTML_QuickForm2

Saturday, May 22nd, 2010

For PHP developers, HTML_QuickForm2 PEAR package provides a programmatic interface for rigorously defining form controls, value requirements, and user notifications. Using HTML_QuickForm2 helps these developers create usable and secure Web forms without sacrificing visual appeal. This solution takes much of the guesswork out of secure forms development, allowing you to create robust forms with minimal time investment.

It will show you how to take advantage of HTML_QuickForm2 to streamline the creation and validation of complex HTML forms.

Installing HTML_QuickForm2

HTML_QuickForm 2 is a PEAR package, meaning you can install it using the PEAR package installer. Presuming you’re using the original installer, execute the following command to install HTML_QuickForm2:

%>pear install –onlyreqdeps HTML_QuickForm2-alpha

When installed, you can begin programmatically creating your forms.

Creating a Form with HTML_QuickForm2

HTML_QuickForm2 is the second incarnation of the aptly-named HTML_QuickForm, rewritten from the ground up to take advantage of the object-oriented (OO) features in PHP 5. Therefore, if you’re not familiar with the OO development approach, it will take some time to get acquainted with the syntax because forms are created using a rigorous class structure.

Let’s see these examples that hopefully will help elucidate how you can use HTML_QuickForm2 is to create increasingly complex forms.

Figure-1:
html quickform2 fig1 Creating Complex, Secure Web Forms with PHP and HTML QuickForm2
Figure 1. Creating a Simple Form with HTML_QuickForm2

The form in Figure 1 can be created using approximately 25 lines of code, as shown below.

<?php
 require_once "HTML/QuickForm2.php";
 require_once 'HTML/QuickForm2/Renderer.php';
 $format = array(
 ''     => 'Newsletter Format:',
 'text' => 'Text',
 'html' => 'HTML'
 );
 $form = new HTML_QuickForm2('newsletter');
 $name = $form->addText('name')->setLabel('Your Name:');
 $email = $form->addText('email')->setLabel('Your E-mail Address:');
 $newsletter = $form->addSelect('format', null, array('options' => $format));
 $newsletter->setLabel('Preferred Newsletter Format:');
 $form->addElement('submit', null, 'Submit!');
 $renderer = HTML_QuickForm2_Renderer::factory('default');
 echo $form->render($renderer);
?>

HTML_QuickForm2 offers a series of methods, which are responsible for creating form controls such as text fields (addText())and select boxes (addSelect()). Each of these controls is accompanied by control labels that can be added using the setLabel() method. Finally, HTML_QuickForm2 gives you the flexibility to render forms using a wide variety of approaches, including using the Smarty templating engine and an HTML_QuickForm2_Renderer object.

You can just use the default renderer as demonstrated here, passing that object to the render() method in order to output the form. Next, let’s consider the slightly more complex example depicted in Figure 2.

html quickform2 fig2 Creating Complex, Secure Web Forms with PHP and HTML QuickForm2

Figure 2. Creating a More Complex Form Variation

This time, the fieldset tag has been used to create a slightly more organized form structure. Because the form elements appear inside the fieldset boundary, you need to create these elements using the methods exposed through the fieldset object! Neglecting to do so will cause form elements to be rendered outside of the boundary. The following listing creates the form presented in Figure 2.

<?php
require_once “HTML/QuickForm2.php”;
require_once ‘HTML/QuickForm2/Renderer.php’;
$format = array(
” => ‘Newsletter Format:’,
‘text’ => ‘Text’,
‘html’ => ‘HTML’
);
$form = new HTML_QuickForm2(’newsletter’);
$fieldSet = $form->addFieldset()->setLabel(’Subscribe to the Newsletter!’);
$name = $fieldSet->addText(’name’)->setLabel(’Your Name:’);
$email = $fieldSet->addText(’email’)->setLabel(’Your E-mail Address:’);
$newsletter = $fieldSet->addSelect(’format’, null, array(’options’ => $format));
$newsletter->setLabel(’Preferred Newsletter Format:’);

$fieldSet->addElement(’submit’, null, ‘Submit!’);
$renderer = HTML_QuickForm2_Renderer::factory(’default’);
echo $form->render($renderer);
?>

Try experimenting with creating form controls using the HTML_QuickForm2 object instead of the FieldSet object in order to observe the effects of control location placement.

Validating the Form

The previous two examples demonstrated how easy it is to create and render forms using HTML_QuickForm2, but they did nothing to inspect and validate user input — not to mention informing users when the form wasn’t successfully completed. You can configure control-specific validation requirements using theaddRule() method, as shown here: $name = $form->addText(’name’)->setLabel(’Your Name:’); $name->addRule(’required’, ‘Please provide your name.’); After having added similar requirements to the other form controls, HTML_QuickForm2 will automatically adjust the rendered form to inform the user of the required fields, as depicted in Figure 3.

html quickform2 fig3 Creating Complex, Secure Web Forms with PHP and HTML QuickForm2

Figure 3. Adding Field Requirements

The required rule is just one of several supported by HTML_QuickForm2. Among others, you can control input lengths, compare the input with some predefined value, and even create your own custom rules using a callback. Consult the HTML_QuickForm2 documentation for the complete details. Even with the rules added, HTML_QuickForm2 will not actually validate the input until you explicitly call the HTML_QuickForm2 object’svalidate() method. Therefore, you’ll need to add the following code somewhere before the form is rendered:

if ($form->validate()) {
 echo "<p>SUCCESS!</p>";
}

Of course, in a real-world situation you’ll want to carry out more than just a simple notification; you likely will add the subscriber’s information to a database. However, if the user’s input does not meet the validation requirements, HTML_QuickForm2 will notify the user of the problem, as depicted in Figure 4.

html quickform2 fig4 Creating Complex, Secure Web Forms with PHP and HTML QuickForm2

Figure 4. Displaying Error Messages

You can easily add CSS to the form, thereby making the error messages quite evident.

HTML_QuickForm2 provides developers with the means for rigorously creating and validating forms in a manner that greatly reduces the likelihood of invalid or harmful input while simultaneously reducing the amount of time and effort needed to develop complex form layouts.

http://get-a-designer.com

http://www.all1sourcetech.com

Tags: , , ,
Posted in Purely Technical | No Comments »

NaggieConverting HTML to PDF Using Java and Qt

Thursday, March 4th, 2010

Here is the code:

import com.trolltech.qt.core.*;
import com.trolltech.qt.gui.*;
import com.trolltech.qt.webkit.*;
class html2pdf extends QWebView
{
private QPrinter printer = new QPrinter();
public html2pdf()
{
loadFinished.connect(this, \”loadDone()\”);
setHtml(\”This is <b>HTML</b>\”);
// Or use load() to convert html page from url to pdf
}
public void loadDone()
{
printer.setPageSize(QPrinter.PageSize.A4);
printer.setOutputFormat(QPrinter.OutputFormat.PdfFormat);
printer.setOutputFileName(\”test.pdf\”);
print(printer);
System.out.println(\”Done\”);
QApplication.exit();
}
public static void main(String args[])
{
QApplication.initialize(args);
html2pdf h2p = new html2pdf();
h2p.show();
QApplication.exec();
}
}

import com.trolltech.qt.core.*;
import com.trolltech.qt.gui.*;
import com.trolltech.qt.webkit.*;
class html2pdf extends QWebView
{
private QPrinter printer = new QPrinter();
public html2pdf()
{
loadFinished.connect(this, \”loadDone()\”);
setHtml(\”This is <b>HTML</b>\”);
// Or use load() to convert html page from url to pdf
}
public void loadDone()
{
printer.setPageSize(QPrinter.PageSize.A4);
printer.setOutputFormat(QPrinter.OutputFormat.PdfFormat);
printer.setOutputFileName(\”test.pdf\”);
print(printer);
System.out.println(\”Done\”);
QApplication.exit();
}
public static void main(String args[])
{
QApplication.initialize(args);
html2pdf h2p = new html2pdf();
h2p.show();
QApplication.exec();
}
}

http://www.all1social.com

http://www.all1martpro.com

Tags: , ,
Posted in Editorial, Purely Technical | No Comments »

NaggieHTML tool: eLyXer 0.4.1

Saturday, February 27th, 2010

eLyXer thumb  HTML tool: eLyXer 0.4.1

eLyXer 0.4.1 is a LyX to HTML converter, with a focus on flexibility and elegant output. LyX is a wonderful text editor which produces beautiful PDF files. Internally it exports documents to LaTeX, and from there to PDF. It can convert documents generated with LyX versions from 1.5.5 to 1.6.2 into valid HTML pages.

The output requires XHTML, CSS2 and Unicode; therefore a CSS2-compatible browser is required.

Minimum browser versions for some popular programs are: Microsoft Internet Explorer 7, Mozilla Firefox 3, Safari 3 and Chrome 1. It requires Python 2.3.4 or higher

It consists of –

  • Select the translation based on document language.
  • Added em-dash — such as in this sentence, ■, \textup.
  • Added option –converter inkscape to use Inkscape as SVG converter.
  • Solved bug when numbering unordered unique parts such as Part* (thanks, Geremy!).
  • Show error instead of crashing when included document does not exist.
  • Support for all box styles. In CSS: switched to outline-style instead of border for boxes.
  • Support for vertical space insets.
  • Support for references inside paragraphs and formatted references.
  • Listings are now converted using <pre> tags, instead of <code>. They are also justified left.
  • Solved bug that prevented numbered listings to appear numbered (thanks, Sam!).
  • Support for generic Flex insets, including Flex CharStyle: MenuItem.
  • All entities are now generated as the Unicode U+00A0 character.
  • New option –iso885915 to generate a document with ISO-8859-15 encoding.
  • Support for Sam Liddicott’s Newfangle module for literate programming.
  • Updated the developer guide for potential contributors; added link from the main page.
  • Support for \underbrace and \overbrace.


http://get-a-designer.com

http://www.all1sourcetech.com

Tags: , , , , , , , ,
Posted in Editorial, Purely Technical | No Comments »

SuzanneHTML Tools: Haml 2.3.0

Friday, February 26th, 2010

Haml 2.3.0 is a markup language, which is used to describe the HTML of any web document easily and simply, without the use of inline code.

It is compiled into XHTML, similarly to ERB, and attempts to fix many flaws in templating engines like explicitly coding HTML into the template.

Haml itself is a description of the HTML, with code to generate dynamic content. And it functions as a replacement for inline page templating systems such as PHP, ASP, and ERB.

Example:
The #foo Hello World! line of code will output this HTML line of code: <div id=”foo”>Helo World!</div>

Automatically, Haml handled the end tag and tag defining. For CSS stylings, SASS does the same thing as Haml does for HTML. SASS is bundled with Haml by default.

Platform: Windows/ Linux/ Mac OS/ BSD/ Solaris
Language: Ruby

http://www.all1martpro.com

http://www.all1sourcetech.com

Tags: , , , , , ,
Posted in Purely Technical | No Comments »