gpt4 book ai didi

javascript - 如何创建包含 php + Javascript 变量的 href 链接

转载 作者:行者123 更新时间:2023-11-28 00:14:28 25 4
gpt4 key购买 nike

我正在尝试使用密码保护一些 pdf 文件,使用 onclick 函数,如下面的代码所示

我在这里想要实现的目标:

如果用户插入了正确的 ID,脚本会为他提供 pdf 文件的相应 href 链接,其中应包含:

证书/First_Last_ID.pdf

其中“First”和“Last”是 php 变量,php 变量 ID = js var truePassword

例如。如果我点击 Jon Doe 的行并插入正确的密码 AAA0000

我得到以下链接:

window.location.href = '/certificates/Jon_Doe_AAA000.pdf';

实现这一点可能需要在链接名称中添加一些变量!

请多多指教

<html>
<body>
<?php
$con=mysqli_connect("localhost","x","y","z");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"SELECT * FROM Academy");



// Fetching Data from MySql
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['First'] . " " . $row['Last'] . "</td>";
echo "<td>" . $row['Program'] . "</td>";
echo "<td>" . $row['Date Accredited'] . "</td>";
echo "<td>" . $row['Grade'] . "</td>";
echo "<td>" . "<a href='javascript:void(0);' onclick='myFun(event);validate()'><img src='../SVG/certificate_logo.svg' /><span id='ID' style='display:none;'>" . $row['ID'] ."</span></a>" . "</td>";
echo "</tr>";

}

mysqli_close($con);
?>

<script>
var truePassword;
var password;

function myFun(e){

truePassword = (e.target.nextSibling.innerText);

}

function validate(){

password = prompt('Enter Your Course Reservation ID (Case Sensitive)', 'YYY0000');

if (password==truePassword) {

window.location.href = '/certificates/<?php echo $row['First']."_".$row['Last'] ?>.pdf'; //relative to domain

}else{
alert ("Wrong ID");
return;
}
}
</script>
</body>
</html>

最佳答案

将最后一个“td”更改为以下内容

echo "<td>" . "<a href='javascript:void(0);' onclick='myFun(event);validate(this.id)' id='" . $row['First'] . "_" . $row['Last'] . "'><img src='../SVG/certificate_logo.svg' /><span id='ID' style='display:none;'>" . $row['ID'] ."</span></a>" . "</td>";

试试这个

<script>
var truePassword;
var password;

function myFun(e){

truePassword = (e.target.nextSibling.innerText);

}

function validate(clicked_ele_id){

password = prompt('Enter Your Course Reservation ID (Case Sensitive)', 'YYY0000');

if (password==truePassword) {

window.location.href = '/certificates/' + clicked_ele_id + '_' + password + '.pdf'; //relative to domain

}else{
alert ("Wrong ID");
return;
}
}
</script>

关于javascript - 如何创建包含 php + Javascript 变量的 href 链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30592704/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com