2014-02-10 69 views
0

我使用的XML文件显示在谷歌地图的画布标记点,想实现一个网页,我可以将数据输入格式并保存它作为中新的标记我data.xml文件。我试图按照PHP手册,但似乎无法让它工作,所以想知道你是否能够帮助我。表单提交,但似乎没有进入XML文件。我在PHP里面做错了什么?我错过了什么是实际将数据发送到我的XML文件?下面的代码我到目前为止:将表单数据发送到XML

HTML:

<form name="input" action="map.php" method="post"> 
     <p>Name</p> 
     <input type="text" name="name" placeholder="Name of road/junction"/> 
     <p>Latitude</p> 
     <input type="text" name="lat" placeholder="Latitude (should start with 54)"/> 
     <p>Longitude</p> 
     <input type="text" name="lng" placeholder="Longitude (should start with -2)"/> 
     <p>Image</p> 
     <input type="text" name="img" placeholder="include - images/"/> 
     <p>Custom Marker</p> 
     <input type="text" name="custommarker" placeholder="car.png"/> 
     <p>Description</p> 
     <input type="text" name="description" placeholder="Description of junction with tips"/> 
     <input type="submit" value="send"/> 
    </form> 

PHP:

$sxe = new SimpleXMLElement($xmlstr); 
$xmldoc->load('../data.xml'); 


$name = $_POST['name']; 
$lat = $_POST['lat']; 
$lng = $_POST['lng']; 
$img = $_POST['img']; 
$custommarker = $_POST['custommarker']; 
$description = $_POST['description']; 

$root = $xmldoc->firstChild; 

$marker = $sxe->addChild('marker'); 
$root->addAttribute('name', $name); 
$root->addAttribute('lat', $lat); 
$root->addAttribute('lng', $lng); 
$root->addAttribute('img', $img); 
$root->addAttribute('custommarker', $custommarker); 
$root->addAttribute('description', $description); 

echo $sxe->asXML(); 
$xmldoc->save('../moredata.xml'); 

然后我的XML布局为如下:

<markers> 
    <marker name="" lat="" lng="" img="" custommarker="" description "" /> 
</markers> 
+0

你不解释你的代码是如何失败的,以满足您的期望。事实上,你甚至不会提出问题。你能编辑这个问题并解决这个问题吗? –

回答

0

我不了解PHP的第一行,但我这样做,它的工作。 您使用$ sxe添加一个子对象,然后将其打印出来并将xmldoc保存到文件中。

data.xml中

<?xml version="1.0" encoding="utf-8"?> 
<markers/> 

map.php

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

$name = $_POST['name']; 
$lat = $_POST['lat']; 
$lng = $_POST['lng']; 
$img = $_POST['img']; 
$custommarker = $_POST['custommarker']; 
$description = $_POST['description']; 

$marker = $xmldoc->addChild('marker'); 
$marker->addAttribute('name', $name); 
$marker->addAttribute('lat', $lat); 
$marker->addAttribute('lng', $lng); 
$marker->addAttribute('img', $img); 
$marker->addAttribute('custommarker', $custommarker); 
$marker->addAttribute('description', $description); 

echo $xmldoc->asXML(); 
$xmldoc->asXML('moredata.xml'); 

moredata.xml包含

<?xml version="1.0" encoding="utf-8"?> 
<markers> 
<marker name="[[$name]]" lat="[[$lat]]" lng="[[$lng]]" img="[[$img]]" custommarker="[[$custommarker]]" description="[[$description]]"/> 
</markers> 

所以区别在于。 您可以创建名为$ marker的变量,而不是将属性添加到$ xmldoc-> firstChild。

$marker = $xmldoc->addChild('marker'); 
$marker->addAttribute('name', $name); 
$marker->addAttribute('lat', $lat); 
$marker->addAttribute('lng', $lng); 
$marker->addAttribute('img', $img); 
$marker->addAttribute('custommarker', $custommarker); 
$marker->addAttribute('description', $description); 

您的代码:

$marker = $sxe->addChild('marker'); 
$root->addAttribute('name', $name); 
$root->addAttribute('lat', $lat); 
$root->addAttribute('lng', $lng); 
$root->addAttribute('img', $img); 
$root->addAttribute('custommarker', $custommarker); 
$root->addAttribute('description', $description); 
+0

太棒了。谢谢你的帮助。现在已经开始工作了,PHP一定不会喜欢那些开头的几行。 – Bic1245