PDA

View Full Version : HTML::Template


tom123
03-06-2006, 06:31 PM
Im using HTML::Template, if the user selects a table from a select box a cgi script retreives the tables fields, then returns to the html page displaying the selected tabel and its fields in another select box. Problem is the following code is displaying the selected table twice, i.e the unless statment dosent seem to be working. Please help, ive been pulling my hair out all weekend trying to fix it.

If user selected table dog then after cgi called the slecect box will display:

dog
dog
cat
mouse
etc..

######################html###################################

<td align="top"><i>Tables:</i></BR>
<select name="table" onchange="reload(this.form)">
<option value="<tmpl_var table_selected>" selected="true"><tmpl_var table_selected></option>
<tmpl_loop name="tables">
<tmpl_unless name="table_selected">
<option value="<tmpl_var table_name>"><tmpl_var table_name></option>
</tmpl_unless>
</tmpl_loop>
</select>
</td>

##################The cgi code#########################

# Create reference to each entry in @tables. Needed by template file data_selection.tmpl, to access data.
while(@tables)
{
my %my_tables;
$my_tables{table_name} = shift @tables;
push(@tables_ref,\%my_tables);
}

$session->param('tables', \@tables_ref);

FishMonger
03-08-2006, 08:38 AM
Where are you setting $session->param(table_selected => ?) and what value are you setting it to?

You're not showing enough of the code to say for sure, but could it be that you need to reverse the logic and put the loop inside the unless block?

tom123
03-08-2006, 03:23 PM
This is where i set the table_selected fishmonger. This cgi is called with the above code when the page reloads. Putting the unless statement outside the loop dosent seem to work.

elsif ($page =~ /data_selection.cgi/)
{

$table_selected = param("table");

# Create session variable for table selected
$session->param('table_selected',$table_selected);
}

FishMonger
03-08-2006, 05:11 PM
What's the value of param("table")? Does it hold the value you should be expecting...either 0 or 1? If you change it to: <tmpl_if name="table_selected"> does it give the results that you expect?

FishMonger
03-08-2006, 06:47 PM
I should have seen this before, but you're using the same name for 2 different template params...that's going to cause a problem.

<tmpl_var table_selected> appears that it should hold the name of the table.

<tmpl_unless name="table_selected"> is a boolean test and should be set to either 1 for true or 0 for false, (undef also equates to false). If it's set to anything else, it should evaluate to true.

Try changing the name of the unless param.

tom123
03-09-2006, 07:00 PM
Might it have something to do with the fact that im reloading the page when a javascript onchange() event occurs? Im not actually submitting the data therefore i dont think im getting any params. Will probaly just send the params in the url.

FishMonger
03-10-2006, 05:55 PM
Yes, it might be related to the javascript, but you haven't shown us that part (and my javascript knowledge is minimal). Why are you having the javascript reload the page? Would't it be better to have the javascript update the form's values rather than retieving the same page from the server?