0

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:

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

FYI: I am using windows.fetch for posting.

in PHP:

$trafficFilters = $_GET["trafficFilters"];
$obj = json_decode($trafficFilters);       
var_dump($obj);
3
  • 1
    Trying to output any complex object or array with echo, makes little sense to begin with. Use var_dump for proper debug outputs. Commented Dec 15, 2020 at 12:32
  • @CBroe true, thanks! Commented Dec 15, 2020 at 12:43
  • Does this answer your question? php javascript url encoding Commented Dec 16, 2020 at 4:26

4 Answers 4

1

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:

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:

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:

$traffic = explode(",", $_GET['traffic']);
Sign up to request clarification or add additional context in comments.

2 Comments

almost there, but it gives me this weird looking array when I print it in php: array(3) { [0]=> string(10) ""roadworks" [1]=> string(4) "snow" [2]=> string(9) "blocking"" }
What's weird? I see that roadworks and blocking have a quote inside, that's it? If you are not familiar with var_dump(), try to use var_export() or print_r() to view the array.
1

It's quite simple, you are passing these data to php with ajax, correct?

First of all, you are creating the javascript array incorrectly:

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

Don't use brackets to create arrays with keys, use this format instead:

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

Now, in the ajax, just add the array in the data:

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

4 Comments

I am using Fetch not Ajax
and I have an array not an object
In your code snippet you actually created an array of objects because you declared the keys of the values, that's why I tried to help you using the same way. But, even if you have an common javascript array, my answer still works. Also, you don't specified the method you are passing the data to php in your question, that's why I gave the Ajax example. Next time, try to be more specific in your questions.
thanks, I just edited my question. I also changed the array format. Please take a look
0

All demands to the server are executed as an http requests. Ther are two types of HTTP requests - GET and POST.

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

What you're describing is called GET request. With GET request the parameters are passed via the address bar. For making an http request you have two options.

  1. The direct HTTP GET request. For this you need simply open a new page with

    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. You have a lot of options here:
  • an old-fashion way with xmlHttpRequest object
  • a modern fetch API with promises
  • third-part libraries like jQuery.ajax etc.

AJAX request can send and receive information from the server (either in GET or POST request) without renewing the page. After that the result received can be managed with your javascript application however you want.

Hope, it makes more clear for you. You can search about all this technologies in the google. This is the way how to exchange data from front-end to back-end.

1 Comment

I am using Fetch not Ajax, and I need to pass it as parameter of ?trafficFilters=
0

Try using ajax and pass that array and retrieve values at the PHP end.

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

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

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

1 Comment

what about window.fetch()? shouldnt it be the same? It doesnt work with fetch, so why would it work with ajax?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.