我创建了一个muilti-page PHP表单,将结果插入MySQl数据库并且工作正常,我唯一的问题是当我尝试返回时,我得到'Confirm Form Resubmission''ERR_CACHE_MISS'错误页面。
Php不是我最强的语言,需要一些修复它的帮助。
它是一个20页的调查,每个页面都有单选按钮,可以选择1到10个。
我的代码示例如下,任何建议都会很棒。
干杯
<?php session_start();
$_SESSION['answer_04'] = $_POST['answer_04'];
?>
<form action="peem-05.php" id="peem" method="post">
<ul class="answers">
<li class="group1"><input type="radio" id="radio1" name="answer_05" value="1" data-msg-required="Please choose an answer bewteen 1 and 10" required><label for="radio1"><br>1</label></li>
<li class="group1"><input type="radio" id="radio2" name="answer_05" value="2" required><label for="radio2"><br>2</label></li>
<li class="group2"><input type="radio" id="radio3" name="answer_05" value="3" required><label for="radio3"><br>3</label></li>
<li class="group2"><input type="radio" id="radio4" name="answer_05" value="4" required><label for="radio4"><br>4</label></li>
<li class="group3"><input type="radio" id="radio5" name="answer_05" value="5" required><label for="radio5"><br>5</label></li>
<li class="group3"><input type="radio" id="radio6" name="answer_05" value="6" required><label for="radio6"><br>6</label></li>
<li class="group4"><input type="radio" id="radio7" name="answer_05" value="7" required><label for="radio7"><br>7</label></li>
<li class="group4"><input type="radio" id="radio8" name="answer_05" value="8" required><label for="radio8"><br>8</label></li>
<li class="group5"><input type="radio" id="radio9" name="answer_05" value="9" required><label for="radio9"><br>9</label></li>
<li class="group5"><input type="radio" id="radio10" name="answer_05" value="10" required><label for="radio10"><br>10</label></li>
</ul>
<a href="javascript:history.back(1)"><img class="backButton" src="img/back-button.png"/></a>
<button class="rightArrow" name="submit" type="submit" value="Next" ><img class="backButton" src="img/next-button.png"/></button>
</form>
答案 0 :(得分:0)
不是使用javascript:history.back(1)
,而是最好将数据保存到PHP Session,然后返回意味着重新访问同一页面。
[save.php]
$step = $_POST['formStep'];
$formData = $_POST['Form'][$step]; // all values from form
$formID = $_POST['formID']; // validate
if ($formData && $step) {
if ($step < $maxSteps) {
$_SESSION[$formID][$step] = $formData;
$this->showStep($step++);
} else {
$this->db->saveForm($_SESSION[$formID]);
}
}
[showStep.php]
$step = isset($_REQUEST['step']) ? $_REQUEST['step'] : 1;
$formID = isset($_SESSION['formID']) ? $_SESSION['formID'] : rand(0, 99);
$formData = isset($_SESSION[$formID][$step]) ? $_SESSION[$formID][$step] : [];
?>
<form action="save.php?step=<?= $step; ?>">
<input type="hidden" value="<?= $formID; ?>" name="formID"/>
<?php switch ($step): ?>
<?php case 1: ?>
<input type="radio" id="radio10" name="Form[<?= $formID; ?>][<?= $step; ?>][answer_05]" value="10" <?= isset($formData['answer_05']) ? 'checked="checked"' : ''; ?>/>
<?php case 20: ?>
...
<?php break; ?>
</form>
<a href="showStep.php?step=<?= $step - 1; ?>">Back</a>
答案 1 :(得分:0)
请注意,您必须重命名peem-x.php
文件,删除零以使该代码有效。我评论了我的变化,也许这会对你有帮助。
<?php
session_start();
$page = basename(__FILE__); // get current filename
$page = preg_replace("/[^0-9]/","",$page); // remove everything except numbers
$page_last = "peem-".($page-1).""; // calculate last page name
$page_now = "peem-".$page.""; // current page name
for ($i = 0; $i < count($_POST['answer']); $i++) { // loop for total count of answers selected
$_SESSION['page'.$page_now.'']['answer'.$i.''] = $_POST['answer'.$i.'']; // assign session to answer
}
?>
<form action="" id="peem" method="post">
<ul class="answers">
<li>Please choose an answer between 1 and 10</li>
<?php
for ($i = 1; $i < 10; $i++) {
// check every loop if it's the current page and current answer that has been checked before
// if so, set a variable to mark it as checked
if ($_SESSION['page'] == $page_now && $_SESSION['page']['answer'.$i.''] == 'page'.$page_now.'answer'.$i.'')
{
$checked = " checked"; // create var to check current answer in loop
}
echo '<li class="group1"><input type="radio" id="radio1" name="answer' . $i . '" value="1"'.$checked.'><label for="radio' . $i . '"><br>' . $i . '</label></li>';
unset($checked); // unset $checked var for next loop to avoid checking unchecked radio buttons
}
?>
</ul>
<a href="-<?php echo $page_last; ?>.php"><img class="backButton" src="img/back-button.png"/></a>
<button class="rightArrow" name="submit" type="submit" value="Next" ><img class="backButton" src="img/next-button.png"/></button>
</form>
<?php
// test session and post variables
echo "<pre>";
print_r($_POST);
print_r($_SESSION);
echo "</pre>";
?>