如何从表中获取内容并使用表单发布到表中?

时间:2017-11-18 16:39:14

标签: javascript node.js forms postgresql express

所以在我的项目中,我试图从表格行中获取内容并使用表单发送回后端,但我无法弄清楚如何操作。

这是我的HTML表单代码:

<form method="POST" action="/add">
    <div class="container">
        <div id="class_form">
            <h3>Choose your course</h3>
            <fieldset>
                <select name="institution" id="institution" onchange="getInstTermVal(event)">
                    <option value="" selected="selected">Choose college...</option>
                    <option value="BAR01">Baruch College</option>
                    <option value="QNS01">Queens College</option>
                </select>
            </fieldset>

            <fieldset>
                <select id="term" name="term" onchange="getInstTermVal(event)">
                    <option value="" selected="selected">Choose Term...</option>
                    <option value="1179" >Fall Semester 2017</option>
                    <option value="1182" >Spring Semester 2018</option>
                </select>
            </fieldset>

            <fieldset>
                <select id="subject" name="subject">
                    <option value="" selected="selected">Choose Subject...</option>
                </select>
            </fieldset>

            <fieldset>
                <input type="number" id="course_num" name="course_num" placeholder="Course Number">
            </fieldset>

            <fieldset>
                <button type="button" id="submit_class" onclick=getSpecificCourse(event)>Submit</button>
            </fieldset>
        </div>
    </div>

    <div class="table-responsive-vertical" id="class_table">
        <table id="table" class="table table-hover">
            <thead>
                <tr>
                    <th>Class Number</th>
                    <th>Day & Time</th>
                    <th>Location</th>
                    <th>Instructor</th>
                    <th>Select</th>
                </tr>
            </thead>
            <tbody id="table_body">

            </tbody>
        </table>
    </div>
</form>

这是我的JS代码,我附加到table_body:

    function getSpecificCourse(event){
            for(let secKey in classes[key]){
                        if(classes[key].hasOwnProperty(secKey)) {
                            let classNmbr = classes[key][secKey].Class;
                            let dayTime = classes[key][secKey]['Days & Time'];
                            let room = classes[key][secKey].Room;
                            let instructor = classes[key][secKey].Instructor;
                            document.getElementById("table_body").insertRow(-1).innerHTML = `<td class="classNmbr">${classNmbr}</td>` +
                                    `<td>${dayTime}</td>` +
                                    `<td>${room}</td>` +
                                    `<td>${instructor}</td>` +
                                    `<td><button type="submit" class="submit_cart">Add to cart</button></td>`;
                        }
                    }
                }
            }
        });
    }

我试图获得班级编号以及大学,学期和科目中的价值观,但是为了做到这一点,我只是说req.body.institution或req.body.term但我不会&#39;我知道当他们点击按钮时我如何从表格行调用类号。

这是来自express.js的片段,我尝试将其添加到数据库中:

app.post('/add', (req, res) => {
    db.query("INSERT INTO classes (institution, term, subject, topic, class_num, phone, carrier) VALUES ($1, $2, $3, $4, $5, $6, $7)",
        [req.body.institution, req.body.term, req.body.subject, req.body.topic, req.body.class_num, req.body.phone, req.body.carrier], (error, response) => {
        if (error){
            return console.error('Error inserting into database: ', error);
        }
    });
});

如果有人可以帮助我,那将非常感激。提前谢谢!

0 个答案:

没有答案