在使用form.php从process.php 上的$laws数组检索特定文章之前,我想知道如何验证form.php中的数据
(form.php)
<form action="process.php" method="get">
Group:
<select name="group">
<option value="group1">Group1</option>
<option value="group2">Group2</option>
</select>
Chapter:
<input type="text" name="chapter">
Article:
<input type="text" name="article">
<input type="submit" value="Go to Article">
</form>
(process.php)
<?php
session_start();
$laws=array(
"group1"=>array(
"1"=>array(
"1"=>"This is article (1) in chapter (1) of (group1)",
"2"=>"This is article (2) in chapter (1) of (group1)",
"3"=>"This is article (3) in chapter (1) of (group1)",
),
"2"=>array(
"1"=>"This is article (1) in chapter (2) of (group1)",
"2"=>"This is article (2) in chapter (2) of (group1)",
"3"=>"This is article (3) in chapter (2) of (group1)",
),
),
"group2"=>array(
"1"=>array(
"1"=>"This is article (1) in chapter (1) of (group2)",
"2"=>"This is article (2) in chapter (1) of (group2)",
"3"=>"This is article (3) in chapter (1) of (group2)",
),
"2"=>array(
"1"=>"This is article (1) in chapter (2) of (group2)",
"2"=>"This is article (2) in chapter (2) of (group2)",
"3"=>"This is article (3) in chapter (2) of (group2)",
),
)
);
$_SESSION['group'] = $_GET['group'];
$_SESSION['chapter'] = $_GET['chapter'];
$_SESSION['article'] = $_GET['article'];
$group = $_SESSION['group'];
$chapter = $_SESSION['chapter'];
$article = $_SESSION['article'];
// Echo Article from Laws Array
echo $group . " chapter" . $chapter . " article" . $article . "<br/>";
echo $laws[$group][$chapter][$article];
?>
<br/>
<a href="sample.php">PREVIOUS Articles</a> <a href="sample.php">NEXT Articles</a>
问题1
在使用$_GET变量中的数据设置$_SESSION值之前,如何使用以下方案验证表单中的$_GET值?
Data in CHAPTER and ARTICLE input forms:
are not empty;
are numbers;
are not less than or greater than the existing array Chapters and Article numbers;
do not have spaces;
do not have decimals;
do not have letters;
do not have negative, positive and; or other signs;
问题2
我应该在NEXT Article和PREVIOUS Article链接中添加哪些PHP代码,以在当前响应的文章之后响应下一篇文章?
我应该如何将此代码添加到链接中;
由于NEXT article和PREVIOUS article链接无法访问特定章节中不再存在的数组,因此当最后一篇文章或第一篇文章被呼应时,应该对页面执行什么操作,以防止错误被呼应。
谢谢!
问题1:你试过intval吗?http://php.net/manual/en/function.intval.php所以,按照你想要的顺序,
if(isset($_GET['group']) && !empty($_GET['group']) && is_int($_GET['group'])){
$groupval = intval($_GET['group']);
$_SESSION['group'] = ($groupval > 0)? $groupval : 1; //A group cannot be 0
}
你也可以在章节和文章中使用类似的语句。
问题2:您只需要使用返回给您的get值,加1和减1。例如:
echo '<a href="process.php?group=' . $group . '&article=' . ($article+1) . '">Next Article</a>'
注意,如果这是最后一篇文章,你必须写下支票,然后进入下一组/接下来的任何内容。