a variable does not pass from javascript to php through ajax

I think the issue was with the checkboxes set. Nonetheless, its working now. All the values being checked are passes properly and can be checked by uncommenting the lines in php code.


<!DOCTYPE html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

    <span class="custom-checkbox">
    <input type="checkbox" class="checkbox" id="checkbox1" name="options1[]">
    <input type="checkbox" class="checkbox" id="checkbox2" name="options2[]">
    <input type="checkbox" class="checkbox" id="checkbox3" name="options3[]">
        <label for="checkbox1"></label>
    <button class="button1">Submit</button>

<script type="text/javascript">
    $( document ).ready(function() {
        $( ".button1" ).click(function() {
            var val = [];

            //Fetch all checked values 
            $("input:checked").each(function (index,value) {
                val[index] = this.value;
            var myJSONText = JSON.stringify(val);
                data: {'kvcArray': myJSONText},
                url: 'test.php',
                type: 'POST',
                success: function(result) {


PHP Code:

    //To Test the incoming values just uncomment these lines below.
    // print_r($_POST);
    // die();

    /*Check if form value is set*/ 
    if (isset($_POST['kvcArray'])) {
        // Pass the value to ajax success method
        echo $_POST['kvcArray'];

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top