Can I pass multiple flag values ​​with the same name?

I am using the Twitter boot tray and you need to transfer the list of checkbox sections to my server for processing. The HTML flag is as follows:

<div class="controls"> <label class="checkbox"><input type="checkbox" name="my_match[]" value="190">TEST 190</label> <label class="checkbox"><input type="checkbox" name="my_match[]" value="200">TEST 200</label> <label class="checkbox"><input type="checkbox" name="my_match[]" value="210">TEST 210</label> </div> ... $.post("form.php", $("#form_id").serialize(), function(){ ... }); 

I pass the values ​​of the form as a serialized string using jquery, but the values ​​are sent like this:

 my_match=190&my_match=200 

Can I send them in the following format?

 my_match=190:200 

I'm not sure if I need to change my HTML, or this is what I need to process using javascript. Any thoughts?

+6
source share
3 answers

Something like this would do the trick:

 <div class='controls'> <label class="checkbox"> <input type="checkbox" name="my_match[]" value="190">TEST 190</label> <label class="checkbox"> <input type="checkbox" name="my_match[]" value="200">TEST 200</label> <label class="checkbox"> <input type="checkbox" name="my_match[]" value="210">TEST 210</label> </div> <form> <input id='my_match' type='hidden' name='my_match[]' /> <button>Submit</button> </form> 

 $('form').submit(function() { var arr=[]; $('input:checked[name=my_match[]]').each(function(){ arr.push($(this).val()); }); $('#my_match').val(arr.join(':')); alert($('#my_match').val()); // Prevent actual submit for demo purposes: return false; });​​ 

Try the violin


Edit: this is exactly what Chase described in his answer.

+12
source

I believe that checkboxes with the same name are returned with a comma separated list. What you can do is create a hidden field, add the necessary checkboxes as you want, and then send the hidden field.

+4
source

For anyone who receives from Google:

As far as I know, JavaScript is the only way to make them a delimited string, because it gets complicated if you want a general solution that can deal with all the possible delimiters you want and support escaping the delimiter if it appears in one of the value strings.

If at all possible, I strongly recommend just accepting the format my_match=190&my_match=200 and converting them to the server.

In PHP, this is automatic if you put [] at the end of a name. Other languages ​​like Python will usually have a special getter that returns a list, not a single value. (For example, request.POST.getlist('my_match') in Django)

Based on JavaScript, your page becomes more fragile because a debugging connection, network icon, or a broken firewall at the application level can prevent JavaScript from loading or delay it long enough for the user to try to submit without it.

(And if you turn off the submit button until JavaScript is loaded, you just annoy and / or upset your visitors and give the impression that your site is designed so that everyone else does this fine. Always remember how your actions affect the first impression you make.)

... not to mention that depending on JavaScript, when you don’t need strictly (for example, drop-down menus without backup :hover , forms that frivolously require sending JavaScript, etc.), annoying people like I who use JavaScript for whitelisting, such as NoScript for:

  • Make a web ad and do a survey! interstitial less annoying
  • Reduce the slowness of opening tabs and extensions.
  • Limit the success rate of a 0-day exploit

If you absolutely must use JavaScript in this situation, be sure to use the <noscript> tag to warn people are reloading with JavaScript enabled before filling out the form.

0
source

Source: https://habr.com/ru/post/915545/


All Articles