如何使用PHP通过HTML表单在JSON文件中附加数据?

2021年4月28日19:58:50 发表评论 1,174 次浏览

本文概述

本文的目的是使用PHP通过HTML表单将数据附加到JSON文件中。

方法1:如果未创建JSON文件, 则我们将创建一个新的JSON文件, 向其发送数据, 然后在其中添加数据。要了解如何通过从HTML表单获取数据来创建JSON文件, 请参阅此链接。

方法二:如果已经创建了JSON文件, 则我们直接将数据附加到JSON文件。要将数据从HTML表单发送到JSON文件, 我们正在使用json_encode()该函数返回JSON编码的字符串。

我们正在制作一个由用户填写HTML表单的值数组。然后我们将这个数组传递给json_encode()功能。的json_encode()函数返回JSON编码的字符串。为了创建一个JSON文件, 我们使用了PHPfile_put_contents()用于将数据写入文件。我们传入2个参数file_put_contents()功能。第一个参数是我们要以JSON格式存储数据的文件名, 第二个参数是我们的PHPget_data()功能。

根据第一种方法, 我们将使用来成功创建一个JSON文件。json_encode()功能。现在, 我们的JSON文件已创建。下一个任务是将数据追加到该JSON文件。要将数据附加到JSON文件, 我们必须将先前的数据存储到变量中。要获取JSON文件的数据, 我们将使用file_get_contents()功能。的file_get_contents()将文件读取为字符串。要解码字符串, json_decode()使用的function是PHP中的内置函数, 用于解码JSON字符串。该函数将JSON编码的字符串转换为PHP变量。数组中的HTML数据分配给我们的解码字符串。的json_encode()功能和file_put_contents()用于编码字符串并将内容分别放入JSON文件。

例子:以下HTML和PHP代码演示了上述方法。

HTML

<html> 
  
<head> 
     <meta charset = "UTF-8"> 
  
     <style> 
         h3 { 
             text-align: center; 
         } 
  
         img { 
             display: block; 
             margin: auto; 
             height: 150px; 
             width: 150px; 
         } 
  
         .input { 
             margin: 6px; 
             padding: 10px; 
             display: block; 
             margin: auto; 
             color: palevioletred; 
             font-size: 30px; 
         } 
  
         input { 
             width: 90%; 
             display: block; 
             margin-left: 12px; 
             background: none; 
             background-color: lightyellow; 
         } 
  
         select { 
             width: 90%; 
             display: block; 
             margin-left: 12px; 
             background: none; 
             background-color: lightyellow; 
         } 
  
         #heading { 
             font-family: cursive; 
             text-align: center; 
             color: green; 
             padding-top: 20px; 
  
         } 
  
         #form_page { 
             height: 500px; 
             width: 50%; 
             display: flex; 
             flex-wrap: wrap; 
             flex-direction: row; 
             margin: auto; 
  
         } 
  
         #form_body { 
             border-radius: 12px; 
             height: 330px; 
             width: 450px; 
             background-color: beige; 
             border: 1px solid pink; 
             margin: auto; 
             margin-top: 12px; 
         } 
  
         #text { 
             color: red; 
             width: 100px; 
         } 
  
         #head { 
             border-bottom: 2px solid red; 
             height: 100px; 
             background-color: aliceblue; 
         } 
  
         #submit { 
             background-color: white; 
             width: 70px; 
         } 
     </style> 
</head> 
  
<body> 
     <form method = "post" action = "gfg.php"> 
  
         <div id = "form_page"> 
  
             <div id = "form_body"> 
                 <div id = "head"> 
                     <h1 id = "heading">GFG</h1> 
                 </div> 
                 <br /> 
                 <div id = "input_name" class = "input"> 
                     <input id = "name" type = "text"
                         Placeholder = "Name" name = "name"
                         required> 
                 </div> 
                 <div id = "input_class" class = "input"> 
                     <input type = "text" placeholder = 
                         "Branch" name = "branch" required> 
                 </div> 
                 <div id = "input_year" class = "input"> 
                     <input id = "school" type = "text"
                         name = "year"
                         placeholder = "Year"> 
                 </div> 
  
                 <div class = "id input"> 
                     <input id = "submit" type = "submit"
                         name = "submit" value = "submit"
                         onclick = "on_submit()"> 
                 </div> 
             </div> 
         </div> 
     </form> 
</body> 
   
</html>

PHP代码:以下是上述HTML文件中使用的" gfg.php"文件。

PHP

<?php
       
if ( $_SERVER [ 'REQUEST_METHOD' ] == 'POST' ) {
                   
     function get_data() {
         $name = $_POST [ 'name' ];
         $file_name = 'StudentsData' . '.json' ;
   
         if ( file_exists ( "$file_name" )) { 
             $current_data = file_get_contents ( "$file_name" );
             $array_data =json_decode( $current_data , true);
                               
             $extra = array (
                 'Name' => $_POST [ 'name' ], 'Branch' => $_POST [ 'branch' ], 'Year' => $_POST [ 'year' ], );
             $array_data []= $extra ;
             echo "file exist<br/>" ;
             return json_encode( $array_data );
         }
         else {
             $datae = array ();
             $datae []= array (
                 'Name' => $_POST [ 'name' ], 'Branch' => $_POST [ 'branch' ], 'Year' => $_POST [ 'year' ], );
             echo "file not exist<br/>" ;
             return json_encode( $datae );   
         }
     }
  
     $file_name = 'StudentsData' . '.json' ;
      
     if ( file_put_contents ( "$file_name" , get_data())) {
         echo 'success' ;
     }                
     else {
         echo 'There is some error' ;                
     }
}
       
?>

输出如下:" StudentsData.json"文件的内容以JSON格式显示数据。

如何使用PHP通过HTML表单在JSON文件中附加数据?1

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: