Get Multiple Values with comma separated Using PHP and JavaScript

724

Hello I am new in PHP and JavaScript. I have a code of Dropdown Checkbox. I want to try get out values of checked options with comma separate like1,2,3

My problem is that when i run my code my output have one extra comma at the end like1,2,3, and my desired output is1,2,3

Here is my code

HTML Part

<select id="agency" multiple="multiple">
<?php
if (is_array($rating_agencies) && !empty($rating_agencies)) {
    foreach ($rating_agencies as $rating_agencie) {
        echo '<option value="'.$rating_agencie->ID.'"';
        echo '>';
        echo $rating_agencie->name;
        echo '</option>';
    } 
}           
?>
</select>
<input type="button" id="btnSelected" value="Get Selected" />

Java Script

<script type="text/javascript">
    $(function () {
        $('#agency').multiselect({
            includeSelectAllOption: true
        });
        $('#btnSelected').click(function () {
            var selected = $("#agency option:selected");
            var message = "";
            selected.each(function () {
                message += $(this).val() + ",";
            });
            alert(message);
        });
    });
</script>
833

Answer

Solution:

Use slice to remove the last comma.

$('#btnSelected').click(function () {
    var selected = $("#agency option:selected");
    var message = "";
    selected.each(function () {
        message += $(this).val() + ",";
    });
    message = message.slice(0, -1);
    alert(message);
});

This is your question solution, OR you can go with @Rayon.

840

Answer

Solution:

Use slice function :

<script type="text/javascript">
    $(function () {
        $('#agency').multiselect({
            includeSelectAllOption: true
        });
        $('#btnSelected').click(function () {
            var selected = $("#agency option:selected");
            var message = "";
            selected.each(function () {
                message += $(this).val() + ",";

            });
            message = message.slice(0, -1);
            alert(message);
        });
    });
</script>
6

Answer

Solution:

Try to get the value instead of option:selected, It may work for you

var selected = $("#agency").val();
154

Answer

Solution:

use rtrim method in php

// $commaString = "1,2,3,";
  $string = rtrim($commaString,",");
  // output 
  // 1,2,3

in Javascript

var comma_string = "1,2,3,";
string = comma_string.replace(/,+$/,'');

You can use in either side as your logic.

151

Answer

Solution:

Just use$rating_agencies = array_filter($rating_agencies) before your "if" statement.

People are also looking for solutions to the problem: php - Image uploading in yii framework

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.