Skip to main content
Participating Frequently
January 15, 2010
Answered

URL Query String

  • January 15, 2010
  • 2 replies
  • 1903 views

Alright so I've seen a lot of sites even this site have url's containing something that looks like this

http://google.com/search?=ig=hl blah blah blah

can someone point me in the direction on how to do this properly so I am sending the right data through the query string just something very simple like lets say I wanted to make a page say hello to someone

<form name ="myform" method="post">
<table>
<tr><td>Name:</td><td><input type="text" name="name" /></td></tr>
<tr><td>Age:</td><td><input type="text" name="age" /></td></tr>
<tr><td colspan="2"><input type="submit" name="submit" value="submit" /></td></tr>
</table>
</form>

<cfif isdefined("form.submit")and form.age neq '' and  form.name neq''>
     <cfif form.age gt 35>
          <cfoutput>#form.name# you are #form.age# years Old!!</cfoutput>
     <cfelse>
          <cfoutput>#form.name# you are #form.age# years young</cfoutput>
     </cfif>
</cfif>

lets say instead of haivng the direct page input this data I want it to go to a differnt page so the query link would look something like this

<a href="http://index.cfm/?greet=old"></a>

Or am I not barking up the right tree?

    This topic has been closed for replies.
    Correct answer Dan_Bracuk

    that allowed the data to print to the screen. Just out of curiosity why does the url no longer look like http://localhost/age_name.cfm?name=Andrew&age=30 it now just looks like this localhost/age_name.cfm I dont care but i was just wondering if there was something wrong thank you all for your help


    To see url variables, you either have to bookmark a page that has them, and then call the bookmark, or select a link that includes a query string in the url.

    2 replies

    Inspiring
    January 15, 2010

    I am not sure I follow either. But it seems like you are asking two questions:

    1) How to submit a form to another page (not the same page)?

        You use the form's "action" attribute to specify the target page. So using your example of "index.cfm"

          <form name ="myform" action="index.cfm"  ...>

    2)  How to submit the form field values through the query string?

         The form "method" determines how the values are submitted. When you use the method=GET, the values will be submitted

         through the query string

         <form name ="myform" action="index.cfm" method="GET">

         So using your sample form, if you entered the name "John" and age "25" into the form, those values would appear in the url:

               http://yourservername/index.cfm?name=John&age=25&submit=submit

    You can read more about <form>'s here

    http://www.w3schools.com/html/html_forms.asp

    TivoilosAuthor
    Participating Frequently
    January 15, 2010

    thank you. Now what I am trying to really ask is do you have to write the url query yourself or does it do it on it's own? can you give me some sort of example code where it uses this type of thing?

    Inspiring
    January 15, 2010

    you write it yourself.  something like

    <cflocation url="somewhere?yourquerystring">

    Inspiring
    January 15, 2010

    if you see a url like:  www.somewhere.com?a=1&b=2&c=3

    then everything after the question mark is either your url structure or your cgi.query_string variable.  What you do with it is up to you.

    TivoilosAuthor
    Participating Frequently
    January 15, 2010

    yes but where do i get the data? for the query string??

    Owainnorth
    Inspiring
    January 15, 2010

    Erm, not sure I follow? The query string for what? Are you talking google-specific or something custom for your website?

    If you mean the latter then there's really no answer to that question. In a hyperlink, if you link to, for example:

    /index.cfm?forename=Dave&surname=Smith

    Then once the link has been clicked, your URL scope will have the variables URL.FORENAME and URL.SURNAME. Do with them as you will.