简体   繁体   English

使用jQuery的AJAX请求未发布表单数据

[英]AJAX request using jQuery not posting form data

I'm trying to create a search form that posts the results queried from a MySQL database and I'm having trouble. 我正在尝试创建一个搜索表单,该表单发布从MySQL数据库查询的结果,但遇到了麻烦。 The query is running correctly but the information entered in my form field is not 'posting' into the php document and actually getting through 该查询运行正常,但是在我的表单字段中输入的信息没有“发布”到php文档中并实际上通过

    <form name="IDsearchform" action="">
    <input class='required digits' type="text" value="" maxlength='8' minlength='8' name="term" id="search" />
    </form>


$(document).ready(function(){
    //show loading bar
    function showLoader(){
        $('.search-background').fadeIn(200);
    }
    //hide loading bar
    function hideLoader(){
        $('#sub_cont').fadeIn(1500);
        $('.search-background').fadeOut(200);
    };
    $('#search').keyup(function(e) {
      if(e.keyCode == 13) {
        showLoader();
        $('#sub_cont').fadeIn(1500);
        $("#content #sub_cont").load("<?php bloginfo('template_directory'); ?>/searchID.php", hideLoader());
      }
      });     
    $(".searchBtn").click(function(){   
        //show the loading bar
        showLoader();
        $('#sub_cont').fadeIn(1500);         
        $("#content #sub_cont").load("<?php bloginfo('template_directory'); ?>/searchID.php", hideLoader());
    });
});

What you need to do is fist serialize the form data and then send it across. 您需要做的是首先序列化表单数据,然后将其发送出去。 Otherwise jquery does not send the form data. 否则,jQuery不会发送表单数据。 This is what you need to do - 这是您需要做的-

$("#content #sub_cont").load("<?php bloginfo('template_directory'); ?>/searchID.php",$("#IDsearchform").serialize(), hideLoader());

This way your form post data is automatically send my jQuery. 这样,您的表单发布数据将自动发送给我的jQuery。

You should include the extra parameter in your call to $.load(): 您应该在对$ .load()的调用中包含额外的参数:

$("#content #sub_cont").load("<?php bloginfo('template_directory'); ?>/searchID.php",
 {term:$(#search.value)}, hideLoader());

It's important to include the data, first of all. 首先,包含数据很重要。 ;-) Second, you want to make sure that the data is an object so that jquery will do a POST instead of a GET. ;-)其次,您要确保数据是一个对象,以便jquery将执行POST而不是GET。

Source 资源

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

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