blob: a3f3e1df61c0711028195a9742d50c05e43e9e6f [file] [log] [blame]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Variable Access</title>
<link rel="stylesheet" href="../css/wondrous/styles.css" type="text/css" />
</head>
<body>
<div id="container">
<div id="header">
<h1><a href="../index.html">Rivet</a></h1>
<h2 id="slogan">Webscripting for Tcl'ers</h2>
<div class="clear"></div>
</div>
<div id="body">
<div id="content">
<h2><headline>Variable Access</headline>
</h2>
<div class="example">
<!-- p class="title"><b>Example 3. Variable Access</b></p-->
<div class="example-contents">
<p>Here, we demonstrate how to access variables set by GET or POST operations.</p>
<p>Given an HTML form like the following:</p>
<pre class="programlisting">
&lt;form action="vars.rvt"&gt;
&lt;table&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;b&gt;Title:&lt;/b&gt;&lt;/td&gt;
&lt;td&gt;&lt;input name="title"&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;b&gt;Salary:&lt;/b&gt;&lt;/td&gt;
&lt;td&gt;&lt;input name="salary"&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;b&gt;Boss:&lt;/b&gt;&lt;/td&gt;
&lt;td&gt;&lt;input name="boss"&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;b&gt;Skills:&lt;/b&gt;&lt;/td&gt;
&lt;td&gt;
&lt;select name="skills" multiple="multiple"&gt;
&lt;option&gt;c&lt;/option&gt;
&lt;option&gt;java&lt;/option&gt;
&lt;option&gt;Tcl&lt;/option&gt;
&lt;option&gt;Perl&lt;/option&gt;
&lt;/select&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;input type="submit"&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/form&gt;
</pre>
<p>We can use this Rivet script to get the variable values:</p>
<pre class="programlisting">&lt;?
set errlist {}
if { [var exists title] } {
set title [var get title]
} else {
set errlist "You need to enter a title"
}
if { [var exists salary] } {
set salary [var get salary]
if { ! [string is digit $salary] } {
lappend errlist "Salary must be a number"
}
} else {
lappend errlist "You need to enter a salary"
}
if { [var exists boss] } {
set boss [var get boss]
} else {
set boss "Mr. Burns"
}
if { [var exists skills] } {
set skills [var list skills]
} else {
lappend errlist "You need to enter some skills"
}
if { [llength $errlist] != 0 } {
foreach err $errlist {
puts "&lt;b&gt; $err &lt;/b&gt;"
}
} else {
puts "Thanks for the information!"
?&gt;
&lt;table&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;b&gt;Title:&lt;/b&gt;&lt;/td&gt;
&lt;td&gt;&lt;?= $title ?&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;b&gt;Boss:&lt;/b&gt;&lt;/td&gt;
&lt;td&gt;&lt;?= $boss ?&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;b&gt;Salary:&lt;/b&gt;&lt;/td&gt;
&lt;td&gt;&lt;?= $salary ?&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;b&gt;Skills:&lt;/b&gt;&lt;/td&gt;
&lt;td&gt;&lt;?= $skills ?&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;?
}
?&gt;
</pre>
<p>The first statement checks to make sure that the boss variable has been passed to the script,
and then does something with that information. If it's not present, an error is added to the list of errors.</p>
<p>In the second block of code, the variable salary is fetched, with one more error check - because it's a
number, it needs to be composed of digits.</p>
<p>The boss variable isn't required to have been sent - we set it to "Mr. Burns" if it isn't among the
information we received.</p>
<p>The last bit of variable handing code is a bit trickier. Because skills is a listbox, and can
potentially have multiple values, we opt to receive them as a list, so that at some point,
we could iterate over them.</p>
<p>The script then checks to make sure that errlist is empty and outputting a thankyou message.
If errlist is not empty, the list of errors it contains is printed.</p>
</div>
</div>
<div class="contentbottom">
<div id="last_modified">Last Modified: 06-11-2011 11:47:35 UTC</div>
</div>
</div>
<div class="sidebar">
<ul>
<li id="rivetexamples">
<h4 class="">Examples</h4>
<ul class="blocklist">
<li class="navitem">
<a href="hello_world.html" class="">Hello world!</a>
</li>
<li class="navitem">
<a href="shaded_table.html" class="">A shaded table</a>
</li>
<li class="navitem">
<a href="var_access.html" class="">Variable Access</a>
</li>
<li class="navitem">
<a href="file_upload.html" class="">File Upload</a>
</li>
<li class="navitem">
<a href="file_download.html" class="">File Download</a>
</li>
<li class="navitem">
<a href="ajax.html" class="">XML and Ajax</a>
</li>
<li class="navitem">
<a href="calendar.html" class="">Calendar</a>
</li>
</ul>
</li>
<li id="main">
<h4 class="">Rivet</h4>
<ul class="blocklist">
<li class="navitem">
<a href="../index.html" class="">Rivet Homepage</a>
</li>
<li class="navitem">
<a href="http://tcl.apache.org/" target="asf">Apache Tcl Home</a>
</li>
<li class="navitem">
<a href="download.html" class="">Getting Rivet</a>
</li>
<li class="navitem">
<a href="hello_world.html" class="">Examples</a>
</li>
<li class="navitem">
<a href="about.html" class="">About Us - Contact</a>
</li>
</ul>
</li>
<li id="manual">
<h4 class="">Manuals</h4>
<ul class="blocklist">
<li class="navitem">
<a href="http://tcl.apache.org/rivet/manual2.0/" target="rivetman2.0">Rivet 2.0 Manual</a>
</li>
<li class="navitem">
<a href="http://tcl.apache.org/rivet/manual2.1/" target="rivetman2.1">Rivet 2.1 Manual</a>
</li>
</ul>
</li>
</ul>
</div>
<div class="clear"></div>
</div>
</div>
<div id="footer">
<div class="footer-content">
<p><a href="http://www.apache.org/">Apache Software Foundation</a> | Design by <a href="http://www.spyka.net">Free CSS Templates</a> | <a href="http://www.justfreetemplates.com">Free Web Templates</a></p>
</div>
</div>
<div style="text-align: center; font-size: 0.75em;">Design downloaded from <a href="http://www.freewebtemplates.com/">free website templates</a>.</div></body>
</html>