snakespan
snakespan

Reputation: 1211

Can a Radio button carry multiple values

Is it possible to pass key values pairs in a radio button submit instead of a single value?

Regular Radio field:

<input type="radio" name="foo" value="bar" id="fooBar" required="true" checked>

but maybe something like:

<input type="radio" name="['key1', 'key2', 'key3']" value="['value1, 'value2', 'value3']" id="fooBar" required="true" checked>

Upvotes: 4

Views: 15072

Answers (2)

In Html :

<input type="radio" name="attribut111" id="attr_id" value="2221, 9886">

in javascript: First of all, get the value, then split it by a comma and make it like an arry. Then you can use those easily.

var check_prod_attr = $("input:radio[name=attribut111]:checked").val();
        
var nameArr = check_prod_attr.split(',');
console.log(nameArr);
console.log(nameArr[0]);
console.log(nameArr[1]);

Upvotes: 1

Quentin
Quentin

Reputation: 944054

Not as such.

The value attribute holds a string. When you submit the form, that string will be sent to the server.


You could use a serialisation technique to store a more complex data structure as a string (for example: JSON) and then decode it on the server.

<input type="radio" name="example" value='{ "key1": "value1", "key2": "value2", "key3": "value3" }'>

And then something like:

sub process_form :Local {
    my ($self, $catalyst) = @_;
    my $radio_value = $catalyst->request->parameters->{example};
    my $decoded_radio_value = decode_json $radio_value;
    $c->log->debug("Value of key1 is " . $decoded_radio_value->{key1});
}

You could also store all the data on the server and then put some kind of identifier (which could (for example) be a hash key, or a database row id) in the value attribute.

<input type="radio" name="example" value='44'>

and then something like:

sub process_form :Local {
    my ($self, $catalyst) = @_;
    my $radio_value = $catalyst->request->parameters->{example};
    my $database_row = $catalyst->model("ExampleTable")->find($radio_value);
    $c->log->debug("Value of key1 is " . $database_row->key1);
}

Upvotes: 6

Related Questions