使用 ajax/jquery 设置 php 会话变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15053620/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
set php session variable using ajax/jquery
提问by user2102316
Im new to ajax/jquery. so is there an easy way that when i click a cell with in the table, the value of it will be stored in a session variable.
我是 ajax/jquery 的新手。那么有没有一种简单的方法,当我单击表格中的单元格时,它的值将存储在会话变量中。
thanks in advance
提前致谢
回答by CL22
<html>
<head>
...
<script src="jquery.js"></script>
...
<head>
<body>
....
<table id="clickable"><tr><td>Hello</td></tr></table>
<script>
$('#clickable td').on('click', function(e){ $.ajax({
type: 'POST',
url: 'yourphppage.php',
data: {sendCellValue: $(this).text()}
}); });
</script>
....
</body>
</html>
The table can be as large as you like, and the above script will send to your PHP page any cell that is clicked in the table that has the id "clickable". Note that if you want more than one clickable table, you should change it from an id to a class.
表格可以任意大,上面的脚本会将表格中具有“可点击”ID 的任何被点击的单元格发送到您的PHP 页面。请注意,如果您想要多个可点击的表格,您应该将其从一个 id 更改为一个类。
Then in yourphppage.php, (which given the above code must be in the same directory:
然后在你的phppage.php中,(上面给出的代码必须在同一个目录下:
<?php
session_start();
$_SESSION['cellValue'] = $_POST['sendCellValue'];
?>
Saving the information to a database is very different to a session. Do you need to differentiate who clicked what? Or just record what was clicked on, regardless of who clicked it?
将信息保存到数据库与会话非常不同。你需要区分谁点击了什么吗?或者只记录点击了什么,而不管是谁点击的?
If so, use the following instead of the second snippet above:
如果是这样,请使用以下代码而不是上面的第二个代码段:
<?php
$connection = mysql_connect("localhost","my_db_username","pass1234");
mysql_select_db("my_db_name", $connection);
mysql_query('INSERT INTO TheTable (CellValue)
VALUES ("'.mysql_real_escape_string($_POST['sendCellValue']).'")');
?>
For that you will want a table (called TheTable). It will need two columns. The first could be called "ID" or something similar. It will need to be set as "auto-incrementing", and its type must be "Integer". The second column must be called "CellValue", and its type can be set to "varchar" with 255 characters.
为此,您需要一张桌子(称为 TheTable)。它将需要两列。第一个可以称为“ID”或类似的东西。它需要设置为“自动递增”,并且它的类型必须是“整数”。第二列必须称为“CellValue”,其类型可以设置为“varchar”,长度为 255 个字符。
To output the information, so it can be printed, you should ask a new question on how to output a database table to HTML. In all likelihood you will probably not want to print the table as-is, so you should be specific about what it is people will be clicking on, i.e. is it text? Or a nummber? And if it's a number, what kind of number? Do you want to group the numbers and show a total number of clicks for each group? Etc, please be specific. You may need to change some of the above depending on what post-processing you want to do on the information in the table before you print it out. For example, you may need to change the second column's type from varchar to integer or float (and in that case you would need to change the PHP file as well).
要输出信息以便打印,您应该提出一个关于如何将数据库表输出为 HTML 的新问题。您很可能不想按原样打印表格,因此您应该具体说明人们将点击的内容,即它是文本吗?还是一个数字?如果是数字,那是什么数字?是否要将数字分组并显示每个组的总点击次数?等等,请具体点。在打印出来之前,您可能需要根据您要对表格中的信息进行什么后处理来更改上述某些内容。例如,您可能需要将第二列的类型从 varchar 更改为 integer 或 float(在这种情况下,您还需要更改 PHP 文件)。
回答by Starx
It can be as simple as this:
它可以像这样简单:
The following snippet send the value to a page where session variable can be set
以下代码段将值发送到可以设置会话变量的页面
$.post('sessionsetter.php', { 'fieldname' : 'sessionvariablename', 'value' : 'sessionvalue'});
Now 'sessionsetter.php' can be something like this:
现在 'sessionsetter.php' 可以是这样的:
session_start();
$_SESSION[$_POST['fieldname']] = $_POST['value'];
Now, you can send as many variables you want to set as SESSION variable.
现在,您可以发送任意数量的要设置为 SESSION 变量的变量。

