douxu2467 2014-06-19 20:12
浏览 20
已采纳

通过php保存xml数据并使用$ variables

I have seen many questions on here regarding saving data to xml and I have managed to create a form that does so (thanks guys who asked and answered), there is only one minor blip

Here is the code so you know what I mean

<?php

function insertxml($Iname,$Iurl)
{
    $doc = new DOMDocument;
    //$doc->preserveWhiteSpace = false;
    $doc->load("bookmark.xml");
    // we want a nice output
    //$doc->formatOutput = true;

    $root = $doc->getElementsByTagName('bookmark')->item(0);

    $title = $doc->createElement('url', $Iurl);
    $domAttribute = $doc->createAttribute('name');
    $domAttribute->value = ('$Iname');
    $title->appendChild($domAttribute);
    $newtitle = $root->appendChild($title);

    echo 'Wrote: ' . $doc->save("bookmark.xml") . ' bytes'; // Wrote: 72 bytes

}    

$Name = $_POST['name'];
$Url = $_POST['url'];

insertxml($Name,$Url);
?>

and the form looks like so:

<!DOCTYPE html>
<html>
<head>
<title> Xml Reader</title>
</head>
<body>

<form action="index.php" method="post">
Name: <input type="text" name="name"><br>
URL: <input type="text" name="url"><br>
<input type="submit">
</form>

</body>
</html>

the code works fine however when I put 'Test' in the name box and 'http://test.com' in the url box it comes out like this:

<url name="$Iname">http://test.com</url>

What i would like is

<url name="test">http://test.com</url>
<url name="(what ever name I put in the name box)">(what ever name I put in the url box)</url>

ultimatly what syntax should i put for the following?:

$title = $doc->createElement('url', $Iurl);
  • 写回答

1条回答 默认 最新

  • dqfwcj0030 2014-06-19 20:22
    关注

    remove the quotes from your variable

    ('$Iname')
    

    should be

    ($Iname)
    

    then remove the braces as they are not needed, too (there is nothing to group):

    ($Iname)
    

    should be

    $Iname
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效