如何从火箭中的HTML表格中接收发布数据?
我正在在火箭网中构建一个简单的登录流,该流程详细介绍了用户的详细信息,并将其存储在数据库中。
但是,我似乎无法弄清楚如何从HTML表单中接收数据。我阅读“ nofollow noreferrer”>“火箭指南”的形式,我对此进行了编码:
use rocket::*;
use rocket::response::content::RawHtml;
use rocket::http::RawStr;
use rocket::form::Form;
#[get("/")]
fn index() -> RawHtml<&'static str> {
RawHtml(include_str!("templates/index.html"))
}
#[derive(FromForm)]
struct UserInput<'r> {
r#type: &'r str,
}
// #[post("/create_account", data = "<userdata>")]
// fn create_account(user_input: Form<UserInput>) -> RawHtml<&'static str> {
// print(format!("Your value: {}", user_input.value));
// return RawHtml(include_str!("templates/account.html", messages = messages))
// }
//use rocket::form::Form;
#[derive(FromForm)]
#[derive(Debug)]
struct user_input<'r> {
r#type: &'r str,
}
#[post("/create_account", data = "<task>")]
fn create_account(task: Form<user_input<'_>>) -> RawHtml<&'static str> {
println!("{:#?}",task);
return RawHtml(include_str!("templates/account.html"))
}
#[rocket::main]
async fn main() -> Result<(), rocket::Error> {
sql::setup();
let _rocket = rocket::build()
.mount("/", routes![index])
.mount("/", routes![create_account])
// .mount("/confirmation", routes![confirmation])
.launch()
.await?;
Ok(())
}
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>Account Creation</h1>
<br>
<h2>User Details</h2>
<form action="/create_account" method="post">
<!-- name, age, email, phno, password -->
<label for="Name">Name (Enter Full Name)</label>
<br>
<input type="text" name="Name" id="Name" text="Name">
<br>
<label for="age">Age</label>
<br>
<input type="number" name="age" id="age">
<br>
<label for="email">BCBS outlook email</label>
<br>
<input type="email" name="email" id="email" text="BCBS outlook email">
<br>
<label for="phno">Phone Number</label>
<br>
<input type="tel" name="phno" id="phno">
<br>
<label for="password">Password</label>
<br>
<input type="password" name="password" id="password">
<br> <br>
<input type="submit" value="Create Account">
</form>
</body>
</html>
I am building a simple login flow in a Rocket webserver which takes in details of the user and stores it in the database.
However, I can't seem to figure out how to receive the data from the HTML form. I read the Rocket guide for forms and I coded this:
use rocket::*;
use rocket::response::content::RawHtml;
use rocket::http::RawStr;
use rocket::form::Form;
#[get("/")]
fn index() -> RawHtml<&'static str> {
RawHtml(include_str!("templates/index.html"))
}
#[derive(FromForm)]
struct UserInput<'r> {
r#type: &'r str,
}
// #[post("/create_account", data = "<userdata>")]
// fn create_account(user_input: Form<UserInput>) -> RawHtml<&'static str> {
// print(format!("Your value: {}", user_input.value));
// return RawHtml(include_str!("templates/account.html", messages = messages))
// }
//use rocket::form::Form;
#[derive(FromForm)]
#[derive(Debug)]
struct user_input<'r> {
r#type: &'r str,
}
#[post("/create_account", data = "<task>")]
fn create_account(task: Form<user_input<'_>>) -> RawHtml<&'static str> {
println!("{:#?}",task);
return RawHtml(include_str!("templates/account.html"))
}
#[rocket::main]
async fn main() -> Result<(), rocket::Error> {
sql::setup();
let _rocket = rocket::build()
.mount("/", routes![index])
.mount("/", routes![create_account])
// .mount("/confirmation", routes![confirmation])
.launch()
.await?;
Ok(())
}
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>Account Creation</h1>
<br>
<h2>User Details</h2>
<form action="/create_account" method="post">
<!-- name, age, email, phno, password -->
<label for="Name">Name (Enter Full Name)</label>
<br>
<input type="text" name="Name" id="Name" text="Name">
<br>
<label for="age">Age</label>
<br>
<input type="number" name="age" id="age">
<br>
<label for="email">BCBS outlook email</label>
<br>
<input type="email" name="email" id="email" text="BCBS outlook email">
<br>
<label for="phno">Phone Number</label>
<br>
<input type="tel" name="phno" id="phno">
<br>
<label for="password">Password</label>
<br>
<input type="password" name="password" id="password">
<br> <br>
<input type="submit" value="Create Account">
</form>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在输入元素中使用小写名称。
然后,您可以在Rust Code中使用这些字段。
Use lowercase names in your input elements.
You can then use those fields in your rust code.