如何在xhr.responseText

时间:2017-10-21 22:06:30

标签: javascript php ajax model-view-controller

我一直在尝试用Ajax进行快速研究,我想在xhr.responseText中得到一个html表,接下来得到一个新的研究结果,我这样做但我没有找到一种方法可以帮助我解决它,如果可能的话?

Javascript功能

  function changement(str) {

        xhr = new XMLHttpRequest(); 
         var input = document.getElementById('rech').value;

        xhr.open('GET','?controller=livres&action=search&val=1316', true);      
        xhr.send();
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4 && xhr.status == 200) {
                document.getElementById("tab").innerHTML = xhr.responseText;
                alert(xhr.responseText);
            }

        };          

    }

控制器

  public function search()
 {
 if(!isset($_GET['val']))
 {
 return call('pages','error');
 }

 $livre= Livre::findByCin($_GET['val']);
  //require_once('views/livres/index.php');
 }


 }
 ?>

模型功能

      public static function findByCin($cin)
     {

      $db=Db::getInstance();
   $cin=intval($cin);
   $req=$db->prepare("SELECT * FROM document where ref like '%".$cin."%'");
     $req->execute();
     $list=[];
    foreach ($req->fetchAll() as $temp)
    {
     $Livre=new Livre();
       $Livre->setReference($temp['ref']);
      $Livre->setNom($temp['nom']);
      $Livre->setDateCreation($temp['date_creation']);
     $Livre->setPrix($temp['prix']);
     $Livre->setAuteur($temp['auteur']);
     $Livre->setnbPages($temp['nbrPages']);
     $list[]=$Livre;
     }

      return $list;
     }

HTML页面

        <div class="box">
        <div class="col-lg-12">
            <hr>
            <h2 class="intro-text text-center">
                <strong>Liste des Livres</strong>

            </h2>
            <hr> <br>
            <div class="row">
                <div class="col-sm-6">
                    <a class="pull-left btn btn-default" href="?controller=chercheurs&action=add">
                        Ajouter Livre</a>


                        <div class="col-sm-6">

                            <form action="?controller=livres&action=search" method="POST">
                                <div class="input-group">
                                    <input  type="search" onkeyup="changement(this.value)" name="search" class="form-control" placeholder="Recherche par Réference" id="rech">
                                    <span class="input-group-btn">
                                <button type="submit" class="btn btn-default"  > Filtrer </button>
                                    </span>
                                </div>
                            </form>
                        </div>
                    </div></div><br>
                    <div class="table-responsive" id="tab">
                        <table class="table table-stripped"><thead>
                            <tr>
                                <th>Réference</th><th>Nom</th><th>DateCreation</th><th>Prix</th><th>Auteur</th><th>NbrPages</th>
                            </tr> </thead>
                            <tbody>
                            <?php foreach ($livre as $livr) {?>
                                <tr>
                                    <td><?php echo $livr->getReference(); ?></td>
                                    <td><?php echo $livr->getNom(); ?></td>
                                    <td><?php echo $livr->getDateCreation(); ?></td>
                                    <td><?php echo $livr->getPrix(); ?></td>
                                    <td><?php echo $livr->getAuteur(); ?></td>
                                    <td><?php echo $livr->getnbPages(); ?></td>
                                    <td>
                                    <a class="btn btn-danger" href="?controller=livres&action=delete&id=<?php
                                    echo $livr->getReference(); ?>">
                                    Supprimer </a>
                                    <a class="btn btn-primary" href="?controller=livres&action=edit&id=<?php
                                    echo $livr->getReference(); ?>">
                                    Modifier </a
                                    </td>
                                </tr> <?php } ?>
                            </tbody> </table>
                        </div></div></div></div>

谢谢。

0 个答案:

没有答案