2015-11-02 49 views
1

我有多個多動態複選框,並勾選複選框 它返回對象格式的值如下。使用ajax將對象類型值傳遞到php頁面jquery

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

請幫助我,謝謝。

下面

<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); 
    }); 
}); 
+0

顯示我們至少位您的HTML – RiggsFolly

+0

Riggs我添加了html –

回答

-1

我使用的功能,看起來像這樣我的html提到:

// 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']; 
+0

現在我知道人們爲什麼害怕發佈問題的答案。一個匿名的投票沒有解釋花費我們的聲望點。 –

相關問題