1

After hours of searching and testing I finally have decided to post a question.

I need to be able to make something like this to work with a function.

var myCSV = [1,3,5,6];

Currently, I can grab the CSV from the database as that is how it is stored, myColumn = 1,3,5,6. I can get it to pass as an AJAX success response but it seems to want to add quotes. I then tried to add JSON encode to my PHP side of things and JSON Parse to the success call but still cannot get the function to work. The end goal is to select checkboxes based off the csv value.

This works

var FilterArray =  [1,3,5,6]; // Manually added these numbers as is for testing
$('#myForm').find('.checks').each(function () {
   $(this).prop("checked", ($.inArray(parseInt($(this).val()), FilterArray ) != -1));
});

After trying to much to get my AJAX success response to work in the FilterArray, I decided to just pass it to an input value and work with it. However, cannot figure out how to not treat it as a string when I pass it to the function. Here is what I have tried.

In my getCSV.php I have at the end this

json_encode($foundCSV);

In my AJAX Success

var FilterArray = JSON.parse(response);

I have also tried it without the json_encode and just sending it to an input value which does not add quotes.

So in summary, how can I take a csv e.g. 1,3,5,6 stored value and pass it to a function that works as shown above?

2 Answers 2

3

Assuming you have your string in a variable, called response, and its value is "1,3,5,6", you can translate it into an array of integers with this:

response.split(',').map(e => parseInt(e))

and you can then pass the result of this to your function. Sticking to your naming, the code should look like this:

var FilterArray = response.split(',').map(e => parseInt(e));

What the code does:

  • takes the response and split it by using the comma character as a delimiter
  • this will create an array of strings
  • for each string in that array, tries to parse it as an integer (an ID, i guess?)
Sign up to request clarification or add additional context in comments.

1 Comment

That did it! I had been playing around with split and map but missed the e => part as it only would grab the first digit. Thank you!
0

Clarifying question: What does your $foundCSV look like on the PHP side, before you json-encode it? Is it an array or a string with comma-separated numbers? You might want to check with your developer tools/debugger what the Ajax response contains. It sounds like you have a string but expect an array.

If that's the case, you can create an array on the JS- or PHP-side. For the JS-side, consult @GregorioPalama's answer. Alternatively, you could do this on the PHP-side, which might be shorter:

$response = explode(',', $foundCSV);

and then work with json_encode($response).

Comments

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.