Php未定义索引写入xml文件
我正在用php从html表单编写一个xml,由于这个问题,我无法继续。。谢谢你的帮助Php未定义索引写入xml文件,xml,php,Xml,Php,我正在用php从html表单编写一个xml,由于这个问题,我无法继续。。谢谢你的帮助 <?php $root = array(); $root [] = array( 'subtitle' => $_POST['subtitle'], ); echo $_POST['subtitle'];//checker if POST really passes data $doc = new DOMDocument(); $doc->formatOutp
<?php
$root = array();
$root [] = array(
'subtitle' => $_POST['subtitle'],
);
echo $_POST['subtitle'];//checker if POST really passes data
$doc = new DOMDocument();
$doc->formatOutput = true;
$r = $doc->createElement( "root" );
$doc->appendChild( $r );
$subtitle = $doc->createElement( "subtitle" );
$subtitle->appendChild($doc->createTextNode( $root['subtitle'])); --Undefined index-
$r->appendChild( $subtitle );
代码确实生成了一个xml文件,但节点是空的
<?xml version="1.0"?>
<root>
<subtitle></subtitle>
</root>
谢谢
在表单中编写print_r($root)和qwerty。这是输出
数组([0]=>Array([subtitle]=>qwerty))您正在执行的
$root[]=Array(…)
。因此,它在$root
数组的索引0处创建另一个数组
试着做:
$subtitle->appendChild($doc->createTextNode($root[0]['subtitle']));
或者在初始化
$root
数组时删除括号。请将错误消息复制给我们好吗?尝试打印($root)以查看数组中包含的内容。注意:未定义的索引:subtitle in…\testcode.php@chrischooney可能与$root=数组(…)重复或仅从中删除[]
declaration:)是的,我只是把它添加到我的答案中;)完美的我被这个问题困扰了好几个小时,甚至尝试重建我的xml模板。哈哈,奇怪的括号。谢谢
$subtitle->appendChild($doc->createTextNode($root[0]['subtitle']));