我有一个幻灯片演示,用户可以单击其中带有箭头的按钮来指向下一个和上一个。当用户单击箭头时,我要在其中保存页面名称,因此它将其重定向到正确的页面。我还想存储一个数字,以便当用户单击上一个或下一个时,该整数将保存在userTakingModule
下的正确表checkPoint
中。
执行此操作的最佳方法是什么,即在button
标签内使用form
标签?我粘贴了到目前为止用户尝试单击两个箭头之一时发生的两种方法:
a。)通过使一个箭头的action
中的page1.html和另一个箭头的page3.html,使用户返回一页
b。)单击按钮后,将值保存到userTakingModule
表中。
html尝试1。)
<div class="arrow-container">
<form style="display: inline" action="dashboard.php" method="post">
<button value="1"><i class="fas fa-arrow-circle-left"></i></button>
</form>
<form style="display: inline" action="slide2.php" method="post">
<button value="3"><i class="fas fa-arrow-circle-right"></i></button>
</form>
</div>
html尝试2。)
<div>
<form action ="dashboard.php" method = "POST"> <!-- However I may need this page link to change depending on whether they click forward or back -->
<label>Competition Categories</label>
<select name="checkPoint">
<option value="">Select</option>
<option value="1">Previous</option>
<option value="3">Next</option>
</select>
</fieldset>
<button name="save_check_point" type="submit" type="button">View</button>
</form>
</div>
到目前为止,我的查询是这样:
<?php
// The module ID will always be 5, as they are in the Stress & Anxiety slideshow which has an ID of 5 in the table.
$idUsers = $_SESSION['id'];
$ModuleID = 5;
$checkPoint = $_POST['checkPoint'];
// Preparing and binding my SQL query which inserts a checkpoint number into the table
$stmt = $conn->prepare ("INSERT INTO `userTakingModule` (`userTakingModuleID`, `idUsers`, `ModuleID`, `checkPoint`) VALUES (NULL, ?, ?, ?)");
$stmt->bind_param("iii", $idUsers, $ModuleID, $checkPoint);
$stmt->execute();
?>
我确实已经为此苦苦挣扎了一段时间了,所以对此的任何帮助都是非常棒的,谢谢您。
答案 0 :(得分:1)
我建议一个更简单的方法。如果您的页面具有相同的名称格式:'page',后跟页码,并以文件扩展名结尾(例如,page2.html);那么您就无需使用表单,因为PHP可以为您提供:
如果您不需要可靠的消息来源,那么从安全性角度来说,可以使用$_SERVER['SCRIPT_NAME']
和$_SERVER['HTTP_REFERER']
来获取信息,并使用简单的定位符来链接页面。
这是一个PHP代码,必须在所有幻灯片页面中,您的PHP代码(看起来不错)之前以及HTML按钮之前插入:
<?php
//A simple function to parse script names and get your page number
function page_number($script_name){
//Remove the first '/' character and the word 'page' to get '#.php'
$number = substr($script_name,1+strlen('page'));
//Remove the extension part to get just the page number
$number = substr($number,0,strpos($number,'.')); //
return $number;
}
//Get the page script name, like '/page#.php'
$script_name = $_SERVER['SCRIPT_NAME'];
$n = page_number($script_name);
//Get the referer URL
$referer = $_SERVER['HTTP_REFERER'];
//Get the script name of that URL
$referer = substr($referer,strrpos($referer,'/'));
$checkPoint = page_number($referer);
?>
此PHP代码结束时,您已准备好在PHP脚本中使用$checkPoint
整数。还有$n
变量可在按钮的HTML链接中使用。
这是上一个和下一个按钮的HTML:
<div class="arrow-container">
<a href="page<?php echo($n-1); ?>.php">
<i class="fas fa-arrow-circle-left"></i>
</a>
<a href="page<?php echo($n+1); ?>.php">
<i class="fas fa-arrow-circle-right"></i>
</a>
</div>
PS:请注意,页面需要使用 .php 扩展名而不是 .html 扩展名。