繁体   English   中英

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

[英]How do i send ajax function values from a page to another

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

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

然后这个 AJAX 实时搜索 function 接收输入,将其发送到“livesearch.php”文件以在数据库中搜索匹配数据。 然后接收一个段落元素以将其显示为搜索结果。

“index.php”中的 AJAX function

<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);

暂无
暂无

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

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