通过设置PHP会话变量AJAX / jQuery的 [英] set php session variable using ajax/jquery

查看:106
本文介绍了通过设置PHP会话变量AJAX / jQuery的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

林新来的Ajax / jQuery的。那么有没有一种简单的方法,当我点击一个单元格,在该表中,它的价值将被存储在一个会话变量。

在此先感谢

解决方案

 < HTML>
< HEAD>
...
<脚本SRC =jquery.js和>< / SCRIPT>
...
< HEAD>
<身体GT;
....
<表ID =点击>< TR>< TD>你好< / TD>< / TR>< /表>
<脚本>
$('#可点击TD')。在('点击',功能(五){$阿贾克斯({
    键入:POST,
    网址:yourphppage.php,
    数据:{sendCellValue:$(本)的.text()}
}); });
< / SCRIPT>
....
< /身体GT;
< / HTML>
 

只要你喜欢该表可大,和上面的脚本将发送到您的PHP页面被点击了ID为点击的表中的任意单元格。请注意,如果你想要一个以上的可点击的表,你应该把它从ID更改为一类。

然后在yourphppage.php,(它给出了上述code必须是在同一目录下:

 < PHP
在session_start();
$ _SESSION ['cellValue'] = $ _ POST ['sendCellValue'];
?>
 

保存信息到数据库是非常不同的会话。您是否需要区分谁点击了什么?或者只是记录一下被点击,不管是谁点击它呢?

如果是这样,请使用以下,而不是第二个片段上面的:

 < PHP
$连接=的mysql_connect(localhost,则my_db_username,pass1234);
mysql_select_db(my_db_name,$连接);

的mysql_query(INSERT INTO TheTable(CellValue)
VALUES('.mysql_real_escape_string($ _ POST ['sendCellValue'])。')');
?>
 

有关,你会希望有一个表(称为TheTable)。这将需要两列。第一个可以被称为ID或类似的东西。它需要被设置为自动增加,和它的类型必须是整数。第二列必须被称为CellValue,并且它的类型可以设为VARCHAR与255个字符。

要输出的信息,因此它可以打印,你应该问如何输出一个新的问题一个数据库表的HTML。在所有的可能性,你可能不希望打印的表原样,所以你应该具体说明它是什么人会被点击,即是它的文字?或者nummber?如果它是一个数字,什么样的数字呢?是否要组的号码,并显示的点击次数对各组的总数量?等,请具体说明。您可能需要改变一些根据以上对你想做的事在表中的信息是什么后处理打印出来了。例如,您可能需要从varchar改变第二列的类型为整数或浮点数(你会在这种情况下,需要改变的PHP文件也一样)。

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.

thanks in advance

解决方案

<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.

Then in yourphppage.php, (which given the above code must be in the same directory:

<?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.

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).

这篇关于通过设置PHP会话变量AJAX / jQuery的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆