使用ajax或jquery在html索引页面中加载php查询文件

的index.html

       $(function() { $(".search_button").click(function() { // getting the value that user typed var searchString = $("#search_box").val(); // forming the queryString var data = 'search='+ searchString; // if searchString is not empty if(searchString) { // ajax call $.ajax({ type: "POST", url: "query.php", data: data, beforeSend: function(html) { // this happens before actual $("#results").html(''); $("#searchresults").show(); $(".word").html(searchString); }, success: function(html) { // this happens after we get results $("#results").show(); $("#results").append(html); } }); } return false; }); $(document).ready(function{ $.ajax({ url: "query.php" }).done(function(data) { $('body').html(data); }); }); });   $(document).ready(function() { $.ajax({ type: "POST", url: "query.php", dataType: "text", data: dataString, success: function (response) { alert(response); //alert responce from query.php }, error:function (xhr, ajaxOptions, thrownError) { alert(xhr); } }); });    

query.php

Persons

ID First Name Last Name Age Hometown Job

我是javascript,jquery和ajax的新手,我想在修改上面的代码时提供一些帮助,这样当页面加载时,我可以将名为’query.php’的php / mysql查询的结果查看到我的索引文件中HTML页面。 任何帮助将不胜感激。

 $(document).ready(function() { jQuery.ajax({ type: "POST", // this is post request u can also do get request url: "query.php", dataType: "text", success: function (response) // this is the response from url: "query.php", { alert(response); //alert responce from query.php and here you can do // whatever u like with response. }, error:function (xhr, ajaxOptions, thrownError) { alert(xhr); // if any error function. } }); }); 

在你的index.html使用AJAX ,如下所示:

 $(document).ready(function{ $.ajax({ url: "query.php" }).done(function(data) { $('body').html(data); }); }); 

有关AJAX的更多信息。

确保在代码中包含了jQuery。