dstt1818 2014-05-13 02:41
浏览 60
已采纳

将javascript数据存储到PHP SESSION VARIABLE中?

I'm wondering if it's possible to store a number generated from a javascript script into a PHP SESSION variable. I basically want to store my code '+.res.id+' and '+.res.level+' into a PHP SESSION. Those 2 javascript codes generate a random number and inputs that number in a html form. IS there anyway I could take that number and store it in a PHP SESSION? For example

<script>
JAVASCRIPT CODING

'+res.level+'
'+res.id+' // BTW it actually does generate a number I just can't echo it in another      page or store it in a SESSION 
</script>

<?php
$_SESSION['.res.id.'] = $idnum;
$_SESSION['.res.level.'] = $levelnum;
?>

Then after in a seperate page I want to call that SESSION and echo the number that the code generated. But my only problem is that when I echo the $_SESSION it echo's as ".res.id." instead of the randomly generated number. How do I fix this to make it echo the random generated number?

  • 写回答

1条回答 默认 最新

  • du512926 2014-05-13 02:50
    关注

    First of all javascript is running on a client side and php is running on a server side. It means you can't just store the generated number. But there is a workaround on that. You can do that by means of AJAX request.

    In your js you can do this.

    var res_id = your_random_number;
    var res_level = your_random_number
    
    $.post("/any/url/that/contains/your/php/code", {res_id:res_id, res_level:res_level});
    

    In your php.

    $_SESSION["res_id"] = $_POST["res_id"]; 
    
    $_SESSION["res_level"] = $_POST["res_level"];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动