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');
});
It is pulling the ID from the anchor tags from this list
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.
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?
EDIT: So, I figured out that this would be my query:
$result = mysqli_query($con,"SELECT content FROM htmlcontent WHERE 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?
解决方案
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){
...
So the rendered URL would end up looking like www.somesite.com/path/to/your/php/script?id=24
then on your server, you can grab the id from the url easily with the php $_GET method, like so...
$id = $_GET['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);
I recommend you research the basic HTTP request types (GET,POST,PUT,DELETE) and their implementations in php like the $_GET and $_POST methods.