如何从 textarea ejs 文件中获取数据库的数组?

How to get an array for the database from the textarea ejs file?

我有一个文本区域,用户可以在其中放置 5 个或更多电话号码或电子邮件。我需要将此文本区域中的信息作为数组保存到数据库(我使用 Mongoskin)。所以它必须是一个包含 5 个不同项目的数组。所以这就是我需要添加到数据库中的内容。

var phones = ["1number","2number","3number","4number","5number"];

当用户按下逗号、分号或开始新行时,这意味着其中一个数字已完成,因此必须将其视为数组项之一。我所做的是从文本区域获取值,但它只是发送到数据库的 1 个数组项。

var whatidonotneed = ["1number2number1number2number1number"];

我不需要的是一个只有 1 个项目的数组,而我需要 5 个单独的数组文件。这样我就可以操作它们了。

这是我的 EJS 文件中的代码:

<div style="text-align: center;">

  <form action="/emailsid" method="POST">
    <textarea placeholder="Type your Emails Here" name="users" rows="6" cols="60">
    </textarea>
    <button>Submit</button>
  </form>

在我的 app.js 文件中:

app.post("/emailsid", function(req,res){
  db.collection('emails').insert({emails: [req.body.users]}, function(err,result) {
    if(err){
      console.log(err);
    } else{
      console.log(result);
      res.render("emailsid", {result: result});
    } 
  });
});

我只是不明白如何使这些数字成为单个数组项,以及如何说文本区域,以便它理解逗号或分号表示它是其他数组项之一。因为我得到的只是一个数组项,其中包含所有信息。

建立字段名称。 EJS 文件的更改:

<form name="recopiled" action="/emailsid" method="POST">
    <textarea placeholder="Type your Emails Here" name="users" rows="6" cols="60"></textarea>
    <input type='button' onClick="go(document.recopiled.users.value);" value="Submit" />
</form>

您必须将此添加到您的 app.js 并设置文本区域 phone 数字的分隔符。

function go(content) {

    delimiter=","   // comma but you can set any character here.

    phones=content.split(delimiter);

    //  now the phones of the textbox are in an array with separate values.

    phones.forEach(function(value, index) {   
        //  Here you can handle each item.
        //
        //  Example:
        document.write('item: ' + phones[index] + '<br>');    
    });

    //  you can submit the form from here, or whatever you want.

}

请看一下https://www.w3schools.com/js/js_arrays.asp