如何从火箭中的HTML表格中接收发布数据?

发布于 2025-02-12 11:37:20 字数 3030 浏览 0 评论 0原文

我正在在火箭网中构建一个简单的登录流,该流程详细介绍了用户的详细信息,并将其存储在数据库中。

但是,我似乎无法弄清楚如何从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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倒带 2025-02-19 11:37:20

在输入元素中使用小写名称。

    <input type="text" name="name" id="name">

然后,您可以在Rust Code中使用这些字段。

#[derive(FromForm)]
struct CreateAccount {
    name: String,
}

#[post("/create_account", data = "<account>")]
fn create_account(account: Form<CreateAccount>) -> RawHtml<&'static str> {
    println!("{:#?}",account.name);
    return RawHtml(include_str!("templates/account.html"))
}

Use lowercase names in your input elements.

    <input type="text" name="name" id="name">

You can then use those fields in your rust code.

#[derive(FromForm)]
struct CreateAccount {
    name: String,
}

#[post("/create_account", data = "<account>")]
fn create_account(account: Form<CreateAccount>) -> RawHtml<&'static str> {
    println!("{:#?}",account.name);
    return RawHtml(include_str!("templates/account.html"))
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文