我有一个调查的基本表单(包含单选按钮和几个隐藏字段)。表单提交另一个PHP文件,该文件保存当前页面的答案,然后重定向回调查(通过GET传递下一页)。这是使用标题重定向完成的。
偶尔会有不同的用户在尝试保存答案时看到错误。这是因为$ _POST为空。每个失败的请求都显示该页面是使用GET而不是POST请求的,并且$ _REQUEST变量为空,除了一些cookie /会话数据。
我不知道下一步该寻找什么。有什么想法吗?
UPDATE:我的数据库显示答案已保存,并且在表单发布后,以GET再次点击该页面然后失败(而不是重定向到上一页)。
修改
这是一些代码。来自表单页面的Javascript:
function nextPage(page,question_count)
{
if (checkForm(question_count))
{
btn = document.getElementById('nextbutton');
btn.disabled = true;
gotoPage(page);
}
}
function prevPage(page)
{
btn = document.getElementById('prevbutton');
btn.disabled = true;
gotoPage(page)
}
function completeSurvey(question_count)
{
if (checkForm(question_count))
{
frm = document.surveynav;
frm.action = 'act_complete_survey.php';
frm.submit();
return true;
}
}
function gotoPage(page)
{
action = document.getElementById('gotopage');
action.value = page;
document.surveynav.submit();
return true;
}
function checkForm(question_count)
{
frm = document.surveynav;
answer_count = 0;
for (var i = 0; i < frm.elements.length; i++)
{
if ((frm.elements[i].name.indexOf('q_') > -1) && frm.elements[i].checked)
{
answer_count++;
}
}
if (answer_count < question_count)
{
alert('Please answer all questions on this page before proceeding.');
return false;
}
return true;
}
function openSurveyWin()
{
window.open("survey.php", "surveywin", "location=0, toolbar=0, directories=0, status=0, menubar=0, scrollbars=0, resizable=0, fullscreen=0")
}
来自表单的HTML:
<form action="act_savestate.php" name="surveynav" id="surveynav" method="post">
<input type="hidden" name="gotopage" id="gotopage" value="" />
<div class="question_holder"><div class="question">1 . Some question text here.</div><div><div style="padding-left:15px;"><input type="radio" name="q_1" value="1" id="a_1" /><label for="a_1">Strongly Disagree</label><br />
<input type="radio" name="q_1" value="2" id="a_2" /><label for="a_2">Disagree</label><br />
<input type="radio" name="q_1" value="3" id="a_3" /><label for="a_3">Neither Agree nor Disagree</label><br />
<input type="radio" name="q_1" value="4" id="a_4" /><label for="a_4">Agree</label><br />
<input type="radio" name="q_1" value="5" id="a_5" /><label for="a_5">Strongly Agree</label><br />
</div></div></div>
<input type="hidden" name="questions" id="questions" value="1,10,19,28,37,46,55,64,73,2,11,20,29,38,47,56,65,74,3,12" />
<input type="button" id="nextbutton" name="nextbutton" value="Next >>" onclick="nextPage(2,20);" />
</form>
来自act_savestate.php的代码:
$gotopage = isset($_POST['gotopage']) ? (int)$_POST['gotopage'] : 1;
if (!isset($_POST['questions'])) {
// No post
die("No questions included");
}
$questions = explode(',',$_POST['questions']);
foreach($questions as $questionid)
{
if (isset($_POST['q_' . $questionid]))
{
$survey->setAnswer($survey_id,$questionid,$_POST['q_' . $questionid]);
}
}
header("Location: survey.php?page=$gotopage");
die();