0
  • First, I put the array in localStorage into the variable.
    var urls = JSON.parse(window.localStorage.getItem("urls"));
  • Now I need to transfer this array to a variable in php in the same page.



  • The ways I tried;

  1. $urls = array();
    for($i=0;$i<$count;$i++){
    array_push($urls, "<script>document.write(urls[$i]);</script>");
    }
    Actually it kind of works.
    <?php echo $urls[$i];?>
    Normally when I say print to the screen, the variable works the way I want.
    But
    <a href="download.php?urls=<?php echo $urls[$i]; ?>" style="text-decoration: none">
    When I say, the variable returns empty.
    Or, when I say <img src = "<?php $urls [$i]; ?> it also returns blank.


  2. AJAX
    I tried very hard with Ajax and could not reach the result I wanted. My code is working. The problem is: I'm returning the variable with JS, but I can't use the variable over and over in php. I want to keep the variable in php and use it in php on the same page.


  3. COOKIES
    It worked for me perfectly. It meets what I want.
    But
    Data overload happened and crashed.
    I do not intend to use it.

In short, I need to transfer an array in javascript to an array in PHP. I will use the array I transferred to PHP among php codes continuously.

Or isn't what I want is possible because php is a server-side language?


  • NOTE: My knowledge of JavaScript or PHP is limited. The primary language I use is SWIFT. I have been working on this problem for a long time. Thank you for your support.
  • if the data is generated in JS, then AJAX is probably the best bet. Reusing data stored in variables on the same page in PHP is trivial so how you are using it is probably the problem. But to be honest there isn't enough information here to push you in the right direction. – imvain2 Jan 04 '21 at 21:10
  • Does this answer your question? [Get localStorage value into php](https://stackoverflow.com/questions/44834919/get-localstorage-value-into-php) – Nico Haase Jan 04 '21 at 21:31
  • To allow JavaScript to send data to PHP, you must send a request to the server, where PHP has access to the data. Here is a simple way to post data to the server: [Dynamically create inputs in a form and submit it](https://stackoverflow.com/a/133997/3113485). – terrymorse Jan 04 '21 at 22:25
  • @NicoHaase Sorry, it has not been fully resolved. – İsa Diliballı Jan 04 '21 at 22:36

1 Answers1

2

In JavaScript you can fetch.

  fetch("https://yourapi.api",
  {
    headers: {
        "Content-Type": "application/json",
    },
        method: "POST",
        body: JSON.stringify({
          url: [
            "https://otherurl1.url",
            "https://otherurl2.url",
            "https://otherurl3.url"
          ]
        })
  });

If window.localStorage.getItem("urls") is array you can put url:JSON.stringify(window.localStorage.getItem("urls")) and In PHP you can get

  $content = trim(file_get_contents("php://input")); //this will catch POST from JavaScript
  $decoded = json_decode($content, true); //this will decode from JSON to PHP array
  foreach($decoded['url'] as $url){
    echo $url;
  }

$decoded will be your array as PHP array

in PHP you can also check if post was sent as JSON

if ($_SERVER["CONTENT_TYPE"] === "application/json") {
    $content = trim(file_get_contents("php://input"));
    $decoded = json_decode($content, true);
}

To see as function/method, JavaScript will look like this

let fetchApi = () => {
    fetch("https://yourapi.api",
      {
        headers: {
            "Content-Type": "application/json",
        },
            method: "POST",
            body: JSON.stringify({
              url: window.localStorage.getItem("urls")
            })
      });
};

and PHP, you can use this everywhere

public static function get_data_from_js(){
    $contentType = isset($_SERVER["CONTENT_TYPE"]) ? trim($_SERVER["CONTENT_TYPE"]) : '';
    $decoded = [];
    if ($contentType === "application/json") {
        $content = trim(file_get_contents("php://input"));
        $decoded = json_decode($content, true);
    }else{
        //You can do here anything
    }
    return $decoded;
}
Tiffany
  • 175
  • 8