I have been experimenting with the Smarty template engine. I have been able to set up Smarty and assign variables, but I have a question concerning updating a template following a form sumbit. My current template (index.tpl) is as follows:
<html>
<head>
<link rel="stylesheet" type="text/css" href="Styles.css">
<title>Smarty</title>
</head>
<body>
<div class="body">
<form action='index.php' method='post'>
<br>
<center>Hello, please enter your name: <input type=text name=name class=nameTextField></center>
<br>
<center><input type=submit value="Submit Name" name=submitName class=nameButton></center>
</form>
<center>
<div class="nameDisplay">
{if isset($name)}
<h1>Hello, {$name}</h1>
{/if}
</div>
</center>
</div>
</body>
</html>
The PHP file (index.php) runs the following code during a form submit:
<?php
if(isset($_POST['submitName'])){
global $smarty; // previously declared
$name = $_POST['name'];
$smarty->assign('name', $name);
$smarty->display('index.tpl');
}
?>
The idea is that the "Hello" message in the template only appears after the form is submitted, since before that, the $name Smarty variable is not set.
However, what actually happens when $smarty->display('index.tpl') is run is that on top of the "Hello" message, the top half of the form is printed as well, so now there are two forms on the same page.
Are there any ideas how this can be fixed? Thank you.