我有一页的问题,有两个选项作为单选按钮。每个问题都有一个表单标签。现在我想通过php中的单个函数将用户检查的值存储到数据库中。怎么做呢?我的html代码如下:
<li>Question1 </li>
<br /><form>A.
<input type="radio" name="radio" id="1ARadioButton" value="1ARadioButton" />
<label for="1ARadioButton">Answer1</label><br /><br />
B.
<input type="radio" name="radio" id="1BRadioButton" value="1BRadioButton" />
<label for="1BRadioButton"> Answer2</label>
</form><br /><br />
<li>Question2</li>
<br /><form>A.
<input type="radio" name="radio" id="2ARadioButton" value="2ARadioButton" />
<label for="2ARadioButton">Answer1</label>
<br /><br />
B.
<input type="radio" name="radio" id="2BRadioButton" value="2BRadioButton" />
<label for="2BRadioButton">Answer2</label>
</form><br /><br />
<li>Question3</li>
<br /><form>A.
<input type="radio" name="radio" id="3ARadioButton" value="3ARadioButton" />
<label for="3ARadioButton">Answer1</label>
<br /><br />
B.
<input type="radio" name="radio" id="3BRadioButton" value="3BRadioButton" />
<label for="3BRadioButton">Answer2</label>
</form><br /><br />
你在所有的单选按钮上都有相同的名字,所以你只会得到最后一个选中的单选按钮的值。你将记录所有问题的相同答案(通常是C,对吗?)
元素id是而不是用于表单提交。它们仅用于DOM操作。在HTML表单中,只有name
和value
类型属性与表单提交过程相关。
你应该拥有的是:
Question 1:
<input type="radio" name="question1" value="option_A" />
<input type="radio" name="question1" value="option_B" />
Question 2:
<input type="radio" name="question2" value="option_A" />
<input type="radio" name="question2" value="option_B" />
etc...
至于将它们存储在数据库中,这与在数据库中存储任何其他表单数据相同。获取无线电的值与$_POST['question1']
或其他,并做通常的转义/查询建筑/插入。