如何使用ajax和json发布单选按钮和菜单选择

时间:2013-12-27 19:30:41

标签: javascript jquery ajax json

我发现了几篇关于此的文章,但没有任何符合我情况的文章。我有这些单选按钮和下拉菜单,我需要在json dataType中使用ajax发布选择。简单地说,我在“这里有什么”这个地方放什么?

提前致谢!

<script>
    $(function() {
        $('#driver').click(function(){
            $.ajax({
                url:'_resources/_helpers/grader-test.php',
                type: "POST",
                data:{ /*WHAT GOES HERE?????*/ },//<---WHAT GOES HERE??
                dataType: "json",
                success: function(result){
                $('#jsonstuff').html(result);
                    console.log(result[0].AVG);
                    $('#grade').html(result[0].AVG);
                }
            });
        });
    });
</script>

<td>
    Select:
    <select id="extension">
        <option value="empty"></option>
        <? foreach ($sql as $row){
            echo '<option value = "' . $row['extension'] . '">' . $row["tech_fname"] . ' ' . $row["tech_lname"] . ' - ' . $row['extension'] . '</option>';
        ?>
    </select>
</td>

<td>
    As Reviewer<input type="radio" class="as_type" name="as_type" value="reviewer"/><br />
    As Reviewed<input type="radio" class="as_type" name="as_type" value="reviewed"/>
    <input type="button" id="driver" value="Submit" />
</td>
<td id="grade"></td>

这是grader-test.php

$ext = $_POST['extension'];// 2752;//
$type = $_POST['as_type'];// "reviewer";// 

switch($type){

   case "reviewer":
      $dbh = new PDO('mysql:host=;dbname=', '', '');
      $sql = "SELECT AVG(grade_average) AS AVG from call_reviews WHERE reviewer_ext = :ext";
      $stmt = $dbh->prepare($sql);
      $stmt->bindParam(':ext',$ext);
      $stmt->execute();
      $average = $stmt->fetchAll(PDO::FETCH_ASSOC);
      echo json_encode($average);
      break;

   case "reviewed":
      $dbh = new PDO('mysql:host=;dbname=', '', '');
      $sql = "SELECT AVG(grade_average) AS AVG from call_reviews WHERE reviewed_ext = :ext";
      $stmt = $dbh->prepare($sql);
      $stmt->bindParam(':ext',$ext);
      $stmt->execute();
      $average = $stmt->fetchAll(PDO::FETCH_ASSOC);
      echo json_encode($average);
      break;

    default:
      echo "select something";
}

0 个答案:

没有答案