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'].

    评论

报告相同问题?

悬赏问题

  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?