繁体   English   中英

将会话变量发送到另一个从未加载的PHP页面

[英]send session variable to another php page that is never loaded

索引页面从登录页面接收会话变量。 我希望将此会话变量与用户的任何操作一起发送到另一个php页面。 使用script.js文件创建一个表并用数据填充。 script.js文件具有对dbmanipulate.php文件的ajax调用。 dbmanipulate.php与数据库进行交互。 现在无论如何都可以将php会话变量从index.php发送到dbmanipulate.php,用户加载的唯一页面是index.php,并且该页面显示为动态生成的表。

编辑后我的index.php页面:

        <?php
        session_start();
        $fname=$_SESSION['mail'];
        ?>

        <!DOCTYPE HTML>
        <html>
        <title>Addressbook</title>
        <head>
        <script type="text/javascript" src="http://code.jquery.com/jquery-1.11.1.min.js"></script>





 <script type="text/javascript">

 function myFunction () {


$.ajax({
     url:"DbManipulate.php",
              type:"POST",
              data:"<?php echo $fname ?>"

               });

   }

   </script>            
        <link rel="stylesheet" type="text/css" href="crudstyle.css" />

        </head>
        <body onload="myFunction()" bg color="">

            <div id="hidden_form_container" style="display:none;"></div>

        <div id="mhead"><h2>Your Adressbook</h2></div>
        <div id="note"> <span> your addressbook is connected to our servers :) </span></div>
        <?php
        echo $fname;

        ?>
        <table id='demoajax' cellspacing="0">
        </table>
        <script type="text/javascript" src="script.js"></script>

        </body>
        </html>
$(function(){
 $.ajax({
 var session_data = <?php $_SESSION['mail'] ?>;
 url:"DbManipulate1.php",
     type:"POST",
     data:"{actionfunction:showData,session_data:session_data"},
     cache: false,
     success: function(response){

      $('#demoajax').html(response);
          createInput();
      }
});

尝试这个 :)

可能会有点作弊? 将其放入php文件而不是包含.js文件。

    <script>
  $(function(){
        $.ajax({
                url:"DbManipulate1.php?mail=<?php echo $_SESSION['mail']; ?>",
                type:"POST",
                data:"actionfunction=showData",
                cache: false,
              success: function(response){

                    $('#demoajax').html(response);
                 createInput();

                                         }
               });
   </script>

然后在另一页上使用$ _GET ['mail']获取变量。


编辑:
尝试这个:

 $( document ).ready(function() { $.ajax({ url:"DbManipulate.php", type:"POST", data:"mail=<?php echo $fname ?>" }); } 

并在另一页上使用$ _POST ['mail']。不要在正文标签中放置on_load。

在不同的主机中有一些微妙之处。 尝试在该文件中调用会话变量,而不必仅在没有AJAX的情况下声明它。 如果生成了,那么一切都会正常,如果没有,那么请询问您的托管服务提供商是否不需要在php.ini中指定会话的路径。 进一步检查将文件本身编码为UTF-8(不带BOM)。

由于您在一个服务器上运行文件,因此会话变量不一定发送POST,这意味着应使用文件开头的session_start()轻松检索它。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM