2017-12-03 73 views
0

到目前为止,我已经设置了窗体和服务来读取应该对json文件进行编辑的php文件。我目前的PHP文件看起来像这样。如何使用Angular 4中的表单通过PHP更新json文件?

if(empty($errors)) 
      {   
      $postdata = file_get_contents("php://input"); 
      $request = json_decode($postdata);  
      $name = $request->name; 
      $designation = $request->designation; 

      $current_data = file_get_contents('currency.json'); 
      $array_data = json_decode($current_data, true); 
      $extra = array( 
        'name'   =>  $name, 

        'designation'  =>  $designation 
      ); 
      $array_data[] = $extra; 
      $final_data = json_encode($array_data); 
      if(file_put_contents('currency.json', $final_data)) 
      { 
        $message = "<label class='text-success'>File Appended Success fully</p>"; 
      } 
      } 
      else 
      { 
       $error = 'JSON File not exits'; 
      } `enter code here` 

I will appreciate any effort for help. 
+0

hello @Kiril。你能否提供更多细节? –

+0

这里有什么问题?为什么使用php://输入来读取通过表单提交的数据? –

回答

0

好吧,我已经花了大约12个小时,我有一个解决方案:

 if(file_exists('currency.json')) 
     { 
     $jsonString = file_get_contents("php://input"); 

     date_default_timezone_set("Europe/Skopje"); 
     $todayDate= date("d.m.Y"); 

     $data = json_decode($jsonString); 

     $eur=array(
       'currency' => 'EUR', 
       'buy' => (double)$data->eurBuy, 
       'sell' => (double)$data->eurSell, 
       'flag' => "flag--europe" 
     ); 
     $aud=array(
       'currency' => 'AUD', 
       'buy' => (double)$data->audBuy, 
       'sell' => (double)$data->audSell, 
       'flag' => "flag--australia" 
      ); 

     $currencies=array($eur, $aud); 

     $obj = array('datum' => $todayDate,'values' => $currencies); 

     $newJsonString = json_encode($obj); 
     file_put_contents('currency.json', $newJsonString); 

}

希望不久我将把示范项目,这样我就可以帮助在未来更加精确以缓解开发者。映入眼帘!