| <!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="../templates/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>Variable Access</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"><form action="vars.rvt"> |
| <table> |
| <tbody> |
| <tr> |
| <td><b>Title:</b></td> |
| <td><input name="title"></td> |
| </tr> |
| <tr> |
| <td><b>Salary:</b></td> |
| <td><input name="salary"></td> |
| </tr> |
| <tr> |
| <td><b>Boss:</b></td> |
| <td><input name="boss"></td></tr> |
| <tr> |
| <td><b>Skills:</b></td> |
| <td> |
| <select name="skills" multiple="multiple"> |
| <option>c</option> |
| <option>java</option> |
| <option>Tcl</option> |
| <option>Perl</option> |
| </select> |
| </td> |
| </tr> |
| <tr> |
| <td><input type="submit"></td> |
| </tr> |
| </tbody> |
| </table> |
| </form></pre> |
| <p>We can use this Rivet script to get the variable values:</p> |
| <pre class="programlisting"><? |
| set errlist {} |
| if { [::rivet::var exists title] } { |
| set title [::rivet::var get title] |
| } else { |
| set errlist "You need to enter a title" |
| } |
| |
| if { [::rivet::var exists salary] } { |
| set salary [::rivet::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 { [::rivet::var exists boss] } { |
| set boss [::rivet::var get boss] |
| } else { |
| set boss "Mr. Burns" |
| } |
| |
| if { [::rivet::var exists skills] } { |
| set skills [::rivet::var list skills] |
| } else { |
| lappend errlist "You need to enter some skills" |
| } |
| |
| if { [llength $errlist] != 0 } { |
| foreach err $errlist { |
| puts "<b> $err </b>" |
| } |
| } else { |
| puts "Thanks for the information!" |
| ?> |
| <table> |
| <tbody> |
| <tr> |
| <td><b>Title:</b></td> |
| <td><?= $title ?></td> |
| </tr> |
| <tr> |
| <td><b>Boss:</b></td> |
| <td><?= $boss ?></td> |
| </tr> |
| <tr> |
| <td><b>Salary:</b></td> |
| <td><?= $salary ?></td> |
| </tr> |
| <tr> |
| <td><b>Skills:</b></td> |
| <td><?= $skills ?></td> |
| </tr> |
| </tbody> |
| </table> |
| <? |
| } |
| ?></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> |
| </div> |
| |
| <div class="sidebar"> |
| <ul> |
| |
| </ul> |
| <ul> |
| <li id="home"> |
| <h4>Rivet</h4> |
| <ul class="blocklist"> |
| <li class="navitem"> |
| <a title="A home for Rivet" href="../index.html">Rivet Homepage</a> |
| </li> |
| <li class="navitem"> |
| <a title="Home of Apache Tcl related stuff" target="asf" href="http://tcl.apache.org/">Apache Tcl Home</a> |
| </li> |
| <li class="navitem"> |
| <a title="Getting Rivet" href="download.html">Getting Rivet</a> |
| </li> |
| <li class="navitem"> |
| <a title="Hello World!" href="hello%5fworld.html">Examples</a> |
| </li> |
| <li class="navitem"> |
| <a title="The Rivet development team" href="about.html">About Us - Contact</a> |
| </li> |
| </ul> |
| </li> |
| <li id="manual"> |
| <h4>Rivet Documentation</h4> |
| <ul class="blocklist"> |
| <li class="navitem"> |
| <a title="Rivet 2.1 Manual" target="rivetman2.1" href="http://tcl.apache.org/rivet/manual2.1/">Rivet 2.1</a> |
| </li> |
| <li class="navitem"> |
| <a title="Rivet 2.2 Manual" target="rivetman2.2" href="http://tcl.apache.org/rivet/manual2.2/">Rivet 2.2</a> |
| </li> |
| <li class="navitem"> |
| <a title="Rivet 2.3 Manual" target="rivetman2.3" href="http://tcl.apache.org/rivet/manual2.3/">Rivet 2.3</a> |
| </li> |
| <li class="navitem"> |
| <a title="Rivet 3.0 Manual" target="rivetman3.0" href="http://tcl.apache.org/rivet/manual3.0/">Rivet 3.0</a> |
| </li> |
| </ul> |
| </li> |
| <li id="rivetexamples"> |
| <h4>Examples</h4> |
| <ul class="blocklist"> |
| <li class="navitem"> |
| <a href="hello%5fworld.html">Hello world!</a> |
| </li> |
| <li class="navitem"> |
| <a href="colorful%5ftable.html">A colorful table</a> |
| </li> |
| <li class="navitem"> |
| <a href="var%5faccess.html">Variable Access</a> |
| </li> |
| <li class="navitem"> |
| <a href="file%5fupload.html">File Upload</a> |
| </li> |
| <li class="navitem"> |
| <a href="file%5fdownload.html">File Download</a> |
| </li> |
| <li class="navitem"> |
| <a href="ajax.html">XML and Ajax</a> |
| </li> |
| <li class="navitem"> |
| <a href="calendar.html">Calendar</a> |
| </li> |
| </ul> |
| </li> |
| |
| </ul> |
| <ul> |
| |
| </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> |
| |