Dynamic Query Example
Complete the following steps to use a dynamic query with CSP:
-
Using Studio, create a new CSP page named MySamplePage2.CSP in csp/user.
-
Add the following script to the page. The script defines a query that retrieves the PhoneNumberType and Number values for each PhoneNumber whose Contact value matches the input value. Note that the script retrieves the Contact value from %request. This technique is discussed later in the tutorial.
<script language="sql" name="rs" p1="#($Get(%request.Data("ID",1)))#" mode="display"> SELECT PhoneNumberType, Number FROM CSPTutorial.PhoneNumber WHERE Contact = ? </script>
-
Add the following <csp:while> tag to the page after the above script. The tag iterates through the query's result set and displays the query results.
<csp:while condition="rs.Next()"> <p>#(rs.Data("PhoneNumberType"))# #(rs.Data("Number"))#</p> </csp:while>
-
Finally, add the following form to MySamplePage2.CSP. The form contains a single text field and a Submit button. When the user presses the button, the value in the form field is submitted to the page. This becomes the search value used in the dynamic query.
<form> <p>Contact ID: <input type="text" size="5" name="ID"/></p> <input type="SUBMIT" Name="Search" Value="Search"/> </form>
-
Open MySamplePage2.CSP in a Web browser. Enter a valid Contact ID value in the text field.
-
Click the Search button. The page displays the phone numbers for the Contact with ID value 1.