sahand1010
Joined: 25 Apr 2008 Posts: 3
|
Posted: Sat Apr 26, 2008 12:53 am Post subject: QuickForm with Smarty Plugins. Help Please! |
|
|
Hi everyone,
I am not only new to PEAR/Smarty but also to this forum. (I apologize ahead of time for any newbie mistakes I make, and a big thanks to all those that are able to help.)
In order to learn LAMP with PEAR and Smarty I am trying to put together a test website. I have come to the point in the project that requires html forms... after some research I decided to use HTML_QuickForms because of its simplicity.
I would like to use QF to display, validate and submit (to MySQL) a simple form.
The user visits the form page: example.php loads example.tpl which is using Smarty plugins.
At the top of my .tpl page is this code: | Code: | | {load_example assign="example"} |
this code loads the example.tpl functions page titled: function.load_example.php
When these pages are loaded into a browser the QF html is displayed but there is also an error for each of the {$form.xx.html/label} tags at the bottom of the page: | Quote: | ERRNO:8 TEXT: Undefined index: form
LOCATION: /var/www/html/website/templates_c/%%ED^EDF^EDF8BD08%%example.tpl.php, line 98 |
The contents of the page are also displayed twice within the browser. I believe this is due to the .tpl being called first and then loading the function.load_example.php page which in the QF code is set to display the same page: | Code: | // render and display the template
$tpl->display('example.tpl'); |
After a week of experimentation and in turn frustration, I turn to you
Could someone please point me in the right direction and explain to me what it is I am doing wrong and how I can correct this.
I would like to incorporate the QF code into my function.load_example.php page. I am sure I have not done this correctly.
My coding skills are very poor so please be patient.
Thank you in advance!
edit 4/28: I may just need some help with using QuickForm within a site that uses Smarty templates and function plugins. Because, using the plugin means that the .tpl file gets called first. (I hope that makes sense
Here is my template code within example.tpl:
| Code: | {load_example assign="example"}
<div class="box">
<form {$form.attributes}>
{$form.name.label} {$form.name.html}
<br />
{$form.zipcode.label} {$form.zipcode.html}
<br />
{$form.number.label} {$form.number.html}
<br />
{$form.send.html}
<br />
{* display an overview of all form errors *}
{foreach from=$form.errors item=error name=errorloop}
{if $smarty.foreach.errorloop.first}<ul class="warning">{/if}
<li>{$error}</li>
{if $smarty.foreach.errorloop.last}</ul>{/if}
{/foreach}
</form>
</div> |
Here is my php code within function.load_example.php:
| Code: | <?php
function smarty_function_load_example($params, $smarty)
{
$example = new Example();
$example->init();
// assign template variable
$smarty->assign($params['assign'], $example);
}
require_once PEAR_DIR . 'HTML/QuickForm.php';
$form =& new HTML_QuickForm('Form');
// delivery options
$form->addElement('text', 'name', 'Name:');
$form->addElement('text', 'zipcode', 'zipcode:');
$form->addElement('text', 'number', 'number:');
// submit
$form->addElement('submit', 'sended', 'continue');
// Filters: trim() function removes leading and trailing white spaces from data submitted
$form->applyFilter('__ALL__', 'trim');
// validation rules:
$form->addRule('name', 'enter your name', 'required');
$form->addRule('zipcode', 'Enter card zipcode', 'required');
$form->addRule('number', 'Enter your number.', 'required');
if ($form->validate())
{
//______if validation complete post values to database
}
else
{
require_once(SMARTY_DIR . 'Smarty.class.php');
require_once PEAR_DIR . 'HTML/QuickForm/Renderer/ArraySmarty.php';
$tpl =& new Smarty();
$tpl->template_dir = TEMPLATE_DIR;
$tpl->compile_dir = COMPILE_DIR;
$renderer =& new HTML_QuickForm_Renderer_ArraySmarty($tpl);
$renderer->setRequiredTemplate(
'{strip}
{if $error}<img src="../images/warning.png" alt="Warning: {$error|strip_tags|escape}" /> {/if}
{$label}
{/strip}'
);
$renderer->setErrorTemplate('{$html}');
$form->accept($renderer);
$tpl->assign('form', $renderer->toArray());
// render and display the template
$tpl->display('cc_shipping.tpl');
}
// class that supports the example page
class Example
{
// public attributes
public $mShippingType = "";
public $mShippingTypeError = 0;
public $mPickupDate = "";
public $mPickupStore = "";
// private attributes
private $mErrors = 0;
private $mHaveData = 0;
// constructor
function __construct()
{
$this->mBoCustomer = new BoCustomer();
if (isset($_POST['sended']))
$this->mHaveData = 1;
if ($this->mHaveData == 1)
{
// Shipping type cannot be left empty
if (empty($_POST['ShippingType']))
{
$this->mShippingTypeError = 1;
$this->mErrors++;
}
else $this->mShippingType = $_POST['ShippingType'];
// Pickup date and store can initially be left empty
if (isset($_POST['pickupDate']))
$this->mPickupDate = $_POST['pickupDate'];
if (isset($_POST['pickupStore']))
$this->mPickupStore = $_POST['pickupStore'];
}
}
// init method of the example class
public function init()
{
// set member variables for use in the Smarty template
$this->mCustomerData = $this->mBoCustomer->GetCurrentCustomer();
$date = $this->mCcMonth . $this->mCcYear;
$this->validate($this->mCcNumber, $this->mCcCvv, $date);
// if there are no errors send example info to database
if ($this->mErrors == 0)
{
$this->mBoCustomer->AddExample($this->mShippingType, $this->mPickupDate, $this->mPickupStore);
header("location:nextPage.php");
exit;
}
}
// main validation function
public function validate($date){
// some validation function
}//end class
?> |
Thank you!! |
|