T.C*_*T.C 6 html php sql mysqli
我有一个幻灯片放映,用户可以单击带有箭头的按钮,指向下一个和上一个。当用户单击箭头时,我要在其中保存页面名称,因此它将其重定向到正确的页面。我还想存储一个数字,以便当用户单击上一个或下一个时,该整数将保存在正确的表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>
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
我到目前为止的查询是这样的:
<?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();
?>
Run Code Online (Sandbox Code Playgroud)
我确实已经为此苦苦挣扎了一段时间,因此对此的任何帮助都将非常棒,在此先感谢您。
我建议采用一种更简单的方法。如果您的页面具有相同的名称格式:“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);
?>
Run Code Online (Sandbox Code Playgroud)
当这段 PHP 代码结束时,您就$checkPoint可以在 PHP 脚本中使用您的整数了。以及$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>
Run Code Online (Sandbox Code Playgroud)
PS:请注意,页面需要.php扩展名才能工作,而不是.html扩展名。
| 归档时间: |
|
| 查看次数: |
70 次 |
| 最近记录: |