简体   繁体   English

苦苦思索如何将普通的 PHP 表单转换为使用 JQuery 提交的表单?

[英]Struggling with how to turn a normal PHP form into a form that submits using JQuery?

I have been reading lots of articles and also questions here on SO but I am still confused how to implement the functionality into my own work and would much appreciate some helpful pointers.我一直在阅读很多关于 SO 的文章和问题,但我仍然对如何在我自己的工作中实现该功能感到困惑,并且非常感谢一些有用的指示。

What I have already is a page with two separate forms - one is a simple search with some drop-down menus and text box inputs - I think this one should be a lot easier to implement.我已经是一个页面,其中包含两个单独的 forms - 一个是带有一些下拉菜单和文本框输入的简单搜索 - 我认为这个应该更容易实现。

The advanced search form includes the simple search values by copying anything in the simple search fields into some hidden inputs.高级搜索表单通过将简单搜索字段中的任何内容复制到一些隐藏输入中来包含简单搜索值。 This form is also based around a row of three dropdowns that is cloned and removed at the users will to allow for any number of inputs to be searched for.此表单还基于一行三个下拉列表,用户可以随意克隆和删除这些下拉列表,以允许搜索任意数量的输入。

The main confusion I have is my forms action point to the same page where I have all of my php code to take the values and construct a MySQL query etc so I don't know how to use the success part of the AJAX request. The main confusion I have is my forms action point to the same page where I have all of my php code to take the values and construct a MySQL query etc so I don't know how to use the success part of the AJAX request.

The complete code for the page, should you wish to see it, is here如果您希望查看该页面的完整代码,请点击此处

A Fiddle showing the advanced search is here: Advanced Seach Fiddle显示高级搜索的小提琴在这里: Advanced Seach Fiddle

So bascially, what I am looking to try and find out is how to turn a static form that works perfectly into one that submits with JQuery and shows a nice 'searching' preloader etc.所以基本上,我想要尝试找出的是如何将一个完美工作的 static 表单变成一个与 JQuery 一起提交并显示一个很好的“搜索”预加载器等的表单。

There are 2 ways to proceed here:有两种方法可以在这里进行:

  1. Split page rendering and search processing code.拆分页面呈现和搜索处理代码。 Search processing code should return search results in a particular format (HTML, XML, JSON).搜索处理代码应以特定格式(HTML、XML、JSON)返回搜索结果。 After getting search results you can display them in whatever way you need.获得搜索结果后,您可以以任何您需要的方式显示它们。

  2. Keep your code as it is and parse data returned by AJAX call to extract search results section from it.保持您的代码不变并解析 AJAX 调用返回的数据以从中提取搜索结果部分。 If you go this way AJAX call will return full page as a result.如果你 go 这样 AJAX 调用将返回整页作为结果。 So using jQuery's find method you will need to extract search results section from it.因此,使用 jQuery 的find方法,您需要从中提取搜索结果部分。

Does this answer your question?这回答了你的问题了吗?

We'll, you've certainly done the hard work here;-) the jQuery side of it is fairly straight forward.我们会的,您肯定已经在这里完成了艰苦的工作;-) 它的 jQuery 方面相当简单。 You query your server with the serialized form data.您使用序列化的表单数据查询您的服务器。 Something to get you on the right track:让你走上正轨的东西:

$form = $('.advancedsearchform');

$form.submit(function() {

    $.ajax($form.attr('action'), {
      data: $form.serialize(),
      dataType: 'JSON',
      type: 'post', 

          beforeSend: function() {
            var $load = $('#loadingmessage');
            if (!$load.length)
            {
                $load = $('<div id="loadingmessage">').appendTo($form);
            }
            $load.html('Loading... Please wait...');
        },

        success: function(response) {
            // response is the text sent back by server
            // maybe you could use json_encode in PHP with a success message?
        },

        complete: function() {
            // Hide loading message:
            $('#loadingmessage').hide();
        }
    });

      return false; // Cancel default event
});

Use the jQuery Form Plugin .使用jQuery 表单插件 It's a dead simple way to AJAXify an existing form.这是 AJAXify 现有表单的一种非常简单的方法。

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

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