简体   繁体   English

在不使用cookie的情况下将变量的值传递到另一个页面(Javascript)

[英]Pass a variable's value to another page without using cookies ( Javascript )

im new to javascript and I am wondering how can I pass a variable's value from page 1 to page 2 ? 我是javascript新手,我想知道如何将变量的值从第1页传递到第2页? In PHP, when submitting a form I can use on page 2 $Test = $_POST[id1]; 在PHP中,提交表单时可以在第2页上使用$Test = $_POST[id1]; to obtain the value of the form element with id1 . 获取具有id1的form元素的值。
In this case I cannot use aa form or PHP because I am populating the Divs containing products images and titles from my database and I need to use the Onclick(); 在这种情况下,我不能使用aa表单或PHP,因为我正在从数据库中填充包含产品图像和标题的Div,并且需要使用Onclick(); function to store the clicked Div's value and pass it to Page2; 用于存储单击的Div值并将其传递给Page2的功能; then populate the item's data. 然后填充项目的数据。
How can I implement this easily without the need of using cookies ? 如何无需使用cookie就能轻松实现此目的?

This is what I have so far : 这是我到目前为止所拥有的:

HTML 的HTML

<?php  foreach( $result as $res) : ?> 

<div class="main"<?php echo $res['tag'];?>">
<div class="image">

<a href="page2.html" title="<?php echo $res['id'];?>"><?php echo $res['image'];?></a>
<a href="page2.html" class="title"><?php echo $res['id'];?></a>

</div> 
</div>    
<?php endforeach;  ?>

Javascript ( get Value of Clicked Div ) Javascript (获取Clicked Div的值)

$('.title').click(function() {
var selectedId = $(this).text();
 window.alert(selectedId); });

The above works fine, How I pass selectedId to page2 ? 上面的工作正常,我如何将selectedId传递给page2? Thnaks 恶作剧

If you want to use only jQuery and no forms, no PHP, you could use the jQuery functions to store values in the session. 如果只想使用jQuery,不使用表格,不使用PHP,则可以使用jQuery函数在会话中存储值。

A plugin might help you, like this one : http://www.jquerysdk.com/api/jQuery.sessionStorage 像这样的插件可能会帮助您: http : //www.jquerysdk.com/api/jQuery.sessionStorage

sessionStorage.setItem('storedId', $(this).text();); sessionStorage.setItem('storedId',$(this).text(););

should be like this: 应该是这样的:

sessionStorage.setItem('storedId', $(this).text()); sessionStorage.setItem('storedId',$(this).text()); //you have extra semicolon //您有多余的分号

that might be one of your error 那可能是你的错误之一

This can be achieved using jquery and ajax: 这可以使用jquery和ajax实现:

$('#sampButton').click(function(e){


            $.ajax({
                    type:'post',
                    url:'php/filename.php',
                    data:{toSubmit: $('#textBoxValue').val()},
                    success:function(data)
                    {


                    }
        });
        });

Then on the filename.php file, you can call the toSubmit as a POST variable, $_POST['toSubmit']. 然后,在filename.php文件上,可以将toSubmit称为POST变量$ _POST ['toSubmit']。

Hope this helps. 希望这可以帮助。

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

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