Variable Access
Here, we demonstrate how to access variables set by GET or POST operations.
Given an HTML form like the following:
<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>
We can use this Rivet script to get the variable values:
<? 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> <? } ?>
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.
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.
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.
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.
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.