繁体   English   中英

Ajax - 来自另一页的响应

[英]Ajax - Response from another page

我的HTML如下,位于index.php

        <div id="showDetails">
           
        </div>


        <div id="showList">
           
        </div>

而我的Ajax如下,还在index.php

  function funcReadRecord() {
    var readrecord1 = "readrecord1";
    var sometext = $('#SNOW_INC').val();

    $.ajax({
      url : "findsql.php",
      type : 'post' ,
      data : { readrecord1 : readrecord1,
        sometext : sometext } ,
        success : function(data, status){
          $('#showList').html(data);
        }
      });

    }

现在,我可以返回我的列表并在 index.php 中查看所需的列表(显示为列表组)。 我在 index.php 中有一个按钮,单击它会运行 function。

<div>
<button type="button" onclick="funcReadRecord()" class="btn btn-primary">Search SQL (LIKE)</button>
</div>

findsql.php中的代码如下

if(isset($_POST['readrecord1']) && isset($_POST['sometext'])){

  $displaysql = "SELECT * from datadump where short_description LIKE '%".$_POST['sometext']."%'";
  $result = mysqli_query($conn, $displaysql);

  if(mysqli_num_rows($result) > 0){

    while ($row = mysqli_fetch_array($result)) {

      $items[] = array(
                        "number" => $row['number'],
                        "priority" => $row['priority'],
                        "description" => $row['short_description']);
    }
  }

  echo '<p class="lead">SEARCH SQL (LIKE)<p>';

  echo '<div class="list-group">';
  foreach($items as $result) {
      
  ?>

      <a href="#" class="list-group-item list-group-item-action">
      <div class="d-flex w-100 justify-content-between">
        <h5 class="mb-1"><?php echo $result['number']; ?></h5>
        <small></small>
      </div>
      <p class="mb-1"><?php echo $result['description']; ?></p>
      <small><?php echo $result['priority']; ?></small>
    </a>

    <?php
  }
  echo '</div>';

}

我所做的就是从 MySQL 获取数据并将它们分配给数组并列出它们。 我知道我可以直接这样做,但我需要其他一些 function 中的数组。问题是当我单击列表时,如何从数组中获取详细信息以显示在showDetails div 标记中? 现在,HREF 是#。 我可以分配一个 function,但不确定将它们写在哪里。

如果我应该写一个 function 来返回它们,我应该写在 index.php 还是 findsql.php 中?

提前致谢。

我知道您需要在#showDetails div 中添加单独的记录信息:然后是第 1 步,在单击特定项目时分配新的 function onclick="funcReadRecord(number)"。 这应该在 findsql.php 文件:step2。 在 index.php 中写一个 ajax function 它将发送特定的唯一 ID 或您的案例编号

function funcReadRecord(number) {
  $.ajax({
   url : "findsql.php",
   type : 'post' ,
  data : { id: number } ,
    success : function(data, status){
      $('#showDetails').html(data);
    }
  });

第三步:在 findsql.php 中写入另一个function和 else if 块作为检查 id isset与否,更改获取数字或仅获取该特定记录的任何键的查询。

else if(isset($_POST['id'])){
   $displaysql = "SELECT * from datadump where number = ".$_POST['id'].";
   // remaining design code below
 }

我们可以使用 if-else 语句编写多个 ajax 调用,如上。

编辑,注意:请忽略上面代码中的语法问题,专注于使用分支语句对单个 PHP 文件进行多个 ajax 调用的过程。

暂无
暂无

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

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