簡體   English   中英

使用ajax和php根據文本框更改更改查詢

[英]change query based on textbox change using ajax and php

我一直在嘗試使用帶有ajax的php更改查詢。 這是怎么回事

  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。

我需要檢查id為animalfilter的輸入文本框是否已更改,然后使用ajax查詢它

對於2。

$(document).ready(function () {
    $("#animalfilter").change(function () {
        $('#animalfilter').val($("#animaloption").val());
        // var animal= $('#animalfilter').val($("#animaloption").val()); //<-- i highly doubt you need this
        var animal= $('#animalfilter').val();                        //<-- rather this
        $.ajax({
            url: "ajax/test.php",
            type: "post",
            data: animal,
            success: function (response) {
                  $("#kind").html(response);             
            }
       });
   });
}); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM