通过html表格php加载,编辑和更新xml文件

时间:2019-11-23 18:11:23

标签: php xml forms simplexml

我正在尝试编写一个脚本来读取XML文件,将其插入表单中,然后使用php编辑和更新值。

该表单会加载当前数据,但不会更新它们。

有人可以告诉我小费吗? 谢谢毛里齐奥

文件XML

<?xml version="1.0" encoding="UTF-8"?>
<myxml>

  <data name="en_title"><![CDATA[mytitle]]></data>
  <data name="en_scene_title"><![CDATA[ES000021903]]></data>

</myxml>

文件编辑PHP

<?php
$form_fields  = null;
$data = simplexml_load_file('messages_en.xml');
foreach($data->data as $field)
{

  $form_fields .= '<div>';
    $form_fields .=  '<label>' .$field['name'] . ' </label>';
    $form_fields .=  '<input type="text" id="' .$field['name'] . '"placeholder="'.htmlentities($field).'" name="'.$field. '" />';
    $form_fields .= '</div>';
  }
  ?>
  <!DOCTYPE html>
  <html>
  <head>
    <style>
      form { font-size: 16px; }
      form div { margin: .3em; }
      legend { font-weight: bold; font-size: 20px; }
      label { display:inline-block; width: 140px; }
    </style>
  </head>
  <body>
    <form method="POST" action="process.php">
      <legend> Enter Contact Information</legend>
      <?php echo $form_fields; ?>
      <input type="submit" name="submit" value="Upload" class="btn"  />
    </form>
  </body>
  </html>

代码文件process.php

<?php
    $xml = file_get_contents('messages_en.xml');
    $sxml = simplexml_load_string($xml);
    if(isset($sxml->item[$_POST['name']])) {
        $node->data = $_POST['name'];
    }
    file_put_contents('messages_en.xml', $sxml->asXML());
?>

1 个答案:

答案 0 :(得分:0)

是否要基于const core = require('@actions/core'); function run() { try { // This is just a thin wrapper around bash, and runs a file called "script.sh" // // TODO: Change this to run your script instead // const script = require('path').resolve(__dirname, 'script.sh'); var child = require('child_process').execFile(script); child.stdout.on('data', (data) => { console.log(data.toString()); }); child.on('close', (code) => { console.log(`child process exited with code ${code}`); process.exit(code); }); } catch (error) { core.setFailed(error.message); } } run() 元素的name属性更新XML?

如果是这样,则需要a)从HTML中传递名称和值,并b)搜索所需的名称。

<data>