mysql div_如何使用MySQL数据库将HTML到一个div

bd96500e110b49cbb3cd949968f18be7.png

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.

首先,在Spring Boot中使用JavaScript,需要在HTML页面中引入JavaScript文件,可以在HTML页面中添加如下代码: ```html <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script> <script type="text/javascript" src="path/to/your/javascript/file.js"></script> ``` 接着,在JavaScript文件中使用axios向后端发送请求,获取MySQL数据库中的全部信息。可以使用如下代码: ```javascript axios.get('/api/getAllInfo') .then(function(response) { // 处理响应数据 var data = response.data; // 将数据渲染输出到div块中 var output = document.getElementById('output'); data.forEach(function(item) { var div = document.createElement('div'); div.innerHTML = item; output.appendChild(div); }); }) .catch(function(error) { console.log(error); }); ``` 在后端,需要编写一个Controller类来处理前端发送的请求,并将MySQL数据库中的数据返回给前端。可以使用如下代码: ```java @RestController @RequestMapping("/api") public class MyController { @Autowired private MyService myService; @GetMapping("/getAllInfo") public List<String> getAllInfo() { return myService.getAllInfoFromMySQL(); } } ``` 在上述代码中,`MyService`是一个服务类,用于从MySQL数据库中获取数据。可以根据自己的需求来编写该类。 最后,在HTML页面中添加一个div块,用于将获取到的数据渲染输出。可以使用如下代码: ```html <div id="output"></div> ``` 当JavaScript代码执行完毕后,MySQL数据库中的全部信息将会被渲染输出到上述div块中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值