将选定的按钮连同输入数据一起发送到 MongoDB

问题描述 投票:0回答:0

所以在注册表单上我有通常要求的输入,但我还有两个按钮可以根据所选的按钮切换突出显示,一个用于个人,一个用于企业。我怎样才能将这个选择与输入数据一起传递给 mongoDB?

我所拥有的例子:

const [active, setActive] = useState('');

const handleClick = (e) => {
  setActive(e.target.id);
}

<form onSubmit={handleSubmit}>
    <button
          key={1}
          className={active === "1" ? "active" : undefined}
          id="personal"
          onClick={handleClick}
        >
          Personal
        </button>
        <button
          key={2}
          className={active === "2" ? "active" : undefined}
          id="business"
          onClick={handleClick}
        >
          Business
        </button>

    *rest of code*
</form>

还有:

const handleSubmit = async (e) => {
e.preventDefault();
// if button enabled with JS hack
const v1 = ZIP_REGEX.test(zipCode);
if (!v1) {
  setErrMsg("Invalid Entry");
  return;
}

fetch("http://localhost:4000/customerInfo", {
  method: "POST",
  crossDomain: true,
  headers: {
    "Content-Type": "application/json",
    Accept: "application/json",
    "Access-Control-Allow-Origin": "*",
  },
  body: JSON.stringify({
    aline1,
    aline2,
    city,
    state,
    zipCode,
  }),
})
  .then((res) => res.json())
  .then((data) => {
    console.log(data, "userAddress");
    if (data.status === "OK") {
      setSuccess(true);
      return;
    } else {
      alert("Error")
    }
  });

感谢您的宝贵时间!

reactjs mongodb button
© www.soinside.com 2019 - 2024. All rights reserved.