javascript - 使用ajax和php根据文本框更改更改查询

标签 javascript php jquery mysql ajax

我一直在尝试使用 php 和 ajax 来更改查询。事情是这样的

  1. 我有一个下拉菜单,更改后它会将所选数据复制到我使用 javascript 实现的文本框,如下所示。

    $(document).ready(function () {
         $("#animaloption").change(function () {
           $('#animalfilter').val($("#animaloption").val());
         });
    });
    

然后

  1. 我需要检查 id 为 animalfilter 的输入文本框是否已更改,然后使用 ajax 查询它,我不知道这个是否正确,因为我无法检索返回

    $(document).ready(function () {
        $("#animaloption").change(function () {
          $('#animalfilter').val($("#animaloption").val());
          var animal= $('#animalfilter').val($("#animaloption").val());
           $.ajax({
            url: "ajax/test.php",
            type: "post",
            data: animal,
            success: function (response) {
                $("#kind").html(response);             
            }
           });
       });
    });
    

带有id = kind的输入不会显示任何内容。

在我的 test.php 中

<?php
  $a = "SUCCESS";
  return $a;
 ?>

请大家帮忙。

谢谢。

最佳答案

您在这里遇到了几个问题,我将记下您关注的领域:

jQuery:

$(document).ready(function() {
    // Not necessary, but better to use .on() here
    $("#animaloption").on('change',function() {
        // Use $(this) instead, grabs the selected value. No sense in
        // capturing the same event value when you have it in this event already
        var animal = $(this).val();
        // Assign value here to field
        $('#animalfilter').val(animal);
        // Start ajax
        $.ajax({
            // Make sure this is the correct path
            url: "ajax/test.php",
            type: "post",
            data: animal,
            success: function(response) {
                $("#kind").html(response);
            }
        });
    });
});

/ajax/test.php

<?php
$a = "SUCCESS";
# You have to echo, not return
echo $a;
# Try print_r() for sure-fire results testing
print_r($_POST);

如果这些更改不能解决问题,则需要查看您的表单 html。

关于javascript - 使用ajax和php根据文本框更改更改查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40856628/

相关文章:

asp.net - jQuery 自动完成和 ASP.NET

javascript - 对象不支持 'Cookie'之后的属性或方法

javascript - 关闭后 qtip2 位置发生变化

php - 以编程方式设置基本图像

php - 我如何在 WordPress 中获取发布日期?

php - 在 PHP 循环中插入数据时优化 PDO 函数

Javascript 在手机上缩小

javascript - 测试 React 组件 : How does it work?

javascript - 一旦我收到 Modal.jsx 中的 Prop ,如何切换模态

javascript - 为不同的浏览器添加类