简体   繁体   English

如何在MySQL数据库中保存jquery keyup()的每个输入值?

[英]How can I save each input value of a jquery keyup() in MySQL database?

I am creating a study for my university and want to save each input value a user types in a password field into a database. 我正在为我的大学创建研究,希望将用户在密码字段中键入的每个输入值保存到数据库中。

For example: A user types "hello" as a password. 例如:用户键入“ hello”作为密码。 I want to save 'h', 'he', 'hel', 'hell' and at least 'hello'. 我要保存“ h”,“他”,“ hel”,“地狱”和至少“ hello”。 If he deletes a character like the last one I want to save this as well: 'hell'. 如果他删除了像最后一个字符一样的字符,我也要保存此字符:“ hell”。

Currently I am saving the last value 'hello' like this: 目前,我正在像这样保存上一个值“ hello”:

startpage.php: startpage.php:

<form action="?atn=validatePassword" method="post" class="form mg_top">
   <label id="enterPW" for="inp_list_generatedPassword">Select or enter Password:</label>
   <input id="inp_list_generatedPassword" name="password" value="" type='password' class="form-control talign_center input" placeholder="Select or enter password">
</form>

ViewController.php: ViewController.php:

private function validatePassword(){        
  $password = $_POST["password"];
}

To get the inputs I have a js file where I am using the keyup event: 为了获得输入,我有一个使用keyup事件的js文件:

startpage.js: startpage.js:

$('#inp_list_generatedPassword').keyup(function() {
var input = this.value;
$('#output').val(input);
$.ajax({
    url: "startpage.php",
    data: {
        'keyValue' : input
    },
    dataType: 'json',
   });
});

In the ViewController.php I am adding this line $currentPW = $_GET["keyValue"]; 在ViewController.php中,我添加以下行: $currentPW = $_GET["keyValue"]; But it doesn't work. 但这是行不通的。 I am getting an error: Undefined index: keyValue in ViewController.php 我收到一个错误: ViewController.php中的未定义索引:keyValue

In addition this won't save each input. 此外,这不会保存每个输入。 It just saves the last entered string when the button is pressed: <button id="matchPassword" class="btn btn-primary form-control">Submit</button> 按下按钮时,它只会保存最后输入的字符串: <button id="matchPassword" class="btn btn-primary form-control">Submit</button>

How can I save all steps? 如何保存所有步骤? And is there a mistake in my code or rather in the Ajax call? 在我的代码中或者在Ajax调用中是否有错误?

Of course, this will give your database an immense overload if someone types really weird stuff. 当然,如果有人键入非常奇怪的内容,这将给您的数据库带来巨大的负担。

You need to use a cache in your PHP-Script, but that is not really part of the Question. 您需要在PHP脚本中使用缓存,但这实际上不是问题的一部分。

To the Solution: 解决方案:

I tried it and it worked with this: 我尝试了它,并与此一起工作:

$(document).on('keyup', '#inp_list_generatedPassword', function() {
    var input = this.value;
    $('#output').text(input);
});

Seeing here: https://jsfiddle.net/cj1367eo/ 在这里看到: https : //jsfiddle.net/cj1367eo/

Now you can use your "input" variable just as you want. 现在,您可以根据需要使用“输入”变量。 I also would prefer using "$.post" as seen here: http://api.jquery.com/jquery.post/ 我也希望使用“ $ .post”,如下所示: http : //api.jquery.com/jquery.post/

Attention, untested code: 注意,未经测试的代码:

$.post( "api.php", { data: input } );

Now you can use the $_POST["data"] to get your input. 现在,您可以使用$ _POST [“ data”]来获取输入。

Additional Information: 附加信息:

But as i told before: Use a cache! 但正如我之前所说: 使用缓存!

Else your system can get in real trouble. 否则您的系统可能会遇到麻烦。

It is really enough to use a global PHP variable that you write every N Minutes or Entries into your database. 使用一个全局PHP变量就足够了,您每隔N分钟或一个条目写入数据库中就可以了。

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

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