İsa Diliballı
İsa Diliballı

Reputation: 515

Submit the array from JavaScript to PHP


  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?


Upvotes: 0

Views: 129

Answers (1)

Tiffany
Tiffany

Reputation: 503

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;
}

Upvotes: 2

Related Questions