我有一個enquete。其中1頁看起來像這樣:後檢查後保留選定的值
<form method="POST">
<input type="hidden" value="true" id="x" name="x">
<table>
<b>How relevant where the topics for your current and/or future business?</b>
<hr />
<tr>
<input type="hidden" value="1" name="question1">
<td><input type="radio" name="answer1" value="1">1</td>
<td><input type="radio" name="answer1" value="2">2</td>
<td><input type="radio" name="answer1" value="3">3</td>
<td><input type="radio" name="answer1" value="4">4</td>
<td><input type="radio" name="answer1" value="5">5</td>
<td><input type="radio" name="answer1" value="6">6</td>
<td><input type="radio" name="answer1" value="7">7</td>
<td><input type="radio" name="answer1" value="8">8</td>
<td><input type="radio" name="answer1" value="9">9</td>
<td><input type="radio" name="answer1" value="10">10</td>
<td>
<textarea rows="4" cols="50" name="comment1"></textarea>
</td>
</tr>
</table>
<br /><br />
<table>
<b>How did you value the networking opportunity?</b>
<hr />
<tr>
<input type="hidden" value="2" name="question2">
<td><input type="radio" name="answer2" value="1">1</td>
<td><input type="radio" name="answer2" value="2">2</td>
<td><input type="radio" name="answer2" value="3">3</td>
<td><input type="radio" name="answer2" value="4">4</td>
<td><input type="radio" name="answer2" value="5">5</td>
<td><input type="radio" name="answer2" value="6">6</td>
<td><input type="radio" name="answer2" value="7">7</td>
<td><input type="radio" name="answer2" value="8">8</td>
<td><input type="radio" name="answer2" value="9">9</td>
<td><input type="radio" name="answer2" value="10">10</td>
<td>
<textarea rows="4" cols="50" name="comment2"></textarea>
</td>
</tr>
</table>
<input id="enquete_next" type="submit" name="Add" value="Next">
<?php
//If the form gets submitted, check if everything is okay.
if(isset($_POST['x'])){
$validcomment = false;
//validate if the answers are not empty. if they are empty go the the else statement.
if(!empty($_POST['answer1'])){
if(!empty($_POST['answer2'])){
$validcomment = true;
}else{
echo "Please fill in all the questions!" . "<br>";
}
}else{
echo "Please fill in all the questions!" . "<br>";
}
//If the form is filled in, and checked. Then do this!
if($validcomment){
insert_page1();
}
}
?>
</form>
以下代碼正在工作。所以當我填寫答案1時,請將答案2留空。我收到一條消息:請填寫所有問題。
但是,我希望表單保持其值。所以我只需填寫空的答案,而不是整個表格。
因爲現在,當它檢查。表格變空了,我必須重新填寫。
在谷歌搜索Core核心表單驗證PHP –
在提交之前先驗證表單 –
保存在sessionStorage或php會話中 – mplungjan