html:
<!DOCTYPE html> <html lang="en" dir="ltr"> <head> <meta charset="utf-8"> <title>test</title> <link rel="stylesheet" href="style.css?v=2"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css"/> </head> <body> <div class="wrapper"> <div class="search-input"> <form method="post" action="store.php"> <input type="text" placeholder="Type to search.."> <input type="submit" value="Done"/> <div class="autocom-box"> <!-- here list are inserted from javascript --> </div> <div class="icon"><i class="fas fa-search"></i></div> </div> </div> <script src="suggestions.js?v=3"></script> <script src="js/script.js?v=2"></script> </body> </html>sugerencia.js
let suggestions = [ "YouTube", "YouTuber", "YouTube Channel", "Blogger", ];Necesito eso cuando el usuario escribe algo en la entrada para que los datos se guarden y se escriban en json, pero no sé cómo no funcionó para mí a través de php, así que necesitaría ayuda para hacerlo en json.
tienda.php
<?php $myfile = fopen("suggestions.js", "a+"); $txt = "Login name : ".$_POST['name'].; fwrite($myfile, $txt); fclose($myfile); ?>$jsonFile = '/path/to/your/json/suggestion.js'; $data = (file_exists($jsonFile)) ? json_decode(preg_replace('#^let suggestions = \[(.+)\];\s*$#', '$1', file_get_contents($jsonFile))) : []; $data[] = $_POST['name']; file_put_contents($jsonFile, 'let suggestions = ' . json_encode($data) . ';');