So am using Perl to dynamically generate a form with radio buttons. And no, this is not a Perl question. Essentially the form allows the user to specificy which links to use for Line 1, 2, 3, etc. And this can be a long form. The problem I have is that in placing the links in the VALUE field, each link shows up on each radio button. Makes it very ugly.

Is there a way to label each radio button with just text, but have it's value be the link? I tried the code below, and am still searching. But when you're new to a certain language, even Googling doesn't just give up answers.

Thanks

<tr>
        <td>
            <label for "radio1"><input type="radio" id="radio1" name="line01" value=$primary_links[$y]>Line 1</label><br>
            <label for "radio2"><input type="radio" id="radio2" name="line02" value=$primary_links[$y]>Line 2</label><br>
            <label for "radio3"><input type="radio" id="radio3" name="line03" value=$primary_links[$y]>Line 3</label><br>
            <label for "radio4"><input type="radio" id="radio3" name="line04" value=$primary_links[$y]>Line 4</label><br>
            <label for "radio5"><input type="radio" id="radio4" name="line05" value=$primary_links[$y]>Line 5</label><br>
        </td>
        <td>
            $primary_links[$y]<p>
        </td>
    </tr>

Dani AI

Generated

Using a URL as a radio value is fine — browsers do not render the value text next to the input. If you see the link text in the page, one of these is happening: the generated HTML is malformed (unquoted or un-escaped attribute values), the URL is being printed elsewhere, or id/for wiring is wrong so labels don't attach to the inputs. was on the right track about HTML syntax; already noticed the long URLs — the fix is to emit valid, escaped HTML and keep label text separate from the value.

Common fixes and a minimal pattern to follow:

  • Always quote attribute values and escape special characters in URLs (&, <, ").
  • Give each input a unique id and match that id from the label's for. Alternatively wrap the input inside the label, but be consistent.
  • Use the same name for radios that belong to the same choice group; use different names for separate groups (one per Line).
  • If you want a visible preview of the URL, render it as an anchor next to the label instead of trying to show the value.

Example (HTML snippet):

<input id="opt-1" type="radio" name="line1" value="https://example.com/path?x=1&amp;y=2" />
<label for="opt-1">Line 1</label>
<a href="https://example.com/path?x=1&amp;y=2" target="_blank" rel="noopener">preview</a>

If you generate HTML from Perl, HTML-escape the URL before inserting it into attributes. A common approach is to use HTML::Entities:

use HTML::Entities qw(encode_entities);

for my $i (0..$#links) {
  my $id  = "opt$i";
  my $url = encode_entities($links[$i], q{&"<>});
  print qq{<input id="$id" type="radio" name="line1" value="$url" />
<label for="$id">Line @{[$i+1]}</label>
<a href="$url">preview</a>\n};
}

Further reading on label semantics and HTML escaping can be found on MDN: label element and the Perl module docs for HTML::Entities: HTML::Entities on MetaCPAN.

Recommended Answers

All 4 Replies

Well a label tag works in this way

<label for="test">Test:</label>
<input type="radio" name="test" />

You place your label's target outside of the label tags

It seems to be because the VALUE is a full reference that this is happening.

So am guessing I need to consolidate all my links into a master array and make the value of each radio the element number of that array.

Ugh

Make sure you're surrounding your values with quotes or it could create malformed HTML

No, seriously, the values themselves are full URL links, and that's why the value shows up....so doing it another way.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.