简体   繁体   English

我可以将 PHP 变量携带到 HTML 或 Javascript 文件中吗

[英]Can i carry PHP variables over into an HTML or Javascript file

I'm new to PHP and am trying to create an Age Validation form that will limit access to specific content on a site if a user is under a certain age.我是 PHP 的新手,我正在尝试创建一个年龄验证表单,如果用户未满一定年龄,该表单将限制对网站特定内容的访问。

This is the HTML form (index.html):这是 HTML 表格 (index.html):

 <,DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width. initial-scale=1.0" /> <title>Document</title> </head> <body> <form action="data.php" method="POST"> <input type="date" name="date" /> <input type="submit" name="submit" /> </form> </body> </html>

and this is the PHP script(data.php):这是 PHP 脚本(data.php):

 <?php //Age Validation Form session_start(); if (isset($_POST['submit'])) { //Check if button clicked on form page $date2=date("Ymd");//today's date $date1=new DateTime($_REQUEST['date']); //user date $date2=new DateTime($date2); $interval = $date1->diff($date2); //check diff between dates $myage= $interval->y; //resulting age if ($myage >= 16){ //full access to website is granted $_SESSION[$limited] = false; header('Location: ../index.php'); }else{ //limited access is granted $_SESSION[$limited] = true; header('Location: ../index.php'); } }else{ //if result page page is loaded without form submission echo "Return to start page"; }?> <form action="index.html"> <!--Return to form page--> <button type="submit">Return</button> </form>

I would like to be able and carry the resulting $limited variable from the PHP file into this HTML file:我希望能够将 PHP 文件中生成的 $limited 变量携带到此 HTML 文件中:

 <,DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width. initial-scale=1?0" /> <title>Document</title> </head> <body> <script type="text/javascript"> function myFunction () { var access = <?php echo $limited;>. var txt = document;createElement('h1'). txt;innerHTML = access. document.body;appendChild(txt); } myFunction(); </script> </body> </html>

This is currently just for testing to make sure it can carry over.这目前仅用于测试以确保它可以结转。 I have tried the $_SESSION method but i can't seem to figure it out.我已经尝试过 $_SESSION 方法,但我似乎无法弄清楚。

Any and all possible solutions welcomed.欢迎任何和所有可能的解决方案。

Thank you谢谢

You're using $limited as a key in the $_SESSION array.您使用 $limited 作为 $_SESSION 数组中的键。 What's in $limited? $limited 里有什么? I'm pretty sure that if you reassign the correct value to $limited, you can access the value in $_SESSION with this:我很确定,如果您将正确的值重新分配给 $limited,您可以使用以下命令访问 $_SESSION 中的值:

    <?php $limited = 'MyAwesomeKey'; ?>
    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
      </head>
      <body>
          <script type="text/javascript">
            function myFunction () {
                var access = <?php echo $_SESSION[$limited] ?>;
                var txt = document.createElement('h1');
                txt.innerHTML = access;
                document.body.appendChild(txt);
            }
            myFunction();
        </script>
      </body>
    </html> 

First, your HTML file must be parsed by the PHP interpreter so it must have the.php extension in order to access any session variable. First, your HTML file must be parsed by the PHP interpreter so it must have the.php extension in order to access any session variable.

Here's an example with 3 files in the same directory based on your question:以下是根据您的问题在同一目录中包含 3 个文件的示例:

index.html索引.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <form action="data.php" method="POST">
      <input type="date" name="date" />
      <input type="submit" name="submit" />
    </form>
  </body>
</html>

data.php:数据.php:

<?php  //Age Validation Form
session_start();
if (isset($_POST['submit'])) { //Check if button clicked on form page

   $date2=date("Y-m-d");//today's date

   $date1=new DateTime($_REQUEST['date']); //user date
   $date2=new DateTime($date2);

   $interval = $date1->diff($date2); //check diff between dates

   $myage= $interval->y; //resulting age

  if ($myage >= 16){  //full access to website is granted
     $_SESSION['limited'] = false;
     header('Location: new.php');
   }else{ //limited access is granted
      $_SESSION['limited'] = true;
     header('Location: new.php');
   }

  }else{ //if result page page is loaded without form submission
      echo "Return to start page";
  }

  ?>

   <form action="index.html"> <!--Return to form page-->
      <button type="submit">Return</button>
   </form>

new.php (the new page, where you access your session variable) new.php (新页面,您可以在其中访问 session 变量)

<?php 
session_start();

$limited = $_SESSION['limited'] ?? true;

?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script type="text/javascript">
        function myFunction () {
            var access = <?php echo $limited ? "true" : "false" ?>;

            var txt = document.createElement('h1');
            txt.innerHTML = access;
            document.body.appendChild(txt);
        }

        myFunction();


    </script>
  </body>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过Javascript将php会话从一页进行到另一页? - How do I carry over a php session from one page to another via Javascript? 如何将多个变量传递给HTML / PHP中的JavaScript函数? - How can I pass multiple variables to a JavaScript function in HTML / PHP? 如何将变量从Flex传递到JavaScript(单独的HTML文件)并返回其他变量? - How I can pass variables from Flex to JavaScript (separate HTML file) and return the other variables? 我可以在同一个 HTML 文件中插入 PHP 和 javascript 代码吗? - Can I insert PHP and javascript code in the same HTML file? 如何在我在HTML文件中编写的脚本中访问JavaScript文件中的变量? - How can I access variables from a JavaScript file from within a script I wrote in an HTML file? JavaScript变量带有我需要在Html.ActionLink中使用的ID - JavaScript variable carry an ID that I need to use in a Html.ActionLink 我无法将PHP变量发送到JavaScript - I can't send PHP variables to JavaScript 如何在此Javascript中添加php变量? - How can I add php variables to this Javascript? 如何遍历 XML 文件并使用 JavaScript 在 HTML 网页中显示数据? - How can I iterate over an XML file and display the data in a HTML web page using JavaScript? 从单独的HTML / php文件中调用javascript变量 - Calling javascript variables from a separate HTML/php file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM