[英]Storing data in the database using jQuery & PHP without a form
Ok, so I'm trying to store users' key presses in a database, but so far I had no luck. 好的,所以我试图将用户的按键存储在数据库中,但是到目前为止我还没有运气。 I have the following JavaScript code: 我有以下JavaScript代码:
$(document).bind('keypress', function (event) {
$.post('processPost.inc.php', { "valueToStore" : event.which } );
}
Then, I have my processPost.inc.php
PHP file, with the following code: 然后,我有以下代码的processPost.inc.php
PHP文件:
<?php
if (isset($_POST['valueToStore'])) {
storeIntoDatabase(sanitizeInput($_POST['valueToStore']));
}
I know that the event detection itself works, that the storeIntoDatabase
and sanitizeInput
functions and everything else works, but I'm almost certain that I'm using jQuery 's $.post
in the wrong way (or there's a typo somewhere but that wouldn't be so hard to fix, just a pain in the ass). 我知道事件检测本身可以工作, storeIntoDatabase
和sanitizeInput
函数以及其他所有功能都可以工作,但是我几乎可以确定我以错误的方式使用了jQuery的$.post
(或者某个地方有错字,但是修复起来不是那么困难,只是一团糟。
I'd like to know how to use it in this specific situation. 我想知道如何在这种特定情况下使用它。 I've been looking up $.post
examples and all, and every example I found talks about forms and serializing, but in this case there is no form at all. 我一直在寻找$.post
示例以及所有示例,而我发现的每个示例都涉及表单和序列化,但是在这种情况下根本没有表单。 Can anyone help me with this? 谁能帮我这个?
According to the doc: [source] 根据文档: [来源]
The data option can contain either a query string of the form key1=value1&key2=value2, or a map of the form {key1: 'value1', key2: 'value2'} 数据选项可以包含格式为key1 = value1&key2 = value2的查询字符串,也可以包含格式为{key1:'value1',key2:'value2'}的映射。
In a map, the key should not be wrapped with quotes: 在地图中,键不应用引号引起来:
$(document).bind('keypress', function (event) {
$.post('processPost.inc.php', { valueToStore : event.which } );
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.