Index.php - PHP Online
Form of PHP Sandbox
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php
in the places where the PHP script should be executed.
Result of php executing
Full code of Index.php.php
- <?php
- $url = 'http://country.io/names.json'; //url para consumir y obtener los paises
- $curlSession = curl_init(); //inicializacion y propiedades del metodo curl para obtener datos
- curl_setopt($curlSession, CURLOPT_URL, $url);
- curl_setopt($curlSession, CURLOPT_BINARYTRANSFER, true);
- curl_setopt($curlSession, CURLOPT_RETURNTRANSFER, true);
- $jsonData = json_decode(curl_exec($curlSession)); //parse de datos json
- curl_close($curlSession);
- $buffer ='<select name="pais" id="pais">'; //construccion de select box
- foreach($jsonData as $key => $name){
- $buffer .='<option value="'.$key.'">'.$key.' - '.$name.'</option>';
- }
- $buffer .= '</select>';
- ?>
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="UTF-8">
- </head>
- <body>
- <h1>Formulario de Registro</h1>
- <form name="registro" method="POST" action="/registro.php">
- <label for="nombre">Nombre:</label>
- <input type="text" id="nombre" name="nombre" required>
- <br><br>
- <label for="pais">Pais:</label>
- <?=$buffer?>
- <br><br>
- <label for="celular">Celular:</label>
- <input type="text" id="celular" name="celular" required>
- <br><br>
- <label for="email">Correo Electornico:</label>
- <input type="text" id="email" name="email" required>
- <br><br>
- <button type="submit" value="submit">Submit</button>
- </form>
- <p>Nota: el formulario utilizara un archivo json local como base de datos.
- <h1>Usuarios Registrados:</h1>
- <div id="usuarios"></div>
- <script type="text/javascript">
- document.addEventListener("DOMContentLoaded", function () {
- //analisis de json para construir el hader
- function addHeaders(table, keys) {
- var row = table.insertRow();
- for( var i = 0; i < keys.length; i++ ) {
- var cell = row.insertCell();
- cell.appendChild(document.createTextNode(keys[i]));
- }
- }
- //funcion que dibuja la tabla
- function dibujartabla(data){
- var children = data;
- var table = document.createElement('table');
- table.style.border = "1px solid";
- table.setAttribute("border", "1");
- for( var i = 0; i < children.length; i++ ) {
- var child = children[i];
- if(i === 0 ) {
- addHeaders(table, Object.keys(child));
- }
- var row = table.insertRow();
- Object.keys(child).forEach(function(k) {
- var cell = row.insertCell();
- cell.appendChild(document.createTextNode(child[k]));
- })
- }
- document.getElementById('usuarios').appendChild(table);
- }
- //fetch para leer los datos de los usuarios registrdos
- fetch('registros.json')
- .then(response => response.json())
- .then(data => dibujartabla(data))
- .catch(error => console.log(error));
- });
- </script>
- </body>
- </html>
File Description
- Index.php
- PHP Code
- 13 Oct-2021
- 2.65 Kb
You can Share it:
Latest PHP Pastes