Home:ALL Converter>Get php selected checkbox values in JAVASCRIPT

Get php selected checkbox values in JAVASCRIPT

Ask Time:2016-04-12T07:31:35         Author:user5883938

Json Formatter

Im new to Javascript and i cannot solve this . I have a checkbox form , so i need to get the selected value(s) and post to a php file .

Here is the script :

<script>
                $("#addusers").click(function(){

                    var checkedValue = null;
                    var inputElements = document.getElementsByClassName('values');
                    for(var i=0; inputElements[i]; ++i){
                        if(inputElements[i].checked){
                            checkedValue = inputElements[i].value;
                            break;
                        }
                    }

                    $.post("users_add.php", {
                        checkedValue:checkedValue

                    }, function(data) {
                        alert(data);
                    });
                });
            </script>

Here is the php code :

<td>
                                <div>
                                    <?php
                                    foreach($userProfiles as $u)
                                    {
                                        foreach($u as $x)
                                        {
                                            echo "<input type=\"checkbox\" name=\"values\"> $x";
                                            echo "<br>";
                                        }
                                    }
                                    ?>

                                </div>
                            </td>

Im checking print_r($_POST) post variables , but it isnt showing anything. Is my javascript okay ?

Author:user5883938,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/36561062/get-php-selected-checkbox-values-in-javascript
yy