【发布时间】:2020-11-12 09:54:14
【问题描述】:
表单中有三个输入字段,我想使用 Axios 将这些输入字段中的数据发布到 Mysql 数据库表中。我尝试了很多次,但我做不到,我需要连接数据库的代码+插入查询
//PostForm.js
import React, { Component } from 'react'
import axios from "axios";
export default class PostForm extends Component {
constructor(props) {
super(props)
this.state = {
userId:"",
name:"",
email:""
}
}
changeHandler =e=>{
this.setState({[e.target.name]: e.target.value})
}
submitHandler= e=>{
e.preventDefault()
const {userId, name, email}= this.state
console.log(this.state)
axios.post('https://jsonplaceholder.typicode.com/posts', this.state)
.then(response=>{
console.log(response)
})
.catch(error =>{
console.log(error)
})
}
render() {
const {userId, name, email}= this.state
return (
<div>
<form onSubmit={this.submitHandler}>
<div>
<input type="text" name="userId" value={userId} onChange={this.changeHandler}/>
</div>
<div>
<input type ="text" name="name" value={name} onChange={this.changeHandler}/>
</div>
<div>
<input type= "text" name="email" value={email} onChange={this.changeHandler}/>
</div>
<button type="submit">Submit</button>
</form>
</div>
)
}
}
【问题讨论】:
-
您需要提供 BE 代码,有大量资源、文章和视频告诉您如何创建全栈 CRUD 应用。
标签: mysql node.js reactjs express axios