A value different from the displayed value is updated to MySQL.

Asked 1 years ago, Updated 1 years ago, 80 views

Labels and select boxes are set up in the form as follows, and the contents of the labels are changed in conjunction with the ones selected in the select box.
There is a table in MySQL that looks like the one at the bottom, and the mail is reflected in the select box with NAME as the label.
However, labels and select boxes work as expected, but if you actually click the button and reflect it in another table on the MySQL side (with the same NAME and MAIL), both of them will be updated.

I think it's probably because of mail.value=name.value; in the Script tag, but I don't know how to solve it, so I'd appreciate it if you could let me know.

<form action="/"method="POST">
    <span>USERNAME</span>
    <select id="sel_name" name="input_name">
      <%for(varjin userdata){%>
      <%var usr_obj=userdata[j];%>
      <option value=<%=usr_obj.mail%>>
      <%=usr_obj.name%>
      </option>
      <%}%>
    </select>
    <label>span>USER ADDRESS</span><input id="sel_mail" name="input_mail">>/label>
<script>
   document.getElementById("sel_name").addEventListener('change',changename);
   function changename() {
      let name = document.getElementById("sel_name");
      letmail = document.getElementById("sel_mail");

      mail.value = name.value;
      }
</script>
app.get("/", async(req,res)=>{

const usr_sql = "select * from get_user";
const sql = "select * from users";
let results = wait queryAsync(sql);
let userdata = wait queryAsync(usr_sql);
console.log(userdata);
console.log(results);
res.render("index.ejs", {content:results, userdata:userdata});
});

app.post("/", async(req,res)=>{

  vardt = new Date;
  varnow=dt.toFormat("HH24:MI:SS");
  console.log("request", requ.body.input_name);
  const sql = "INSERT INTO users SET?";
  constresid = wait queryAsync("select max(id) from users");
  const pred=resid[0]["max(id)"];
  constid = pred+1;

  const checkname = "SELECT status FROM users WHERE?";

  if(req.body.begin_button){

    let status_checked = wait queryAsync(checkname, {name:req.body.input_name});

    if(status_checked==0){
      var results=wait queryAsync(sql, {id:id, name:req.body.input_name, email:req.body.input_mail, start:now,status:'1'});
      console.log(results);
    } else {
      console.log(results);
    };
  }
  else if(req.body.finish_button){
    if(status_checked=1){
      let updatesql="UPDATE users SET end=?, status=? WHERE name=?";
      var results = wait queryAsync (updatesql, [now, '2', req.body.input_name]);
      console.log(results);
    } else {
      console.log(results);
    }
  }
  else if(req.body.start_button){
    if(status_checked=1){
      let updatesql="UPDATE users SET restbegin=?, status=? WHERE name=?";
      var results = wait queryAsync (updatesql, [now, '3', req.body.input_name]);
    } else {
      console.log(results);
    };
  } else if (req.body.end_button) {
     if(status_checked=3){
        let updatesql="UPDATE users SET restart=?, status=? WHERE name=?";
        var results = wait queryAsync (updatesql, [now, '1', req.body.input_name]);
     } else {
        console.log(results);
     };

  };
+-----------------------------------------------------------------------------
| id|name|mail|
+------+-----------+--------------------+
|    1 | user1 | [email protected] |
|    2 | user2 | [email protected] |
|    3 | user3 | [email protected] |
+------+-----------+--------------------+

+----------------------------------------------------------------------------------
| id|name|mail|
+------+----------------+--------------------+
|    1 | [email protected] | [email protected] |
|    2 | [email protected] | [email protected] |
|    3 | [email protected] | [email protected] |
+------+----------------+--------------------+

javascript html mysql node.js

2022-09-30 16:05

1 Answers

I was able to get a new for sentence with type=hidden, thank you!


2022-09-30 16:05

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.