使用ajax jquery将对象类型值传递到php页面

时间:2015-11-02 20:19:27

标签: php jquery ajax

我有多个多重动态复选框,然后选中复选框 它返回对象格式的值,如下所示。

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

如何将其传递到ajax数据并在php文件中接收。

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

请帮助我,谢谢你。

我的html提到下面

<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>

我从下面的函数

收到值
<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);
    });
});

1 个答案:

答案 0 :(得分:-1)

我使用的函数如下所示:

// 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);
        }
    });
}

您应该能够将对象作为json_data参数传递。您必须使用如下签名编写成功处理程序:

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

您应该能够使用以下内容访问PHP中的JSON数据:

$json_data = $_POST['json_data'];
$value = $json_data['key'];