I have a simple web form where I want the form to remember the last entry in case the validation is retrieving an error.
The general process is:
enter data in the form -- press submit -- data validation -- if error, display error message, keep entered data on the form & do not store in DB | if no error -- store in DB.
I am currently using HTML template rendering and would like to keep doing so.
The form is plain HTML and the form part looks like this
<table class="form-table" id="addContact">
<tbody>
<tr>
<td>Eesnimi:</td>
<td><input type="text" id="fname" name="firstName" placeholder="Sinu eesnimi.." value= "{{ session.firstName }}"></td>
</tr>
I have currently stored the values as Session Variables. The session is started from Index.php and the validation and HTML template rendering happens on Index.php. The form is held on Add.html
Index.php
add.html
Session variables as held as follows:
$_SESSION['firstName'] = $firstName;
$_SESSION['lastName'] = $lastName;
$_SESSION['phones'] = $phones;
Currently, the form is just displaying the {{ session.firstName }} and not the session variable value. I tried setting the variable in the HTML template, but this caused a general error.
<?php
namespace {
use tpl\Scope;
function render_template($templatePath, $data = []) {
$node = new DOMDocument();
libxml_use_internal_errors(true);
$node->loadHTMLFile($templatePath);
tpl\traverse($node, new Scope($data));
return $node->saveHTML();
}
}
class Entry {
public $key;
public $value;
public function __construct($name, $key) {
$this->key = $name;
$this->value = $key;
}
public function __toString() {
return $this->key . "->" . $this->value;
}
}
And calling the render_template(template path, data-here pushing $_SESSION)