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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的