Reputation: 1114
I came across a specific and interesting problem yesterday. Assume we have the following html code block:
<div id="addresses">
<input type="text" name="address" id="addr1" />
<input type="text" name="address" id="addr2" />
<input type="text" name="address" id="addr3" />
</div>
While posting this form, input elements that have the same name are joined and values are separated with comma character. For example if we have the values like:
addr1 = 'TEST TEST XXX'
addr2 = 'YYY ZZZ 11111'
addr3 = 'ZZZ KKK TEST '
submitted form post data is as follows:
address = 'TEST TEST XXX', 'YYY ZZZ 11111', 'ZZZ KKK TEST '
then i can get each of these values as elements of an array on the server side (3 different elements of array). But if the values are like:
addr1 = 'TEST, TEST XXX'
addr2 = 'YYY ZZZ, 11111'
addr3 = 'ZZZ KKK, TEST '
then submitted form data becomes:
address = 'TEST, TEST XXX', 'YYY ZZZ, 11111', 'ZZZ KKK, TEST '
then the resulting array contains 6 elements which is totally wrong. I believe COMMA character which is contained in the value is not escaped.
Any ideas on this issue. Thanks in advance...
Upvotes: 1
Views: 6816
Reputation: 1614
Use this HTML
<div id="addresses">
<form:input path="address[0]" />
<form:input path="address[1]" />
<form:input path="address[2]" />
</div>
and Spring should populate your input texts with the values of the String[] address
server-side object and vice-versa.
You can take a look here: http://bitbybitblog.com/forms-and-data-models-in-spring-mvc/
Upvotes: 1
Reputation: 6684
You are not using an array.
Change each input like this:
<input type="text" name="address[]" id="addr1" />
and when you will fetch the data you will have an array without any issue about comma or else.
$_POST['adddess']=array(0=>'TEST, TEST XXX',1=>'YYY ZZZ, 11111',2=>'ZZZ KKK, TEST ');
and you can access each item based on the index
Upvotes: 1
Reputation: 1620
Instead of using multiple input fields with one NAME attribute value, use unique values. That can make your code much more robust.
<div id="addresses">
<input type="text" name="addr1" id="addr1" />
<input type="text" name="addr2" id="addr2" />
<input type="text" name="addr3" id="addr3" />
</div>
On the php side:
<?php
$address = array($_REQUEST['addr1'], $_REQUEST['addr2'], $_REQUEST['addr3']);
?>
Upvotes: 0