我如何将 ajax 函数值从一个页面发送到另一个

问题描述

我有这个输入字段可以读取用户在“index.PHP”中的输入

<input type="text" name="medname" id="medname" class="search-input" placeholder="Search..." name="">
       
<div class="result"></div>

然后这个 AJAX 实时搜索函数接收输入,将其发送到“livesearch.PHP文件以在数据库搜索匹配数据。然后接收一个paradee元素将其显示搜索结果

"index.PHP" 中的 AJAX 函数

<script>
    $(document).ready(function() {
      $('.search input[type="text"]').on("keyup input",function() {
        /* Get input value on change */
        var inputVal = $(this).val();
        var resultDropdown = $(this).siblings(".result");
        if (inputVal.length) {
          $.get("livesearch.PHP",{
            term: inputVal
          }).done(function(data) {
            // display the returned data in browser
            resultDropdown.html(data);
          });
        } else {
          resultDropdown.empty();
        }
      });

      // Set search input value on click of result item
      $(document).on("click",".result p",function() {
        $(this).parents(".search").find('input[type="text"]').val($(this).text());
        $(this).parent(".result").empty();
      });
    });
  </script>

livesearch.PHP

if(isset($_REQUEST["term"])){
    // Prepare a select statement
    $sql = "SELECT * FROM medicine WHERE mName LIKE ?";
    
    if($stmt = MysqLi_prepare($link,$sql)){
        // Bind variables to the prepared statement as parameters
        MysqLi_stmt_bind_param($stmt,"s",$param_term);
        
        // Set parameters
        $param_term = $_REQUEST["term"] . '%';
        
        // Attempt to execute the prepared statement
        if(MysqLi_stmt_execute($stmt)){
            $result = MysqLi_stmt_get_result($stmt);
            
            // Check number of rows in the result set
            if(MysqLi_num_rows($result) > 0){
                // Fetch result rows as an associative array
                while($row = MysqLi_fetch_array($result,MysqLI_ASSOC)){
                    echo "<p>" . $row["mName"] . "</p>";
                }
            } else{
                echo "<p>No matches found</p>";
            }
        } else{
            echo "ERROR: Could not able to execute $sql. " . MysqLi_error($link);
        }
    }

现在对于我的问题,有另一个名为“map.PHP”的页面。我需要将用户选择的“mID:药物 ID”和“mName:药物名称”发送到该页面

我该怎么做?

解决方法

我通过使用 json 解决了这个问题。我不太明白,但它有效(不包括 CSS)。

index.php

<script>
   jQuery(document).ready(function($) {
    $('#keyword').on('input',function() {
        var searchKeyword = $(this).val();
        if (searchKeyword.length >= 1) {
            $.post('search.php',{ keywords: searchKeyword },function(data) {
                $('ul#content').empty().addClass('active');
                $.each(data,function() {
                    $('ul#content').append('<li><a href="map.php?id=' + this.id + '">' + this.title + '</a></li>');
                });
            },"json");
        } else {
            $('ul#content').empty().removeClass('active');
        }
    });
});
  </script>

index.php(输入和结果字段)

<form method="POST">
        <input type="text" id="keyword" class="search-input" placeholder="Search..." name="">
        <ul class="result" id="content"></ul>
        </form>

搜索.php

<?php
define('DB_USER','root');
define('DB_PASSWORD','');
define('DB_SERVER','localhost');
define('DB_NAME','pharmacie');
if (!$db = new mysqli(DB_SERVER,DB_USER,DB_PASSWORD,DB_NAME)) {
    die($db->connect_errno.' - '.$db->connect_error);
}
$arr = array();
if (!empty($_POST['keywords']) && strlen($_POST['keywords']) >= 1) {
    $keywords = filter_var($_POST['keywords'],FILTER_SANITIZE_STRING);
    $keywords = $db->real_escape_string($keywords);
    $sql = "SELECT mID,mName FROM medicine WHERE mName LIKE '%".$keywords."%'";
    $result = $db->query($sql) or die($mysqli->error);
    if ($result->num_rows > 0) {
        while ($obj = $result->fetch_object()) {
            $arr[] = array('id' => $obj->mID,'title' => $obj->mName);
        }
    }
}
echo json_encode($arr);