如何使用MySQL数据库将HTML到一个div [英] How to use a mySQL database to put HTML into a div
问题描述
目前,我使用这个jQuery的code把HTML从另一个文件到内容的div
Currently, I am using this jQuery code to put HTML from another file into a content div
$("#list li a").click(function(){
var id = this.id;
$('#content').fadeOut('fast', function(){
$.ajax({url:"./html/" + id + ".html",success:function(result){
$("#content").html(result);
}});
});
$('#content').fadeIn('fast');
});
这是从这个名单拉从锚标签的ID
It is pulling the ID from the anchor tags from this list
<ul id='list'>
<li><a href="#" class="nav" id="home">Home</a></li>
<li><a href="#" class="nav" id="order">Order Form</a></li>
</ul>
该数据库将有一个表名为内容与名为HTML,其中包含了需要被加载到DIV实际的HTML code柱。
The database would have a table named 'content' with column named 'html' which contains the actual HTML code that needs to be loaded into the div.
我如何使用ID在锚标记,以获得与HTML code中的相应行,并在内容DIV显示呢?
How do I use the ID in the anchor tag to get the corresponding row with the HTML code, and display it in the content div?
编辑:所以,我想通了,这将是我的查询:
So, I figured out that this would be my query:
$result = mysqli_query($con,"SELECT content FROM htmlcontent WHERE id='' ");
我现在的问题是,如何我通过从HTML页面的PHP函数在Ajax调用的ID,以及如何返回$结果,并在#内容股利显示呢?
My question now is, how to I pass the ID from the ajax call in the html page to the php function, and how do I return the $result and display it in the #content div?
推荐答案
有多种方式将数据传递到服务器。也就是说,你可以使用HTTP GET方法或HTTP POST方法。现在,你正在使用jQuery $。阿贾克斯方法,它在默认情况下,使用GET。在一个GET请求,你必须通过在URL中使用以下格式的数据...
There are multiple ways to pass data to a server. Namely, you can use the HTTP GET method or the HTTP POST method. Right now, you are using the jquery $.ajax method, which, by default, uses GET. In a GET request, you must pass the data in the url with the below format...
$.ajax({url:"./path/to/your/php/script?id=" + id ,success:function(result){
...
所以呈现的URL最终会看起来像www.somesite.com/path/to/your/php/script?id=24
So the rendered URL would end up looking like www.somesite.com/path/to/your/php/script?id=24
那么你的服务器上,可以从网址很容易抓住ID与PHP $ _GET方法,像这样......
then on your server, you can grab the id from the url easily with the php $_GET method, like so...
$id = $_GET['id'];
然后就可以查询数据库与该ID的记录...
and then you can query your database for the record with that ID...
$result = mysqli_query($con,"SELECT content FROM htmlcontent WHERE id = " . $id);
我建议你研究的基本的HTTP请求类型(GET,POST,PUT,DELETE)以及它们在PHP像$ _GET和$ _ POST方法的实现。
I recommend you research the basic HTTP request types (GET,POST,PUT,DELETE) and their implementations in php like the $_GET and $_POST methods.
这篇关于如何使用MySQL数据库将HTML到一个div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!