weixin_33688840 2016-11-20 14:29 采纳率: 0%
浏览 34

使用AJAX调用PHP函数

This question already has answers here:
Closed 3 years ago.

I've read all the topics about my question but cannot solve my problem. I want to get php function result using jQuery AJAX.

function fetch_select(){
  val_name = $('#name').val();
  $.ajax({
    type: 'POST',
    url: 'include/get_db.inc.php',
    data: {
           name: val_name,
    },
    success: function (response) {
      document.getElementById('higtchart_medie_gen').innerHTML=response;
      columnChart( JSON.parse(response));
    }
  });
}

function columnChart(data_v){
  if(data_v.length >0){
    $(function () {
      $('#higtchart_medie_gen').highcharts({
        chart: {
          type: 'column'
         },
......

#name is id for select tag.
My code for get_db.inc.php is:

<?php
function test_name () {
  $ret = [];
  if(isset($_POST['name'])){
    $name = $_POST['name'];
    $sql = "SELECT 
            ......
            WHERE ID = $name ";
    $result = $conn->query($sql);
    if($result->num_rows > 0){
      while($row = $result->fetch_assoc()) {
        $ret [] = [$row['NAME'] . ' ' . $row['LASTN'], floatval($row['AVGG'])];
      }
    }
  }
  if(count($ret) >1) echo json_encode($ret);
  else echo 'Not working';
}
?>

How can I call test_name function from Ajax code? Thank you very much!

</div>
  • 写回答

1条回答 默认 最新

  • weixin_33690367 2016-11-20 14:34
    关注

    You do almost correct but only one mistake is you forget to invoke the function. What you do is just send the data to this file.

    So, to fixed this. Just add test_name() to your get_db.inc.php

     <?php
          function test_name () {
               $ret = [];
               if(isset($_POST['name'])){
                    $name = $_POST['name'];
                    $sql = "SELECT 
                     ......
                    WHERE ID = $name ";
                    $result = $conn->query($sql);
                    if($result->num_rows > 0){
                         while($row = $result->fetch_assoc()) {
                              $ret [] = [$row['NAME'] . ' ' . $row['LASTN'],floatval($row['AVGG'])];
                         }
                    }
               }
              if(count($ret) >1) echo json_encode($ret);
              else echo 'Not working';
         }
    
         test_name()
     ?>
    

    Also it will be better to check isset outside the function.

    function test_name ($name) {
         $ret = [];
         $sql = "SELECT 
         ......
         WHERE ID = $name ";
         $result = $conn->query($sql);
         if($result->num_rows > 0){
               while($row = $result->fetch_assoc()) {
                    $ret [] = [$row['NAME'] . ' ' . $row['LASTN'],floatval($row['AVGG'])];
               }
         }
         if(count($ret) >1) echo json_encode($ret);
         else echo 'Not working';
    }
    
    if(isset($_POST['name'])){
        test_name($_POST['name'])
    }
    

    This will make your function to be pure. It will easier to debug later and it will not invoke if you don't have $_POST['name'].

    评论

    报告相同问题?

    悬赏问题

    • ¥20 arcgis制做交通拥堵时变图
    • ¥15 AD20 PCB板步线 这个要怎么步啊
    • ¥50 关于《奇迹世界》1.5版本,数据修改
    • ¥15 请问这个问题如何解决(关键词-File)
    • ¥50 visual studio 2022和EasyX图形化界面
    • ¥15 找一下报错原因,纠正一下
    • ¥50 Cox回归模型Nomogram图制作报错
    • ¥20 SQL如何查询多级用户的数据
    • ¥15 给车牌识别代码加一个识别轮廓长宽比的代码
    • ¥30 商品价格预测的transformer模型优化