如何在脚本标记的功能中初始化会话

how to initialize session inside function of a script tag?

本文关键字:功能 初始化 会话 脚本      更新时间:2023-09-26
<?php
// Start the session
session_start();
?> 
 <script src="../../js/jquery.js"></script>
<script>
function hello(y){
              alert( y);
          }
</script>

 <?php 
 {

     require 'db_connect.php';
$count=0;
     $count = mysql_query ('SELECT COUNT(ques_no) FROM asg_cre_mcq') ;
     $row = mysql_fetch_array($count);
 //echo $count;
     for ($y = 1; $y <=$row[0]; $y++) 
     { 
           echo "<button   style='width:30px;height:30px' onclick='hello($y);'>". $y."</button>";
          // $_SESSION["buttons"] =$y;

         }
     } 
?>

我正在根据来自数据库的值动态生成按钮。然后使用onclick功能,我正在检查单击按钮,即单击哪个按钮。我想要的是使用会话将这个 y 谷(即按钮编号)传递给 hello 函数中的另一个 PHP 页面。我该怎么做?

这不是

$_SESSION 的使用方式。现在,您只需在 for 循环的每次迭代中覆盖 $_SESSION['button'],最终它将具有最后一个$y的值,例如 2332。

要实现您想要实现的目标,请不要使用 $_SESSION,但是,例如,将该值作为 $_GET 参数传递并在下一页上获取。为此,例如,您可以在"hello"函数中获取和传递$y。

 function hello(y){
 window.location.href = 'theUrlYouWantToCall?buttonnumber=' + y
 }

并在下一页上获取此内容

echo $_GET['buttonnumber'];