PHP Sending Array to URL using JSON Encode/Decode
Submitted by nurhodelta_17 on Tuesday, September 12, 2017 - 15:40.
This tutorial features a way to send array to url using json encode/decode. Of course, there are a number of ways to send array to url but for me, I found this efficient when it comes to sending arrays. Also, I've included three ways to send to url which are via href, php header and script location. So, let's get started.
That ends this tutorial. Hope this helps. Happy coding :)
Creating Sample Array
First step is to create our sample array with the 3 ways to send to url. We name this as our "index.php".- <!DOCTYPE html>
- <html>
- <head>
- <title>PHP Sending Array to URL using JSON Encode/Decode</title>
- </head>
- <body>
- <?php
- );
- ?>
- <h4>Result:</h4>
- <ul>
- <?php
- foreach ($myarray as $data):
- ?>
- <li><?php echo $data['fruit'] ?>: <?php echo $data['quantity'] ?> </li>
- <?php
- endforeach;
- ?>
- </ul>
- <h3>Send via:</h4>
- <a href="header.php?myarray=<?php echo $url; ?>">PHP Header</a>
- <a href="script.php?myarray=<?php echo $url; ?>">Script Location</a>
- </body>
- </html>
Creating our Header Page
Next we create a header page that will direct us to our goto page. The purpose of this is to showcase the way to send via php header. We name this as "header.php".- <?php
- //encode again
- //send via header
- ?>
Creating our Script Page
If we have a page for header, we are also going to create a pge for script that will also redirect as to our goto page. We name this as our "script.php".- <?php
- //encode again
- //send via location
- ?>
- <script>
- var data='<?php echo $data; ?>';
- window.location.href='goto.php?myarray='+data;
- </script>
- <?php
- ?>
Creating our Goto Page
Lastly, we create our goto page. This page will retrieve our array that we send to url. We name this as "goto.php".- <!DOCTYPE html>
- <html>
- <head>
- <title>PHP Sending Array to URL using JSON Encode/Decode</title>
- </head>
- <body>
- <h4>Result:</h4>
- <ul>
- <?php
- foreach ($myarray as $data):
- ?>
- <li><?php echo $data['fruit'] ?>: <?php echo $data['quantity'] ?> </li>
- <?php
- endforeach;
- ?>
- </ul>
- <a href="index.php">Back</a>
- </body>
- </html>
Add new comment
- 276 views