通过HTML表单编辑XML(PHP) [英] Edit XML via HTML Form (PHP)

查看:176
本文介绍了通过HTML表单编辑XML(PHP)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个可以让你编辑XML标记内容的表单。我目前有一个form.php:

 <?php 
$ data = simplexml_load_file('welcome.xml' );
$ welcome = $ data-> item-> name;
?>

< form method =post>
< textarea name =name><?php echo $ welcome?>< / textarea>
< br>
< input type =submitname =submitvalue =submit>
< / form>

<?php
if(isset($ _ POST ['submit'])){
$ data = simplexml_load_file('welcome.xml');
$ data-> item-> name = $ _ POST ['name'];
$ handle = fopen(welcome.xml,wb);
fwrite($ handle,$ xml-> asXML());
fclose($ handle);
}
?>

和welcome.xml:

 <欢迎> 
< item>
<名称> $ welcome< / name>
< / item>
< / welcome>

当我按提交时并不保存输入内容,它只刷新页面并删除任何值在xml文件之前是..



更新

现在,但我已经添加了一个重置​​按钮,我需要它来清除xml文件,所以它只有< welcome> 标签。我已将 $ data-> item-> name = $ _ POST ['welcome']; 更改为 $ data =''; ,但会删除文本并保持标记不变。 您可以使用simplexml来完成这项工作。 / p>

从xml读取数据:

  $ data = simplexml_load_file('的welcome.xml'); 

$ welcome = $ data-> item [0] - > name;

并写入数据:

  $ data = simplexml_load_file('welcome.xml'); 

$ data-> item [0] - > name = $ _POST ['welcome'];

$ handle = fopen(welcome.xml,wb);
fwrite($ handle,$ xml-> asXML());
fclose($ handle);

编辑:
对于评论中的问题:

 <?php 
if(isset($ _ POST ['submit'])){
$ data = simplexml_load_file('welcome。 XML');
$ data-> item-> name = $ _ POST ['name'];
$ handle = fopen(welcome.xml,wb);
fwrite($ handle,$ data-> asXML());
fclose($ handle);
}

$ data = simplexml_load_file('welcome.xml');
$ welcome = $ data-> item-> name;

?>

< form method =post>
< textarea name =name><?php echo $ welcome?>< / textarea>
< br>
< input type =submitname =submitvalue =submit>
< / form>


I'm trying to create a form that will let you edit the contents of an xml tag. i currently have a form.php:

<?php
$data=simplexml_load_file('welcome.xml');
$welcome=$data->item->name;
?>

<form method="post">
    <textarea name="name"><?php echo $welcome ?></textarea>
    <br>
    <input type="submit" name="submit" value="submit">
</form>

<?php
if(isset($_POST['submit'])) {
$data=simplexml_load_file('welcome.xml');
$data->item->name=$_POST['name'];
$handle=fopen("welcome.xml","wb");
fwrite($handle,$xml->asXML());
fclose($handle);
}
?>

and welcome.xml:

<welcome>
    <item>
        <name>$welcome</name>
    </item>
</welcome>

when i press submit it doesn't save what's entered, it just refreshes the page and deletes whatever the value in the xml file was before..

UPDATE

The form works now, but I've added a reset button, i need it to clear the xml file so it only has the <welcome> tags. I've changed $data->item->name=$_POST['welcome']; to $data=''; but it deletes the text and keeps the tags still.

解决方案

You can do it with simplexml.

To read data from xml:

$data = simplexml_load_file('welcome.xml');

$welcome = $data->item[0]->name;

And to write data:

$data = simplexml_load_file('welcome.xml');

$data->item[0]->name = $_POST['welcome'];

$handle = fopen("welcome.xml", "wb"); 
fwrite($handle, $xml->asXML());
fclose($handle);

EDIT: For the question in the comment:

<?php
if(isset($_POST['submit'])) {
$data=simplexml_load_file('welcome.xml');
$data->item->name=$_POST['name'];
$handle=fopen("welcome.xml","wb");
fwrite($handle,$data->asXML());
fclose($handle);
}

$data=simplexml_load_file('welcome.xml');
$welcome=$data->item->name;

?>

<form method="post">
    <textarea name="name"><?php echo $welcome ?></textarea>
    <br>
    <input type="submit" name="submit" value="submit">
</form>

这篇关于通过HTML表单编辑XML(PHP)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆