You are here: C97net • 5.10 User Preferences
Save & load form values
This feature can save & load form field values.
function save_form ($form_id, $method = 'post')
Save form field values.
$form_id = unique ID.
$method = form method, either GET or POST (default).
function load_form ($form_id)
$form_id = unique ID.
function reset_form ()
Remove saved form. This function automatically called in flush_tpl ().
Notes:
Example 1 test.php
<?php
require './includes/user_init.php';
$cmd = post_param ('cmd');
switch ($cmd)
{
case 'process':
$name = post_param ('name');
$address = post_param ('address');
$details = post_param ('details');
// save the form before proceed
save_form ('myform');
// all fields must be filled
if (empty ($name) || empty ($address) || empty ($details))
msg_die ('echo', 'Please fill all fields');
// reset the form
// usually called in flush_tpl()
reset_form ();
$output = "Name $name<br />Address $address<br />Details $details";
msg_die ('echo', $output);
break;
default:
// load saved form if available
$row = load_form ('myform');
// if form not saved, initialize the values
if (empty ($row)) $row['name'] = $row['address'] = $row['details'] = '';
$tpl = load_tpl ('test.tpl');
$txt['main_body'] = quick_tpl ($tpl, $row);
flush_tpl ();
break;
}
?>
test.tpl
<h1>Welcome to test page!</h1>
<form name="myform" action="test.php" method="post">
<input type="hidden" name="cmd" value="process" />
<p>Your name: <input type="text" name="name" value="{$name}"/></p>
<p>Address: <input type="text" name="address" value="{$address}" /></p>
<p>Details: <textarea name="details">{$details}</textarea></p>
<p><input type="submit" /></p>
</form>
There is no comment. Why not be the first?