我想以这种格式使用多维数组:value [n] [],其中n是问题编号。使用这个新设置,您应该得到以下复选框字段:
<input type="checkbox" value="A" name="value[1][]">
<input type="checkbox" value="B" name="value[1][]">
<input type="checkbox" value="A" name="value[2][]">
<input type="checkbox" value="C" name="value[2][]">
<input type="checkbox" value="E" name="value[2][]">
请注意,所选值是在value属性中编码的。 name属性仅包含值所属的问题。
以上输入的内容是:
question 1: answer: A
question 1: answer: B
question 2: answer: A
question 2: answer: C
question 2: answer: E
我想将这些详细信息插入下面的“问题”和“答案”数据库表中:
问题表:
SessionId QuestionId
MUL 1
MUL 2
答案表:
AnswerId (auto) SessionId QuestionId Answer
1 MUL 1 A
2 MUL 1 B
3 MUL 2 A
4 MUL 2 C
5 MUL 2 E
现在我尝试编写下面的mysqli / php代码将这些值插入到数据库中但是我收到错误并且在想要实现我想要实现的目标时失败了。我需要帮助才能在相关表格中正确插入正确的值。
下面是php / mysqli代码:
var_dump($_POST);
$i = 0;
$c = count($_POST['numQuestion']);
for($i = 0; $i < $c; $i++ ){
/*
switch ($_POST['gridValues'][$i]){
case "3":
$selected_option = "A-C";
break;
case "4":
$selected_option = "A-D";
break;
case "5":
$selected_option = "A-E";
break;
default:
$selected_option = "";
break;
}
*/ needed later on when I insert grid values
$results = $_POST['value'];
foreach($results as $id => $value) {
$answer = $value;
$questionsql = "INSERT INTO Question (SessionId, QuestionId)
VALUES (?, ?)";
$sessid = $_SESSION['id'] . ($_SESSION['initial_count'] > 1 ? $_SESSION['sessionCount'] : '');
if (!$insert = $mysqli->prepare($questionsql)) {
// Handle errors with prepare operation here
}
$insert->bind_param("si", $sessid, $id);
$insert->execute();
if ($insert->errno) {
// Handle query error here
}
$insert->close();
$insert->insert_id;
foreach($value as $answer) {
$answersql = "INSERT INTO Answer (SessionId, QuestionId, Answer)
VALUES (?, ?, ?)";
if (!$insertanswer = $mysqli->prepare($answersql)) {
// Handle errors with prepare operation here
}
$insertanswer->bind_param("sis" $sessid, $lastID, $answer);
$insertanswer->execute();
if ($insertanswer->errno) {
// Handle query error here
}
$insertanswer->close();
}
}
}
var_dump($_POST)
输出如下:
array(3) {
["numQuestion"]=> array(2) {
[0]=> string(1) "1"
[1]=> string(1) "2"
}
["submitDetails"]=> string(14) "Submit Details" ["value"]=> array(4) {
["answerARow"]=> string(2) "on"
["answerCRow"]=> string(2) "on"
["answerBRow"]=> string(2) "on" ["answerERow"]=> string(2) "on"
}
}
以下是我收到的错误以及每个错误链接到的代码行:
警告:为第252行的/.../中的foreach()提供的参数无效
foreach($value as $answer) {
警告:mysqli_stmt :: execute():( 23000/1062):重复输入 'MUL-0'用于第242行的/.../中的'PRIMARY'键
上述错误表明没有插入任何问号,因为它会一直显示为“0”
答案 0 :(得分:0)
所以看起来你要做的主要事情是绕过无法去皮肤的复选框。我之前做过这个。我这样做的方法是使用绑定到隐藏复选框的跨度。
我使用了跨度,因为我不确定表单字段中的按钮是否会在提交表单时发布值。我知道跨度不会。这样可以保证只提交您的复选框值,而不是“掩盖”值。
在你的jsFiddle中,在updateAnswer()
函数中,你有这个代码:
if (!bDisableAppend) {
// append those values to the form
var input = '<input type="checkbox" id="' + hid + '" name="value[' + id + ']" checked /><label for="' + hid + '">' + value + '</label>';
_oCurrAnswerContainer.append(input);
}
在该代码中,您将复选框的name
属性设置为value[answerARow]
。这就是你的问题所在。
<强> jsFiddle 强>
<强> JS / jQuery的:强>
var options = ["A", "B", "C", "D", "E", "F", "G", "H"];
// this will bind the event handler to the document element, so that even when a new element is created,
// the event will be automatically bound to it.
$(document).on("click", "span.checkbox", function() {
$("#chk"+ $(this).attr("id")).click();
$(this).toggleClass("unselected selected");
});
$("#btnAddQuestion").click(function() {
var questionNum = $("#tblQuestions tbody tr").length+1;
var cell = $(document.createElement("td")).addClass("optAns").html("Answer:<br />");
// loop through the options and add them to the cell
for (var i in options)
{
$(cell).append(
// the front-end overlay for the checkbox
$(document.createElement("span"))
.addClass("checkbox unselected")
.attr("id", "Value"+ questionNum + options[i])
.html(options[i])
).append(
// the hidden checkbox that will post the actual value
$(document.createElement("input"))
.attr("type", "checkbox")
.addClass("hidden")
.attr("id", "chkValue"+ questionNum + options[i])
.attr("name", "value["+ questionNum +"][]")
.attr("value", options[i])
);
}
// create a new table row, append the "option and answer" cell
// and question number cell, and append it to the table body
$(document.createElement("tr"))
.append(cell)
.append($(document.createElement("td")).addClass("questionNum").html(questionNum))
.appendTo("#tblQuestions tbody");
});
<强> HTML:强>
<div id="detailsBlock">
<button id="btnAddQuestion">Add Question</button>
</div>
<hr />
<div id="details">
<table id="tblQuestions">
<thead>
<tr>
<th>Option and Answer</th>
<th>Question No</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
答案 1 :(得分:0)
您当前的代码会生成此代码(请注意错误名称属性):
<input type="button" onclick="btnclick(this, 1);" style="display:inline-block;" class="answerBtns answers answerBtnsOff" name="value[answerF]" value="F" id="answerHRow">
请注意,名称属性为value[answerF]
,而不是您在问题中写的value[1][]
。
更新您的javascript并将$newBtn
行更改为:
var $newBtn = $(("<input class='answerBtnsRow answers' type='button' style='display:%s;' onclick='btnclick(this, " + gQuestionIndex + ");' />").replace('%s', $this.is(':visible') ? 'inline-block' : 'none')).attr('name', "value[" + gQuestionIndex + "][]").attr('value', $this.val()).attr('class', $this.attr('class')).attr('id', $this.attr('id') + 'Row');