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

时间:2015-06-02 09:49:54

标签: javascript php html

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

我想在这里实现的目标:

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

证书/ First_Last_ID.pdf

其中“第一”& “Last”是php变量,php变量ID = js var truePassword

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

我收到以下链接:

<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>

实现这一点需要在链接名称中添加一些变量,也许!

请建议

using (FileStream fs = new FileStream(@"\\webserver\recordtest\1234.ogg", FileMode.Append))
{
    fs.Write(bytes, 0, bytes.Length);
}

3 个答案:

答案 0 :(得分:1)

使用以下

更改了最后一个“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>

答案 1 :(得分:0)

您可以在PHP中包含PHP变量:

echo "<td>" . "<a href='".$row['First']."_".$row['Last']."_".$row['ID'].".pdf'";

答案 2 :(得分:0)

我建议使用AJAX调用。这样你也可以保持密码检查服务器端。