Older Newer
Tue, 18 Jun 2013 22:30:15 . . . . Tim Fountain [Removed spam]


Changes by last author:

Added:
FillInFormValues will fill <input>, <textarea> and <select>/<option> with Smarty template variables and values from $_REQUEST.

For example, you might put something like this in your template:

<code>

<form action="{$smarty.server.PHP_SELF|htmlspecialchars}" method="POST">

{fillInFormValues}

<ul class="error"><! Validation errors will go here ></ul>

<input type="text" name="first_name"><br/>

<input type="text" name="last_name"><br/>

<select name="state">

<option value="CA">California

<option value="MA">Massachusetts

</select><br/>

<input type="submit" value="Submit">

{/fillInFormValues}

</form>

</code>

... then do something like this in your PHP code:

<code>

$smarty->register_block("fillInFormValues", "smartyFillInFormValues", false);

$smarty->assign("first_name", "Bob");

$smarty->assign("state", "CA");

$_REQUEST['formErrors'] = array();

if (!empty($_POST)) {

// validate form input... Assuming validateForm() returns

// an array of ($formField => "Error message"):

$_REQUEST['formErrors'] = validateForm($_POST);

}

// Display the form if first time or if there were validation errors:

if (empty($_POST) or !empty($_REQUEST['formErrors'])) {

$smarty->display("formTemplate.tpl");

}

else {

// process form here...

}

</code>