Ajax显示结果已经创建了div

时间:2012-10-07 18:58:23

标签: php jquery ajax

我有index.php之类的主页面,我在index.php中包含了sidebar.php。在index.php我有div id=content已经加载了一些内容..在sidebar.php我有

<form id="search" method="post" action="" >
      <input type="search" name="search" id="search"> 
      <input type="submit" name="search" id="search" class="btnButton" value="Search"/>
</form> 

那么当我点击提交时,如何在div id=content中显示我的搜索结果?

2 个答案:

答案 0 :(得分:1)

您只需从jquery中选择元素,然后执行方法.ajax {}

例如

$('#contentElementId').ajax({options...})

结果加载到

<div id="contentElementId"></div>

答案 1 :(得分:0)

使用Jquery ajax从主页调用search.php。确保你引用jQuery库。

//Main Page
<form id="search" method="post" action="" >
      <input type="text" name="search" id="search"> 
      <input type="submit" name="search1" id="search1" class="btnButton" value="Search"/>
</form> 

<div id="content"></div>

<script>
$(document).ready(function(){

    //jquery click event
    $("#search").live("click", function(e){

       //disable default form submit postpage
       e.preventDefault();

       //make ajax call to the search.php parsing the search textbox value as query string
       $.get("search.php?s="+$("#search").val(), function(result){

            //Result will be dumped in the div
            $("#content").html(result);

       });

   });

});

</script>



//Search PHP Script search.php
<?php

//Get the search String
$string = $_GET["s"];

//DO search query and echo it on this page.
?>