【问题标题】:PHP Set $_SESSION variable value according to (Button/Link) clickPHP 根据(按钮/链接)点击设置 $_SESSION 变量值
【发布时间】:2018-04-16 04:45:36
【问题描述】:

我有以下代码:

<ul>
 <li>
  <a href="index.php?page=quiz" class="cat" >
    <h3>Category 1</h3>
    <p>(explain here)</p>
  </a>
 </li>
 <li>
  <a href="index.php?page=quiz" class="cat" >
    <h3>Category 2</h3>
    <p>(explain here)</p>
  </a>
 </li>
</ul>

我需要的是将类别号发送到页面index.php?page=quiz 通过$_POST 或其他任何方式 无需像这样通过$_REQUEST 发送index.php?page=quiz&amp;cat=1
OR 根据当前单击的链接将类别编号值分配给$_SESSION 变量 获取目标页面index.php?page=quiz的值。


我已经尝试过这个解决方案:

  • 通过javascript将变量值发送到页面
    var num=1; /*or put the current clicked category number */
    $.ajax({
            url: 'index.php?page=quiz',
            type: 'post',
            dataType: 'json',
            data: {
                category_no: num,
            }
        })
    

    发送过程和page=quiz的值好像有问题 没有发送到目的地,我们仍在page=home所在的当前页面。

    您有什么有用的想法吗?

    注意:我的问题不是从当前点击(按钮/链接)到javascript,而是通过上述任何方式将其发送到目标页面。

  • 【问题讨论】:

      标签: javascript php session


      【解决方案1】:

      将类别 ID 添加为数据属性,如下所示:

      <ul>
       <li>
        <a data-category-id="1" href="index.php?page=quiz" class="cat" >
          <h3>Category 1</h3>
          <p>(explain here)</p>
        </a>
       </li>
       <li>
        <a  data-category-id="2" href="index.php?page=quiz" class="cat" >
          <h3>Category 2</h3>
          <p>(explain here)</p>
        </a>
       </li>
      </ul>
      

      然后像这样处理点击事件以进行ajax调用(动态获取点击的类别链接的url和它的id):

      // When page loads
      $(document).ready(function()
      {
          // Handle category click event
          $('a.cat').click(function(e)
          {
              // Prevent default action of link click
              e.preventDefault();
      
              // Get the url
              var categoryUrl = $(this).prop('href');
      
              // Get the category id
              var categoryId = $(this).data('category-id');
      
              // Make ajax call
              $.ajax({
                  url: categoryUrl,
                  type: 'post',
                  dataType: 'json',
                  data: {
                      category_no: categoryId
                  }
              })
              .done(function() {
                  location.href = categoryUrl;
              });
          })
      });
      

      这样,当您单击链接时(不会运行转到 url 的默认操作)。反而;首先向单击的 url(在您的情况下始终为 index.php?page=quiz)发出 ajax POST 请求,当请求完成时,您将被重定向到该页面。

      【讨论】:

      • 我已经尝试过这个解决方案,并且有多个 solotion 将类别 id 发送到 javascript 函数,我的问题不是从当前按钮获取类别 id 到 java 脚本,我的问题是发送到目标页面
      • 目标页面是什么意思?当一个链接被点击时,你试图向哪里发出 ajax 发布请求?
      • 当前页面是:index.php?page=home,扩展页面是:index.php?page=quiz
      • 页面可能在 ajax 请求有机会执行或运行完成之前加载。我会更新我的答案,见上文。
      • 我在上面提到过,我可以像 index.php?page=quiz&cat=1 这样发送它,但出于安全目的我不需要显示猫 ID 我需要另一种发送方式,好的,我会的现在看看
      【解决方案2】:

      如果 num 是您点击的类别,我认为它应该是这样的:

      var num=1;
      $.ajax({
          url: 'index.php?page=quiz',
          type: 'post',
          dataType: 'json',
          data: {
              num,
          }
      });
      

      在你的'index.php'中:

      session_start();
      $_SESSION['catID'] = $_POST['num'];
      

      如果这不起作用,查看您的 index.php 会很有帮助。

      编辑:让我们在另一边尝试一下......只是为了测试。

      创建一个名为 getCatID.php 的新 php 并将 AJAX 更改为:

      var num=1;
      
          $.ajax({
              url: 'getCatID.php',
              type: 'post',
              dataType: 'json',
              data: {
                  num,
              }
          });
         window.location = "index.php?page=quiz"
      

      在getCatID.php中:

      <?php
          session_start();
          $_SESSION['catID'] = $_POST['num'];
      ?>
      

      然后在你的 index.php 中尝试

      echo $_SESSION['catID'];
      

      PS:你需要以某种方式触发Ajax Call

      【讨论】:

      • 好的,我想我误解了你的问题。你想重定向到测验页面并显示点击类别的问题吗?
      • 当然我需要会话等待另一个评论来描述索引页面
      • 我的索引页面是根据名为“页面”的 url 中 $_REQUEST var 中的变量显示 php 页面的页面,我已经尝试过您的解决方案,但之前可能无法加载 ajax,并且页面仍然使用 value = home,是的,我需要重定向到测验页面,但只能通过 index.php?page=quiz 并且只能通过 index.php 而不是通过制作另一个页面,如 quiz.php 或其他东西
      【解决方案3】:

      解释解决方案:
      问题是没有exit()json_encode() 作为我的函数的结果,不能在页面index.php 代码中应用,这意味着
      整个函数必须移动到另一个文件

      问题解决如下:

      my page:

      <ul>
       <li>
        <a  href="" data-no="1" class="cat" >
          <h3>Category 1</h3>
          <p>(explain here)</p>
        </a>
       </li>
       <li>
        <a  href="" data-no="2" class="cat" >
          <h3>Category 2</h3>
          <p>(explain here)</p>
        </a>
       </li>
      </ul>
      

      JavaScript:

      $('.cat').on('click', function (e) {
          // Prevent default action of link click
          e.preventDefault();
          var cat = $(this).data('no');
          var curr='set_var.php';
          $.ajax({
              url: curr,
              type: 'POST',
              dataType: 'json',
              data: {
                  category: cat
      
              }
          }).done(function(res) {
                  if (res.valid) {
                  document.location.href = 'index.php?page=quiz';
              }
              });
      });
      

      set_var.php :

      <?php 
      session_start();
          if(isset($_POST['category'])){  
              $_SESSION['cat']=$_POST['category'];
              $result['valid'] = TRUE;
          }
          echo json_encode($result);
          exit();
       ?>
      

      感谢您的所有回答

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-07-02
        • 1970-01-01
        • 2021-11-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多