【发布时间】:2017-12-14 15:50:22
【问题描述】:
我有一个输入字段,当用户输入内容时,它会立即使用 jQuery replace 函数创建一个数组。目标是将这些值发送到 PHP 并在 MySQL 中的特定表上执行实时搜索。
我得到了input 字段和ajax 调用的部分,但到目前为止我还没有完成 php 循环的部分。
我的目标是将此数组序列化到 php 并将每个项目用作变量。
这是我的 jQuery 和 HTML fiddle。
这是我的代码:
HTML:
<div class="container-fluid" style="margin-top:10px;">
<div class="row" style="padding:10px;">
<input class="form-control" id="Input">
</div>
<div class="row">
<div class="container rounded" id="smd" style="background-color:red; height:100px;display:none;">
<span>Search Results here</span>
</div>
</div>
</div>
jQuery:
$("#Input").keyup(function() {
var div = $('#smd');
if ($("#Input").val().length > 0) {
div.show();
var source = $("#Input").val();
var splittedSource = source.replace(/\s{2,}/g, ' ').split(' ');
$.post('/url/to/page', splittedSource);
console.log(splittedSource);
} else {
div.hide();
}
});
这是我的字符串的样子:
["This", "is", "my", "string"]
0:"This"
1:"is"
2:"my"
3:"string"
【问题讨论】:
-
那么问题出在哪里?你试过
print_r($_POST)看看你在服务器上得到了什么吗? -
你不能只是把任何东西作为数据参数,它必须是 jQuery 可以使用/转换成适当的请求格式的格式,比如
application/x-www-form-urlencoded,例如$.post(url,{data:splittedSource}) -
注明。我已经更改了
$.post部分。 -
@PatrickEvans - 实际上你几乎可以。仅以
data的形式发送一个数组可以正常工作,但您在服务器上没有访问它的密钥,它只会用数据填充$_POST。
标签: javascript php jquery mysql arrays