PHP - Convert PHP Array To JavaScript Array
Submitted by razormist on Thursday, August 16, 2018 - 13:27.
In this tutorial we will create a Convert PHP Array To Javascript Array using PHP. PHP is a server-side scripting language designed primarily for web development. Using PHP, you can let your user directly interact with the script and easily to learned its syntax. It is mostly used by a newly coders for its user friendly environment. So Let's do the coding...
There you have it we successfully created Convert PHP Array To Javascript Array using PHP. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!
Getting Started:
First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html. Lastly, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.Creating The Interface
This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php.- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
- <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
- </head>
- <body>
- <nav class="navbar navbar-default">
- <div class="container-fluid">
- <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
- </div>
- </nav>
- <div class="col-md-2"></div>
- <div class="col-md-8 well">
- <h3 class="text-primary">PHP - Convert PHP Array To Javascript Array</h3>
- <hr style="border-top:1px dotted #ccc;"/>
- <div class="col-md-6">
- <h4>PHP Array</h4>
- <?php
- "Philippines",
- "USA",
- "France",
- "Japan",
- "China",
- "Germany",
- "Africa"
- );
- echo "<pre>";
- echo "</pre>";
- ?>
- </div>
- <div class="col-md-6">
- <h4>Javascript Array</h4>
- <pre id="result"></pre>
- </div>
- <br style="clear:both;"/>
- <center><button onclick="arrayToJs()" class="btn btn-primary">Convert</button></center>
- </div>
- </body>
- <?php include 'convert.php'?>
- </html>
Creating Convert Script
This is the main function of the application. This code will convert the php array to javascript array when the button is clicked. To do that copy and write these inside the text editor, then save it as convert.php- <script type="text/javascript">
- function arrayToJs(){
- var target = document.getElementById('result');
- var country = [];
- for(var i=0;i<array.length;i++){
- country.push(array[i]);
- }
- target.innerHTML = JSON.stringify(country);
- }
- </script>
Add new comment
- 76 views