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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?