... href='script.pl?company=IBM&place=Brussels'. This works nice and fine
No, that was always incorrect. Your browser was letting you get away with mis-encoded data, and now when you actually had text where it makes a difference, you got bit.

Learn to construct your URLs using the proper encoding, and you would not have had to work on the edge cases.

When converting from filenames or data to URLs, URI-encoding should be used. The URI module is good for that.

When including a URL in HTML code, HTML-entity-encoding should be used. The HTML::Entities module is good for that.

If you're not doing both to something that appears in HREF='...', you are putting out broken HTML. Yours was an example of this. Some of the other answers in this thread were good, but some were bad. {sigh} The amount of cargo cult around this problem is amazing.

For gun = Smith & Wesson and drink = Jack Daniels, you'd use code like this:

use URI; use HTML::Entities; my $u = URI->new("/my/cgi"); $u->query_form("gun" => "Smith & Wesson", drink => "Jack Daniels"); print '<A HREF="', encode_entities("$u"), '">', encode_entities("shoot with Smith & Wesson, and drink Jack Daniels") +, '</A>', "\n";
which correctly prints:
<A HREF="/my/cgi?gun=Smith+%26+Wesson&amp;drink=Jack+Daniels">shoot wi +th Smith &amp; Wesson, and drink Jack Daniels</A>

Note that the HREF parameter contains examples of both URI and HTML escaping. This is necessary. This is the proper way. If anyone else tells you different, they've not read the RFCs or consulted the experts. (Such as the code that runs this site, which when I checked a moment ago, was still broken for HTML.)

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.


In reply to •Re: CGI : How to put an "&" into a GET-parameter by merlyn
in thread CGI : How to put an "&" into a GET-parameter by CountZero

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.