【html+js】怎么在html旅游网页的风景区添加景区收藏功能,并收藏到mysql数据库里面

要在HTML网页中添加景区收藏⭐️功能,并将收藏信息保存到MySQL数据库中,我们可以结合使用HTML、JavaScript和服务器端的技术(如Node.js)来实现。下面是一个基本的实现步骤:

🌱准备工作:

确保你已经安装了Node.js和MySQL,并创建了一个名为scenic_spots的数据库以及一个名为spots的表,包含id、name和location字段。

创建一个Node.js项目,并安装Express和MySQL模块。

在HTML中添加收藏按钮:

在HTML网页中添加一个按钮,用于收藏景区。例如:

(1) html代码区域:

<button οnclick="favoriteScenicSpot('1')">收藏景区1</button>

 

编写JavaScript函数处理收藏操作:

在JavaScript中编写一个函数,用于处理收藏操作,并通过AJAX将收藏信息发送给服务器。

(2) javascript代码区域:

function favoriteScenicSpot(id) {

    // 发送POST请求到服务器,告知服务器收藏哪个景区

    fetch('/favorite', {

        method: 'POST',

        headers: {

            'Content-Type': 'application/json'

        },

        body: JSON.stringify({ id: id })

    }).then(response => {

        if (response.ok) {

            alert('景区已成功收藏!');

        } else {

            alert('收藏失败,请重试!');

        }

    }).catch(error => {

        console.error('收藏失败:', error);

        alert('收藏失败,请重试!');

    });

}

 

在Node.js中编写服务器端代码处理收藏请求:

在Node.js中编写路由处理器,用于接收客户端发送的收藏请求,并将收藏信息保存到MySQL数据库中。

(3) javascript代码区域:

const express = require('express');

const mysql = require('mysql');

 

const app = express();

const port = 3000;

 

const connection = mysql.createConnection({

    host: 'localhost',

    user: 'your_username',

    password: 'your_password',

    database: 'scenic_spots'

});

 

app.use(express.json());

 

app.post('/favorite', (req, res) => {

    const { id } = req.body;

 

    connection.query('INSERT INTO spots (id) VALUES (?)', [id], (error, results, fields) => {

        if (error) {

            console.error('添加景区失败:', error);

            res.status(500).send('添加景区失败');

            return;

        }

        console.log('景区已成功收藏');

        res.status(200).send('景区已成功收藏');

    });

});

app.listen(port, () => {

    console.log(`应用运行在 http://localhost:${port}`);

});

😘浅浅地去尝试一波吧!

  • 23
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是一个简单的示例代码,用于在uniapp中点击收藏后将信息添加MySQL数据库中: ```javascript // 前端部分 // 点击收藏按钮 onCollectionTap() { // 发送请求到后端 uni.request({ url: 'http://localhost:3000/collection', method: 'POST', data: { // 传递需要收藏的信息 title: this.title, content: this.content }, success: (res) => { // 收藏成功 uni.showToast({ title: '收藏成功' }) }, fail: (err) => { // 收藏失败 uni.showToast({ title: '收藏失败' }) } }) } ``` ```javascript // 后端部分 const express = require('express') const mysql = require('mysql') const app = express() // 创建MySQL连接 const connection = mysql.createConnection({ host: 'localhost', user: 'root', password: 'password', database: 'database_name' }) // 处理收藏请求 app.post('/collection', (req, res) => { const title = req.body.title const content = req.body.content // 插入数据到MySQL数据库中 const sql = `INSERT INTO collection (title, content) VALUES ('${title}', '${content}')` connection.query(sql, (err, result) => { if (err) { // 插入失败 console.log(err) res.status(500).send('Error') } else { // 插入成功 res.send('OK') } }) }) // 启动服务 app.listen(3000, () => { console.log('Server is running...') }) ``` 在这个示例中,我们使用了uni.request方法向后端发送了一个POST请求,在后端收到请求后,使用mysql模块连接到MySQL数据库,将传递过来的信息插入到collection表中,最后返回一个成功或失败的状态。 需要注意的是,在实际开发中需要进行参数的校验和防止SQL注入等安全性问题,以上代码仅供参考。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

疯疯不狗GG

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值