简体   繁体   中英

Send Form data to XML

I'm using an XML file to display marker points on a Google Maps canvas, and am trying to implement a page where I can input data into a form and it saves it as a new marker within my data.xml file. I've attempted to follow the PHP manual, but can't seem to get it working, so wondered if you'd be able to help me out. The form submits, but nothing seems to go into the XML file. Am I doing something wrong inside my PHP? What am I missing that's actually going to send the data to my XML file? Here's the code I have so far:

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');

Then my XML is laid out as follows:

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

I don't understand the first rows of the PHP, but i do it like this and it's worked. You use the $sxe you add a child to it than you print it out and you save the xmldoc into file.

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');

The moredata.xml contains

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

So the difference is. You create a variable named $marker than you add attributes to the $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);

Your code:

$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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM