pass object type value into php page using ajax jquery

337
November 23, 2016, at 7:02 PM

I have multiple multiple dynamic checkbox and after checked to the checkbox it return values on object format like below.

 Object { Arunachal Pradesh="Arunachal Pradesh",  Assam="Assam"}

How can I pass it into ajax data and receive in php file.

 alert(url);
    $.ajax({
    type: "POST",
    url: url,
    data: pass here the value,
    cache: false,
    dataType: "html",

Kindly help me please thank you.

my html mention below

<div class="container">
        <div class="row">
        <!-- left-part -->
        <form id="all_check_boxes" name="checkBox">
    <div class="col-md-3 col-sm-3 college-admin">   
        <h2>Refine Results<span class="head-line"></span></h2>
    <br>
        <div>
        <h4>State</h4>
            <div class="clearfix"></div>
        <div style="max-height: 150px; overflow-y: scroll;" class="custom-leftbar">
                <?php foreach($selectState as $value) { ?>
                 <div class="checkbox">
                  <label><input id="<?=$value->state_name; ?>" class="check" type="checkbox" value="<?=$value->state_name; ?>" name="<?php echo $value->state_name; ?>"><?php echo $value->state_name; ?></label>
                 </div>
                 <?php } ?>
        </div>  
            </div>
            <br>
            <div>
        <h4>Co-Education Status</h4>
            <div class="clearfix"></div>
        <div class="custom-leftbar">
                <div class="checkbox">
                    <label><input id="co_ed" class="check" type="checkbox" value="co_ed" name="co_ed">Co-Ed</label>
                </div>
                <div class="checkbox">
                    <label><input id="girls" class="check" type="checkbox" value="girls" name="girls">Girls</label>
                </div>
                <div class="checkbox">
                    <label><input id="boys" class="check" type="checkbox" value="boys" name="boys">Boys</label>
                </div>
        </div>  
            </div>
        <br>
            <div>
        <h4>College Sector</h4>
            <div class="clearfix"></div>
        <div class="custom-leftbar">
                <div class="checkbox">
                    <label><input id="private" class="check" type="checkbox" value="private" name="private">Private</label>
                </div>
                <div class="checkbox">
                    <label><input id="government" class="check" type="checkbox" value="government" name="government">Government</label>
                </div>
                <div class="checkbox">
                    <label><input id="semi_government" class="check" type="checkbox" value="semi_government" name="semi_government">Semi-Government</label>
                </div>
        </div>  
            </div>
            <br>
            <div>
        <h4>Program Mode</h4>
            <div class="clearfix"></div>
        <div class="custom-leftbar">
                <div class="checkbox">
                    <label><input id="part_time" class="check" type="checkbox" value="part_time" name="part_time">Regular</label>
                </div>
                <div class="checkbox">
                    <label><input id="distance" class="check" type="checkbox" value="distance" name="distance">Distance</label>
                </div>
                <div class="checkbox">
                    <label><input id="both" class="check" type="checkbox" value="both" name="both">Both</label>
                </div>
        </div>  
            </div>
            <br>
            <div>
        <h4>Qualifying Exam</h4>
            <div class="clearfix"></div>
        <div class="custom-leftbar">
            <?php foreach($getExamsCategory as $exam_cat) { ?>
                <div class="checkbox">
                    <label><input id="<?=$exam_cat->exam_category_name ?>" class="check" type="checkbox" name="exam_category" value="<?=$exam_cat->exam_category_name ?>"><?=$exam_cat->exam_category_name ?></label>
                </div>
            <?php } ?>              
        </div>  
            </div>
            <br>
            <div>
        <h4>Courses Offered</h4>
            <div class="clearfix"></div>
        <div class="custom-leftbar">
                <?php foreach($getCourseCategoryRandom as $course_category) { ?>
                <div class="checkbox">
                    <label><input id="<?=$course_category->category_name ?>" class="check" type="checkbox" name="course_category" value="<?=$course_category->category_name ?>"><?=$course_category->category_name ?></label>
                </div>
            <?php } ?>
        </div>  
            </div><br>
            <div>
        <h4>Courses Level</h4>
            <div class="clearfix"></div>
        <div class="custom-leftbar">
                <div class="checkbox">
                    <label><input id="UG" class="check" type="checkbox" name="ug" value="UG">UG</label>
                </div>
                <div class="checkbox">
                    <label><input id="PG" class="check" type="checkbox" name="pg" value="PG">PG</label>
                </div>
                <div class="checkbox">
                    <label><input id="Doctorate" class="check" type="checkbox" name="doctorate" value="Doctorate">Doctorate</label>
                </div>
        </div>  
            </div><br>
        <div>
        <h4>Courses Degree</h4>
            <div class="clearfix"></div>
        <div class="custom-leftbar">
                <?php foreach($selectCourse as $course) { ?>
            <div class="checkbox">
                <label><input id="<?=$course->course_title ?>" class="check" type="checkbox" name="course_title" value="<?=$course->course_title ?>"><?=$course->course_title ?></label>
            </div>
            <?php } ?>              
        </div>  
            </div>
    </div>
    </form>

and i receive the values from below function

<script>
$(document).ready(function(){
    //debugger;
    $(".check").click(function(){
        var dataString = {};
        $("input:checkbox[class=check]:checked").each(function () {
            var key = $(this).val();
            var value = $(this).attr("id");
            dataString[key] = value;
        });
        console.log(dataString);
    });
});
Answer 1

I use a function that looks like this:

// POST an AJAX request with JSON data
function put_json_data(json_data) {
    $.ajax({
        url: url,
        type: "POST",
        data: { "json_data": json_data },
        success: get_result,
        error: function(xhr, textStatus, thrownError) {
            console.log("xhr.status       = " + xhr.status);
            console.log("xhr.responseText = " + xhr.responseText);
        }
    });
}

You should be able to pass your object as the json_data parameter. You will have to write a success handler with a signature like this:

function get_result(data, textStatus, xhr) {
    // Handle result
}

You should be able to access the JSON data in your PHP using something like this:

$json_data = $_POST['json_data'];
$value = $json_data['key'];
Rent Charter Buses Company
READ ALSO