Project: To create dynamic multi page form that populates the fields in different ways depending on various situations.
Context: Using CMSMS and Smarty tags to insert PHP into the requried pages. Each single page of the form is being built using a single smarty
Current Method: Using smarty tags, I am writing large chunks of HTML echoed in heredoc notation. When I reach a part of the form that needs to be dynamically genearted (such as a drop down menu) I escape the heredoc, write the PHP (using standard echo functions to generate the HTML required for that form element) then return to heredoc for more chunks of HTML.
Suspicion: this is inelegant, messy, tedious and just feels wrong.
Request: Any idea of a better way of doing this?
EDIT: An example of a chunk of my current setup, here you can see an initial chunk of static HTML is echoed – (the start of the form, and the first question) then a drop down menu which requires dynamic generation depending on the data that exists in the SESSION variables. After this, the heredoc resumes and echos more static HTML:
echo <<<EOD
<form id="myform" method="post" action="?page=2">
<div class="myform">
<div class="formfield">
<div class="question"><label for="sv_01">Question?</label> <input type="text" name="sv_01" value="$sv_01" size="10" maxlength="10" /></div>
<div class="subquestion"><label for="sv_02">What Year?</label>
EOD;
echo '<select name="sv_02">';
$vars = array(
'-Year' => 'Year',
'-2012' => '2012',
'-2011' => '2011',
'-2010' => '2010',
'-2009' => '2009',
'-2008' => '2008',
'-2007' => '2007',
'-2006' => '2006',
'-2005' => '2005',
'Pre 2005' => 'Pre 2005',
);
foreach($vars as $val => $name){
if($_SESSION['sv_02'] == $val){
echo '<option value="' . substr($val, 0, 1) . '" selected>' . $name . '</option>';
} else {
echo '<option value="' . substr($val, 0, 1) . '">' . $name . '</option>';
}
}
echo '</select></div>';
echo <<<EOD
<div class="question"><br /> <label for="sv_04">Another question</label> <input type="text" name="sv_04" value="$sv_04" size="10" maxlength="10" />%</div>
<div class="subquestion"><label for="sv_07">When was the data collected?</label>
EOD;
echo '<select name="sv_07">';
$vars = array(
'-Month' => 'Month',
'-January' => 'January',
'-February' => 'February',
'-March' => 'March',
'-April' => 'April',
'-May' => 'May',
'-June' => 'June',
'-July' => 'July',
'-August' => 'August',
'-September' => 'September',
'-October' => 'October',
'-November' => 'November',
'-December' => 'December',
);
foreach($vars as $val => $name){
if($_SESSION['sv_07'] == $val){
echo '<option value="' . substr($val, 0, 1) . '" selected>' . $name . '</option>';
} else {
echo '<option value="' . substr($val, 0, 1) . '">' . $name . '</option>';
}
}
echo '</select><select name="sv_08">';
$vars = array(
'-Year' => 'Year',
'-2012' => '2012',
'-2011' => '2011',
'-2010' => '2010',
'-2009' => '2009',
'-2008' => '2008',
'-2007' => '2007',
'-2006' => '2006',
'-2005' => '2005',
'Pre 2005' => 'Pre 2005',
);
foreach($vars as $val => $name){
if($_SESSION['sv_08'] == $val){
echo '<option value="' . substr($val, 0, 1) . '" selected>' . $name . '</option>';
} else {
echo '<option value="' . substr($val, 0, 1) . '">' . $name . '</option>';
}
}
echo '</select></div>';
echo<<<EOD
<div class="continue"><input type="submit" value="Continue" /></div>
</div>
</div>
</form>
EOD;
As opposed to standard HTML+PHP? This works fine with templating tools too.
If this isn’t what you’re getting at, show some code.