Skip to main content
Inspiring
January 19, 2009
Question

auto select with select box

  • January 19, 2009
  • 6 replies
  • 1257 views
Hi

I have a site which has 8 services.each service has it own page. When the user goes to the service page there will be a link which will take them to a form. I want the form to be able to identify which service is there and select that one automatically.

I have done this previously but only with a form. Can you please help me to fix this.

Link on service page <a href="moreinfo.cfm?service=WorkGroup Consultancy">Click here for Further Information</a>

select box in form <select name="service">

<option value="UK Immigration" <cfif structKeyExists(form,"UK Immigration") AND form.service eq "WorkGroup Consultancy">selected="selected"</cfif>>UK Immigration</option>
<option <cfif structKeyExists(form,"WorkGroup Consultancy") AND form.service eq "WorkGroup Consultancy">selected="selected"</cfif>value="WorkGroup Consultancy">WorkGroup Consultancy</option>
<option value="Legal Managment Software" <cfif structKeyExists(form,"Legal Managment Software") AND form.service eq "Legal Managment Software">selected="selected"</cfif>>Legal Managment Software</option>
<option value="Search Engine Optimization" <cfif structKeyExists(form,"Search Engine Optimization") AND form.service eq "Search Engine Optimization">selected="selected"</cfif>>Search Engine Optimization</option>
<option value="Marketing Consultancy" <cfif structKeyExists(form,"Marketing Consultancy") AND form.service eq "Marketing Consultancy">selected="selected"</cfif>>Marketing Consultancy</option>
<option value="Project Management" <cfif structKeyExists(form,"Project Management") AND form.service eq "Project Management">selected="selected"</cfif>>Project Management</option>
<option value="Training" <cfif structKeyExists(form,"Training") AND form.service eq "Training">selected="selected"</cfif>>Training</option>
<option value="Web Design"<cfif structKeyExists(form,"Web Design") AND form.service eq "Web Design">selected="selected"</cfif>>Web Design</option>


</select>
    This topic has been closed for replies.

    6 replies

    Inspiring
    January 21, 2009
    Thanks that works great just changed selected="two" to selected="'#service#" and it works perfect
    Inspiring
    January 21, 2009
    I never code it the way you do, so I tried it. This won't work:
    <cfform action="abc.cfm" method="post">
    <cfselect name="abc" selected="two">

    <option value="one">one</option>
    <option value="two">two</option>
    </cfselect>
    </cfform>

    This will
    <cfscript>
    MyQuery = QueryNew("field1", "varchar");
    x = QueryAddRow(MyQuery, 2);
    x = QuerySetCell(MyQuery, "field1","one", 1);
    x = QuerySetCell(MyQuery, "field1","two", 2);
    </cfscript>
    <cfform action="abc.cfm" method="post">
    <cfselect name="abc" query="MyQuery" value="field1" display="field1" selected="two"></cfselect>
    </cfform>
    Inspiring
    January 21, 2009
    With those url variables, your structkeyexists functions will return false 100% of the time.

    For the selected="#url.service#" method, you are probably experiencing difficulty because of the spaces. Try it with Training. That's only 1 word.
    Inspiring
    January 21, 2009
    I thought the spaces may have caused a problem but even when i take them out i am still getting the same problem. Any other suggestions
    Inspiring
    January 21, 2009
    Yeah. I have the following code on each services

    <a href="moreinfo.cfm?service=Search Engine Optimization">Click here for Further Information</a>
    <a href="moreinfo.cfm?service=Training">Click here for Further Information</a>
    <a href="moreinfo.cfm?service=Project Management">Click here for Further Information</a>
    Inspiring
    January 21, 2009
    What was the value of url.service when the cfselect method did not work?

    In your second method, are you sure that you have url variables named UK Immigration, WorkGroup Consultancy, etc?
    Inspiring
    January 19, 2009
    <cfselect selected="#url.service#">
    Inspiring
    January 19, 2009
    Your original code looked pretty close to working. The one thing you had wrong is the variable scope - when you pass a parameter via a URL variable (e.g. moreinfo.cfm?service=WorkGroup Consultancy) the resulting variable on the action page of the form will be in the URL scope, not the FORM scope. If you were to pass the variable as a hidden form field instead of a URL query parameter your code would work (from first glance, that is).