记录用户首选项并将其写入数据库

时间:2015-12-09 19:57:00

标签: javascript html node.js express sqlite

我需要通过从五个单选按钮(每个项目5个)中选择一个来标记五个项目的用户首选项,这些按钮将被标记为“第一选择”,“第二选择”等...但我不知道如何在服务器上记录这个(node.js express)。

如果我不清楚自己,在一个理想的世界里,我会有类似......

//Student Project Preferences
app.get('/api/studentPrefs', function (req, res) {
    console.log("Route API/studentPrefs");
    console.log(req.query.name);
    var project_1 = req.query.project_1,
        project_2 = req.query.project_2,
        project_3 = req.query.project_3,
        project_4 = req.query.project_4,
        project_5 = req.query.project_5;
    
SUPER-pseudo-code...
----------
if(project_1 || project_2 || project_3 || project_4 || project_5 == 'choice_1') {
      INSERT INTO db (choice_1) VALUES (relevant topic)
    }
    else if(project_1 || project_2 || project_3 || project_4 || project_5 == 'choice_2') {
    ....etc
    }
});

<fieldset>
<legend>&nbsp;Please choose your prefered project&nbsp;</legend>
<form id="studentPrefsForm" action="api/studentPrefs" method="get">
    <label for="projectChoice_1">Project Choice 1</label>
    <input type="radio" name="project_1" value="choice_1" />
    <input type="radio" name="project_1" value="choice_2" />
    <input type="radio" name="project_1" value="choice_3" />
    <input type="radio" name="project_1" value="choice_4" />
    <input type="radio" name="project_1" value="choice_5" />
    <br />
    <label for="projectChoice_2">Project Choice 2</label>
    <input type="radio" name="project_2" value="choice_1" />
    <input type="radio" name="project_2" value="choice_2" />
    <input type="radio" name="project_2" value="choice_3" />
    <input type="radio" name="project_2" value="choice_4" />
    <input type="radio" name="project_2" value="choice_5" />
    <br />
    <label for="projectChoice_3">Project Choice 3</label>
    <input type="radio" name="project_3" value="choice_1" />
    <input type="radio" name="project_3" value="choice_2" />
    <input type="radio" name="project_3" value="choice_3" />
    <input type="radio" name="project_3" value="choice_4" />
    <input type="radio" name="project_3" value="choice_5" />
    <br />
    <label for="projectChoice_4">Project Choice 4</label>
    <input type="radio" name="project_4" value="choice_1" />
    <input type="radio" name="project_4" value="choice_2" />
    <input type="radio" name="project_4" value="choice_3" />
    <input type="radio" name="project_4" value="choice_4" />
    <input type="radio" name="project_4" value="choice_5" />
    <br />
    <label for="projectChoice_5">Project Choice 5</label>
    <input type="radio" name="project_5" value="choice_1" />
    <input type="radio" name="project_5" value="choice_2" />
    <input type="radio" name="project_5" value="choice_3" />
    <input type="radio" name="project_5" value="choice_4" />
    <input type="radio" name="project_5" value="choice_5" />
    <br />
    <br />
    <input type="submit" value="Send my Choices">
</form>
</fieldset>

很抱歉,如果上面的JS解释有点太伪,我希望这是可以理解的

我知道我的'理想'解决方案存在一些问题,例如IF语句没有返回哪个项目值为'choice_1 / 2/3/4/5',以便我可以将项目名称写入相关的choice_x数据库中的列,但此时我会采取任何措施。

如前所述,我是一年级学生,所以如果你能看到更好的方法,我会非常乐于接受建议或批评,并再次感谢。

1 个答案:

答案 0 :(得分:-1)

即使您的JS代码可以优化,它也会很好。 你写的是页面的客户端。根据您使用的平台/语言,您需要将这些值传递到将进行保存的服务器端。 让我们知道您在服务器端使用的语言 - php,.NET等。