简体   繁体   English

如何将 JavaScript 数组作为参数传递给 URL 并在 PHP 中捕获它?

[英]How to pass a JavaScript array as parameter to URL and catch it in PHP?

I have an array in JS and I am trying to pass it as parameter to URL and catch it in PHP but I cant get to understand how to do it:我在 JS 中有一个数组,我试图将它作为参数传递给 URL 并在 PHP 中捕获它,但我无法理解如何做到这一点:

var trafficFilterHolder = ["roadworks","snow","blocking"];
var filters = encodeURI(JSON.stringify(trafficFilterHolder));

FYI: I am using windows.fetch for posting.仅供参考:我正在使用 windows.fetch 进行发布。

in PHP:在 PHP 中:

$trafficFilters = $_GET["trafficFilters"];
$obj = json_decode($trafficFilters);       
var_dump($obj);

It's quite simple, you are passing these data to php with ajax, correct?这很简单,您将这些数据传递给 php 和 ajax,对吗?

First of all, you are creating the javascript array incorrectly:首先,您错误地创建了 javascript 数组:

var trafficFilterHolder = [0: "roadworks", 1: "snow", 2: "blocking"];

Don't use brackets to create arrays with keys, use this format instead:不要使用括号来创建带有键的 arrays,而是使用以下格式:

var trafficFilterHolder = {0: "roadworks", 1: "snow", 2: "blocking"};

Now, in the ajax, just add the array in the data :现在,在 ajax 中,只需在data中添加数组:

 $.ajax({
     data: { trafficFilters: trafficFilterHolder }
 });

You are passing the data to php with fetch() intead of ajax, so the alternative of my first answer to do the same with the fetch() is:您正在使用 fetch() 而不是 ajax 将数据传递给 php,因此我的第一个答案的替代方法是使用 fetch() 执行相同操作:

var trafficFilterHolder = ["roadworks","snow","blocking"];
var trafficFilterHolderJoin = trafficFilterHolder.join(); // comma-separeted format => "roadworks,snow,blocking"

Now add the trafficFilterHolderJoin variable to the traffic query of the URL of your fetch(), like:现在将trafficFilterHolderJoin变量添加到 fetch() 的 URL 的traffic查询中,例如:

fetch('script.php?traffic=' + trafficFilterHolderJoin)

Then in your php script file you will convert the comma-separeted format to php array format using the explode function:然后在您的 php 脚本文件中,您将使用explode function 将逗号分隔的格式转换为 php 数组格式:

$traffic = explode(",", $_GET['traffic']);

Try using ajax and pass that array and retrieve values at the PHP end.尝试使用 ajax 并传递该数组并在 PHP 端检索值。

var filters = encodeURI(JSON.stringify(trafficFilterHolder)); var filters = encodeURI(JSON.stringify(trafficFilterHolder));

$.ajax({ type: "POST", url: "test.php", data: {data: filters}, cache: false, $.ajax({ type: "POST", url: "test.php", data: {data: filters}, 缓存: false,

    success: function(){
        alert("OK");
    }
});

All demands to the server are executed as an http requests.对服务器的所有请求都作为 http 请求执行。 Ther are two types of HTTP requests - GET and POST.有两种类型的 HTTP 请求 - GET 和 POST。

https://www.w3schools.com/tags/ref_httpmethods.asp https://www.w3schools.com/tags/ref_httpmethods.asp

What you're describing is called GET request.您所描述的称为 GET 请求。 With GET request the parameters are passed via the address bar.对于 GET 请求,参数通过地址栏传递。 For making an http request you have two options.要发出 http 请求,您有两个选择。

  1. The direct HTTP GET request.直接 HTTP GET 请求。 For this you need simply open a new page with为此,您只需打开一个新页面

    window.location.href = 'http://your_site.com/file.php?name1=value1&name2=value2' window.location.href = 'http://your_site.com/file.php?name1=value1&name2=value2'

This will open a new page in your browser and pass a request with your parameters.这将在您的浏览器中打开一个新页面并使用您的参数传递一个请求。

  1. An Ajax HTTP GET request.一个 Ajax HTTP GET 请求。 You have a lot of options here:您在这里有很多选择:
  • an old-fashion way with xmlHttpRequest object xmlHttpRequest object 的老式方式
  • a modern fetch API with promises带有承诺的现代获取 API
  • third-part libraries like jQuery.ajax etc.第三方库,如 jQuery.ajax 等。

AJAX request can send and receive information from the server (either in GET or POST request) without renewing the page. AJAX 请求可以在不更新页面的情况下从服务器发送和接收信息(在 GET 或 POST 请求中)。 After that the result received can be managed with your javascript application however you want.之后,您可以根据需要使用 javascript 应用程序管理收到的结果。

Hope, it makes more clear for you.希望,它让你更清楚。 You can search about all this technologies in the google.您可以在 google 中搜索所有这些技术。 This is the way how to exchange data from front-end to back-end.这就是如何从前端到后端交换数据的方式。

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

相关问题 如何将PHP数组参数传给Javascript Function? - How to pass PHP array parameter to Javascript Function? 如何将参数传递给javascript中的URL? - how to pass parameter to URL in javascript? Javascript如何很好地传递位数组作为url参数(角度js) - Javascript How to pass an bit array nicely as url parameter (angular js) 如何将数组作为 URL 参数传递? - How to pass an array as a URL parameter? 将 Javascript 值作为 php url 参数的一部分传递 - Pass Javascript value as part of php url parameter 如何将php数组字符串作为参数传递给javascript函数? - How to pass php array string to javascript function as a parameter? 如何将 PHP 数组作为参数从 onClick HTML 按钮传递给 Z686155AF75A60A0F36E9D80C1F7? - How to pass a PHP array as parameter from onClick HTML button to JavaScript? 如何使用javascript传递数组参数 - How to pass an array parameter with javascript 如何将字符串数组作为 url 参数传递和读取,以及如何读取 javascript 中的查询参数? - How to pass and read an array of strings as a url parameter and how to read the query parameter in javascript? 为什么我不能使用 JavaScript 将对象数组作为 URL 参数传递给 PHP? - Why I cant pass an array of objects as a URL parameter from JavaScript to PHP using window.fetch?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM