0

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.

user2938543
  • 301
  • 1
  • 5
  • 12
  • Take a look at: http://stackoverflow.com/questions/7711466/checking-if-form-has-been-submitted-php – Bluedayz Aug 28 '14 at 12:03
  • Also, it's a bad practice using global variables http://stackoverflow.com/questions/1557787/are-global-variables-in-php-considered-bad-practice-if-so-why – Bluedayz Aug 28 '14 at 12:05
  • ^The form does get submitted successfully. The problem is that the $smarty->display() function causes the entire template the display a second time, while I want only the part in the {if} statement to appear. – user2938543 Aug 28 '14 at 13:22
  • Then why do you use the $smarty->display() function? – Bluedayz Aug 28 '14 at 13:35
  • I am not sure how else to update only the part of the template within the {if} statement. – user2938543 Aug 28 '14 at 13:42

0 Answers0