Smartlet Example
Here's a largish example of a working smartlet. It's described in more detail here.
Smartsite SXML | Copy Code |
---|---|
<se:switch expression="translation.arg(1,rem='Use "init" to add all the plumbing to your form, "set" to get the value of a control taking the zip code from posted data.')" > <se:case value="init"> {buffer.set('formId',translation.arg(2,rem='Id of the enriched form'))} {buffer.set('zipControl',translation.arg(3,rem='Name of the zip code control'))} {buffer.set('streetControl',translation.arg(4,rem='Name of the street control for mode init, name of the requested value for mode set.'))} {buffer.set('cityControl',translation.arg(5,rem='Name of the street control'))} <!--// Initialize javascript for enrichment --> {placeholder.addjavascriptonload("$get('addrInvulKnop').style.display='none';")} {placeholder.addjavascriptonload("$addHandler(getZip(), 'change', PostWebRequest);")} <se:placeholder.addjavascriptinclude url="/AJAX/Scripts/MicrosoftAjax.js" /> <se:placeholder.addjavascriptinclude url="/AJAX/Scripts/MicrosoftAjaxWebForms.js" /> <se:placeholder.addjavascriptinclude url="/AJAX/Scripts/Toolkit/Common.js" /> <se:placeholder.addjavascriptinclude url="/AJAX/Scripts/Toolkit/BaseScripts.js" /> <se:placeholder.addjavascriptinclude url="{channel.link('JS_ZIPCODE')}" /> <se:placeholder.addjavascript> function getZip(){ return $get("{buffer.get('formId')}").elements["{buffer.get('zipControl')}"]; } function getStreet(){ return $get("{buffer.get('formId')}").elements["{buffer.get('streetControl')}"]; } function getCity(){ return $get("{buffer.get('formId')}").elements["{buffer.get('cityControl')}"]; } // This function performs a POST Web request. function PostWebRequest() { pwr("{channel.link(translation.number())}"); } </se:placeholder.addjavascript> <input type="submit" id="addrInvulKnop" name="updateAddress" value="Adres invullen" /> </se:case> <se:case value="set" whitespace="remove"> {buffer.set('zipControl',translation.arg(3,rem='Name of the zip code control'))} <!--// TODO: Harden against SQL injection --> {buffer.set('requestedValue',translation.arg(4,rem='Name of the street control for mode init, name of the requested value for mode set.'))} <se:sqlquery condition="request.form(buffer.get('zipControl'),default='')!=''" sql="select {buffer.get('requestedValue',whitespace='normalize')} from zipcodes where zipcode='{request.form(buffer.get(zipControl))}'"> <se:parameters> <se:parameter name="rowformat">{this.field(1)}</se:parameter> </se:parameters> </se:sqlquery> </se:case> <se:case whitespace="remove"> <!--// Handle postback --> <se:json /> <se:if expression="page.json.exists('zip')"> <se:then> <se:sqlquery whitespace="preserve"> <se:parameters> <se:parameter name="sql"> select street, city from zipcodes where zipcode=? </se:parameter> <se:parameter name="params">{page.json.getvalue('zip')}</se:parameter> <se:parameter name="format"> <se:rowformat match="first">"city":"{this.field(city)}","street":"{this.field(street)}"</se:rowformat> <se:rowformat></se:rowformat> </se:parameter> <se:parameter name="resultformat">{{this.result()}}</se:parameter> </se:parameters> </se:sqlquery> {response.contenttype('application/json')} </se:then> <se:else> {response.redirect(4216)} </se:else> </se:if> </se:case> </se:switch> |