weixin_33744854 2017-12-06 22:30 采纳率: 0%
浏览 22

PHP通过按键jQuery

I want to make search in my username database but it doesnt recognize keypress function. also, I want to prevent search.php on first load (can't use isset because there is no button) this is my index.php

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<head>
  <title></title>
  <?php  include 'search.php'; ?>

  <script>
    $(document).ready(function() {
      $("#textbox1").keypress(function() {
        $.ajax({
          type: "GET",
          url: "search.php",
          success: function(data) {
            $("#main").html(data);
          }
        });
      });
    });
  </script>

  <form method="POST">
    enter keyword to search<br>
    <input type="text" name="textbox1" id="textbox1">
    <br><br>
    <div id="main"></div>
  </form>
</head>

<body>

This is my search.php. the connection.php is working proper. so I'm not pasting it here

<?php

    include 'connection.php';

    $search_value = $_POST['textbox1'];
    $query = "SELECT username FROM users WHERE username LIKE '" . $search_value . "%'";
    $conn_status = mysqli_query($conn, $query);

    while($row = $conn_status->fetch_assoc())
    {
        echo $row['username'] . '<br>';
    }
?>
</div>
  • 写回答

2条回答 默认 最新

  • csdn产品小助手 2017-12-06 22:35
    关注

    You should send the field value to your PHP page as data in your ajax request like :

    $.ajax({
        type: "GET",
        url: "search.php",
        data: {textbox1: $(this).val()},
        success: function (data) {
              $("#main").html(data);
        }
    });
    

    NOTE : I suggest the use of input event in this case since it's more efficient when tracking the user input :

    $("#textbox1").on('input', function(){
        //Your logic here
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助