SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: URLEncode

  1. #1
    SitePoint Member
    Join Date
    Feb 2002
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    URLEncode

    I'm trying to use URLEncode to eliminate spaces when it pulls out a field name from a database that is displayed as a link. The link points to a file. Some of the files have spaces in their names. Here's my code. How can I utilize the urlEncode to take out the spaces from the <%=RS("FileName")%> in the code below?

    <%
    Set MyConn = Server.CreateObject("ADODB.Connection")
    MyConn.Open "FILEDSN=C:\Program Files\Common Files\ODBC\Data Sources\jcaho.dsn"
    %>
    <%
    SQL_query = "(SELECT * FROM JUpdate where ArticleType = 'JCAHO Update') "


    Set RS = MyConn.Execute(SQL_query)

    WHILE NOT RS.EOF
    %>

    <LI> <A HREF=<%=RS("FileName")%>> <%=RS("DateSubmit")%>: <%=RS("ArticleTitle")%> </A>
    <%

    RS.MoveNext
    WEND

    %>
    <%
    RS.Close
    MyConn.Close

    set RS=nothing
    set MyConn=nothing



    %>

    Thanks!

  2. #2
    SitePoint Zealot BlitzCraig's Avatar
    Join Date
    Mar 2001
    Location
    Louisville, KY
    Posts
    114
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    URLEncode Method:
    http://www.devguru.com/Technologies/...urlencode.html

    Alternately, I suppose you could just do this:

    Replace
    Code:
    <A HREF=<%=RS("FileName")%>>
    with
    Code:
    <A HREF="<%=Replace(RS("FileName"), " ", "%20")%>">
    Craig H. Rettig - Bitbucket Heaven
    To understand recursion, you must first understand recursion.

  3. #3
    SitePoint Member
    Join Date
    Feb 2002
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Wink

    It worked. Thanks a bunch!

  4. #4
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would have thought that your problem was not solved by the replacement of " " for "%20", but by putting the link url within quotes. Without them (as you previously had it), a link to "http://test.com/a big html file.htm" would come out as:
    [CODE]
    <A HREF=http://test.com/a big html file.htm>
    [CODE]

    The browser would interpret only the "http://test.com/a" but as the URL, and would interpret the remaining bits as individual valueless attributes of the A tag (just like in <OPTION SELECTED>, where SELECTED is a valueless attribute) which it doesn't understand and thus ignores.

    <sidenote id=1>
    Similar problems occur on OSes which have long filenames, Win9x and NT/2K/XP in particular.
    </sidenote>

    Although browsers do try to read attributes which are not wrapped in quote marks, this illustrates exactly the kind of problem that can arise in these cases. Thus, HTML code should be written by encapsulating every attribute value in quote marks, thereby avoiding this kind of issue. This is compulsory for valid XHTML/XML documents, and is probably true of the official HTML spec too, so you should make sure all your HTML code is conformant in order to avoid similar probelm in the future.

    <sidenote id=1>
    In fact, all valueless attributes (such as the <OPTION> example above) must be revised for XHTML conformance, for example <OPTION SELECTED> becomes <OPTION SELECTED="SELECTED">.
    </sidenote>


    M@rco

  5. #5
    SitePoint Zealot BlitzCraig's Avatar
    Join Date
    Mar 2001
    Location
    Louisville, KY
    Posts
    114
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by M@rco
    I would have thought that your problem was not solved by the replacement of " " for "%20", but by putting the link url within quotes.
    Actually, that line of code I put up there addresses both the %20 and the no quotes issue.

    Better safe than sorry, I say. Which is why my code's always littered with copious quantities of Trim() statements...

    Of course, whenever possible, I avoid spaces in filenames anyway, especially when there exist jokers like myself who used to put non-breaking spaces in filenames to protect them from the average user.

    <geek mode="on">
    Or worse, back in my college days, sticking files named "rm -rf" in someone else's directory on a Unix system. Quite humorous when the person attempting to look at the file typed in "more < rm -rf".
    </geek>
    Craig H. Rettig - Bitbucket Heaven
    To understand recursion, you must first understand recursion.

  6. #6
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I suppose technically all links should be be URL encoded using Server.URLEncode (or escaped manually).


    M@rco


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •