简体   繁体   English

如何在OOP中发送AJAX请求?

[英]how can I send AJAX request in OOP?

I have form like this. 我有这样的形式。

<form action="Barang.php" method="POST" class="form-horizontal" role="form">
    <div class="form-group">
      <label class="control-label col-md-3" 
        for="id_suplier">ID suplier :</label>
         <div class="col-md-5">
            <select class="selectpicker" title="Ketikkan ID suplier" data-width="100%" data-live-search ="true" id="id_suplier" autocomplete="off" onchange="" required>
                <?php
                    $query = $db->query('SELECT * FROM tb_suplier');
                ?>
                <?php
                    while($row = $query->fetch(PDO::FETCH_ASSOC)){ ?>

                     <option value="<?php echo $row['id_suplier']; ?>"><?php echo $row['id_suplier']; ?></option>  

                <?php 
                  }
                ?>
            </select>
        </div>
        <span class="badge badge-info" style="margin-top:10px;" id="namasup">nama suplier</span>
    </div>
    </form>

    <script type="text/javascript">
        $('#id_suplier').on('change', function() {

            var id_suplier=$("#id_suplier").val();

            $.ajax({
              type:"POST",
              url:"Barang.php",
              dataType:'json',  
              success:function(data) {
                $("#namasup").html(data.namasup);
              }
        });

    })

    </script>

And I want send the request to Barang.php, and I want to process in getSuplier(), and get name suplier to database and put the value on id=namasup. 我想将请求发送到Barang.php,并要在getSuplier()中进行处理,并向数据库获取更名,并将其值放在id = namasup上。 how can I process it? 我该如何处理?

And here code Barang.php 这里的代码Barang.php

<?php
    class Barang
    {


        function getSuplier($id){
            $query = $this->db->query("SELECT nama from tb_suplier where id='$id' ");
            $result=$query->fetch(PDO::FETCH_ASSOC);
            return $result;
        }
    }

Maybe you can do this 也许你可以做到

 class Barang
 {
     function getSuplier($id){
         // your code here ....
     }      
 }

 $barang = new Barang;

 $barang->getSupplier($_GET['id']);

In addition to that, Jeff is right. 除此之外, 杰夫是正确的。 You are not sending your id_suplier to the server. 您没有将id_suplier发送到服务器。 Please send it. 请发送。

I think you no need to request name from database every time you change data in selection 我认为您每次更改选择中的数据时都不需要从数据库请求名称

In my opinion you can use this code 我认为您可以使用此代码

in html 在HTML

<html>
<body>
<form action="Barang.php" method="POST" class="form-horizontal" role="form">
  <div class="form-group">
    <label class="control-label col-md-3" 
      for="id_suplier">ID suplier :</label>
       <div class="col-md-5">
          <select class="selectpicker" title="Ketikkan ID suplier" data-width="100%" data-live-search ="true" id="id_suplier" autocomplete="off" onchange="" required>

          </select>
      </div>
      <span class="badge badge-info" style="margin-top:10px;" id="namasup">nama suplier</span>
  </div>
</form>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
  function createOption(datas)
  {
    var html = "";
    for (var i in datas){
      var data = datas[i];
      html = html + '<option value="'+data['id']+'">'+ data['name'] +'</option>';
    }
    return html;
  }
  $(document).ready(function() {
      //get all data
      $.ajax({
          type:"POST",
          url:"Barang.php",
          type: "json",
          //dataType:'json',  
          success:function(res) {
            var datas = JSON.parse(res);
            $("#id_suplier").html(createOption(datas));
            //tigger change event
            $('#id_suplier').change();
          }
      });

      $('#id_suplier').on('change', function(){ 
          //change name in span
          var name = $('#id_suplier').find(":selected").text();
          $("#namasup").html(name);
      });
  });


</script>
</body>

in php 在PHP中

class Barang
{
    //Method for get all datas
    public function getSupliers(){
        //I don't have you database so I skip this part

        // $query = $this->db->query("SELECT nama from tb_suplier");
        // $result=$query->fetch(PDO::FETCH_ASSOC);
        // return $result;

        //mock data
        $datas = [[
            'id'   => 1,
            'name' => 'Test 1',
        ],[
            'id'   => 2,
            'name' => 'Test 2',
        ],[
            'id'   => 3,
            'name' => 'Test 3',
        ]];

        return $datas;
    }
}

$class = new Barang();
$datas = $class->getSupliers();
echo json_encode($datas);

Hope this help 希望这个帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM