There is a simple multiple select :

 <select class="chosen" multiple="true" name="name[]"> <option value="1">Англия</option> <option value="2">Бельгия</option> <option value="3">Венгрия</option> </select> 

Is it possible in the array name[] to receive not only value's , but also the text inside the <option> ? Looking for values ​​and text in pairs

Looking for a solution in php , either jquery , or within JQuery Chosen

    3 answers 3

    Volley as an array to represent option value='1; Англия' option value='1; Англия' on php parse explode(';',$val)

    • Good decision! :) - Stanislav
    • Used this solution, it was convenient to implement in the project. Thank. - EuGorDD
    • @EuGorDD if helped mark the answer as correct - Naumov
    • @Naumov points out for the first time on the stackerflow simply - EuGorDD
    • @EuGorDD is clear. Welcome! why doesn’t backend know what id are on your page? - Naumov
     $(".chosen option:selected").text(); 

      In general, transferring not only the value, but also the text of the tag itself is not a very good style. Apparently so it is not provided by default.

      To be honest, I like the @Naumov answer better.

      But it is possible and so:

       <?php if (isset($_POST['method']) && $_POST['method']=="ajax") { //print_r($_POST); $postData = []; if (!empty($_POST['postData'])) { foreach ($_POST['postData'] as $item) { $value = $item[1]; if (count($item)>2) $value = [$item[1], $item[2]]; if (!isset($item[0])) { $postData[$item[0]] = $value; } else { if (is_array($postData[$item[0]])) { $postData[$item[0]][] = $value; } else { $tmp = $postData[$item[0]]; $postData[$item[0]] = [$tmp, $value]; } } } } return; } echo ' <form action="api.php" data-form="ajax"> <select name="myselect" multiple="multiple" size="5"> <option value="1">En</option> <option value="2">Be</option> <option value="3">Ve</option> </select> <br><br> <button type="submit">submit</button> </form> <script src="https://code.jquery.com/jquery-2.2.4.min.js" type="text/javascript"></script> <script type="text/javascript"> $(document).ready(function() { $("form[data-form=ajax]").submit(function() { var elements = $(this).find("input[type=text], input[type=checkbox]:selected, input[type=radio]:selected, textarea, select"), postData = []; if (elements.length>0) { for(var i = 0; i < elements.length; i++) { if (elements.eq(i).is("select")) { var elSelected = elements.eq(i).find("option:selected"); if (elSelected.length>0) { for(var j = 0; j < elSelected.length; j++) { postData.push([elements.eq(i).attr("name"), elSelected.eq(j).val(), elSelected.eq(j).text()]); } } } else { postData.push([elements.eq(i).attr("name"), elSelected.eq(j).val()]); } } } $.ajax({ type: "POST", url: $(this).attr("action"), data: ({ postData : postData, method : "ajax" }), success: function(msg){ alert(msg); } }); return false; }); }); </script> ';