简体   繁体   English

我如何发布数组并使用php遍历它?

[英]How do I post an array and loop through it using php?

I have an input field and when the user is typing something in, it immediately creates an array using jQuery replace function. 我有一个输入字段,当用户输入内容时,它立即使用jQuery replace函数创建一个数组。 The target is to send those values to PHP and perform a live search on a specific table in MySQL. 目标是将这些值发送到PHP并在MySQL中的特定表上执行实时搜索。

I got the part of the input field and the ajax call, but I do not get the portion of the php loop done so far. 我得到了input字段和ajax调用的一部分, 但是到目前为止我还没有完成php循环的一部分。

My target is to send this array serialized to php and use each item as variable. 我的目标是将序列化的数组发送到php,并将每个项目用作变量。

This is my jQuery and HTML fiddle . 这是我的jQuery和HTML 小提琴

This is my code: 这是我的代码:

HTML: 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: 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 how my string looks like: 这是我的字符串的样子:

["This", "is", "my", "string"]

0:"This"
1:"is"
2:"my"
3:"string"

You either need to send as key/value pair or send and receive as application/json . 您要么作为键/值对发送,要么作为application/json发送和接收。

Right now you are only sending a value without a key that can be indexed by $_POST 现在,您只发送一个没有键的值,该键可以由$_POST索引

Try 尝试

$.post('/url/to/page',{arr: splittedSource});

And in php 和在PHP

$arr = $_POST['arr'];//php array

To do it as json: 要做为json:

$.ajax({
   url: '/url/to/page',
   contentType:'application/json',
   data: JSON.stringify(splittedSource)
   // other options
});
//php
$arr = json_decode(file_get_contents('php://input'),true);

While I have the feeling that you could (or even should) handle such logic in your back end code, you might want to take a look at the manual on how to use arrays in a form 虽然我觉得您可以(甚至应该)在后端代码中处理这种逻辑,但是您可能想看一看如何以某种形式使用数组的手册

This suggests that you should use a name that ends in [] . 这建议您使用以[]结尾的名称。 Since posting a form is simply using the application/x-www-form-urlencoded enctype by default, you can simply put the value myarray[]=a&myarray[]=b ... etc in the request body and PHP will interpret it as the array value $_POST['myarray'] . 由于默认情况下发布表单仅使用application/x-www-form-urlencoded ,因此您只需将值myarray[]=a&myarray[]=b ...等放入请求正文中,PHP会将其解释为数组值$_POST['myarray']

this is common format 这是常见格式

$.ajax({
  type: "POST",
  url: url,
  data: data,
  success: success,
  dataType: dataType
});

you need to send correct format like 您需要发送正确的格式,例如

$.post( "test.php", { name: "John", time: "2pm" } );

so your code need to update 所以你的代码需要更新

$.post('/url/to/page', splittedSource); 

to

$.post('/url/to/page',{data: splittedSource});

so you can get data using 这样您就可以使用

<?php
$data = $_POST['data'];

you can use this also format 您也可以使用此格式

$("button").click(function(){
    $.post("demo_test_post.asp",
    {
        name: "Donald Duck",
        city: "Duckburg"
    },
    function(data, status){
        alert("Data: " + data + "\nStatus: " + status);
    });
});

for more information about ajax post 有关ajax发布的更多信息

https://api.jquery.com/jquery.post/ https://api.jquery.com/jquery.post/

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

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