简体   繁体   English

Bootstrap 3-以模态显示Ajax搜索结果,然后将模态发布到其他位置

[英]Bootstrap 3 - showing ajax search results in a modal, then posting the modal elsewhere

I have a text input where a user can perform a search (for a username or email address held in a database). 我有一个文本输入,用户可以在其中执行搜索(查找数据库中保存的用户名或电子邮件地址)。 I already have a PHP script which does the searching etc. 我已经有一个PHP脚本来进行搜索等。

I want to display the search results in a Bootstrap 3 modal - or an error message if there are none. 我想以Bootstrap 3模式显示搜索结果-如果没有则显示错误消息。

I'm struggling to understand the full logic of doing this. 我正在努力了解这样做的全部逻辑。

What I have so far: 到目前为止,我有:

Search input and button: 搜索输入和按钮:

<input type="text" name="search">
<button id="searchBtn">Search</button>

PHP function (accessible via /ajax/search/ ) which accepts POST data (eg $_POST['search'] ), queries the database. 接受POST数据(例如$_POST['search'] )的PHP函数(可通过/ajax/search/访问),查询数据库。 At the moment this returns an array - empty if no results. 目前,这将返回一个数组-如果没有结果,则为空。

What is the correct order to run things? 正确的顺序是什么? I know that I need to pass my search input to /ajax/search/ . 我知道我需要将搜索输入传递给/ajax/search/ But I also need to open a modal - both of these are presumably done on clicking Search ( searchBtn ). 但是我还需要打开一个模式-大概都是在单击Search( searchBtn )时完成的。 Do I submit the form with jquery's ajax method first, and then get the success to open the modal? 我首先使用jquery的ajax方法提交表单,然后成功打开模式吗?

To further complicate matters, once this modal is shown there will be a series of tickboxes next to users that were found. 更复杂的是,一旦显示此模式,将在找到的用户旁边出现一系列复选框。 This will need to be posted elsewhere... But I'm not even getting this far yet. 这将需要发布在其他地方...但是我还没有走到这一步。

I'm struggling to understand this and have read a few posts already such as Bootstrap 3 with remote Modal and this Placing the results of a form post into a modal in Bootstrap 3 我正在努力了解这一点,并且已经阅读了一些帖子,例如带有远程Modal的Bootstrap 3,并且 Bootstrap 3 中的表单发布结果放入模式

If there are any tutorials which show this please could someone point me at one as I can't find one, particularly where things aren't marked "this has been deprecated". 如果有任何显示此内容的教程,请有人将我指向一个,因为我找不到一个,尤其是那些没有标记为“已弃用”的地方。

A simple example to consider: 要考虑的简单示例:

JS JS

$('#myModal').on('shown.bs.modal', function() {
  $(".modal-body").html("Loading...");
  $.ajax({
    url: "/path/to/request",
    data: { 
         // Parameters if needed
    },
    method: "POST",
    success: function(data) {
      $(".modal-body").html(data);
    }
  })
});

Example (mockup) at: 示例(样机)在:

https://jsfiddle.net/4bkhLatg/ https://jsfiddle.net/4bkhLatg/

I see what you're up against. 我知道你在面对什么。 Bootstrap is just your "display", but in looking at Bootstrap to teach the "go get" portion of it, the goodies are lost on the bus that passed by. Bootstrap只是您的“展示”,但是在查看Bootstrap来教它的“获得”部分时,好东西会丢失在经过的总线上。 Jump onto this very simple example of external Ajax: 跳到外部Ajax的这个非常简单的示例:

http://www.w3schools.com/jquery/ajax_ajax.asp http://www.w3schools.com/jquery/ajax_ajax.asp

Then after (hidden) update of (hidden #div1): 然后在(隐藏的#div1)的(隐藏)更新之后:

$("#div1").html(result);

you call your bootstrap modal to show the (hidden) div: 您将您的引导程序模式称为显示(隐藏的)div:

$('#div1').modal('show')   

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

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