Geocomplete to XML

时间:2017-08-29 10:09:05

标签: php xml forms geolocation

我正在尝试将表单数据插入/提交到XML,但它没有将任何内容保存到XML文件中..目前只发送纬度行以使其工作。

我一直在处理GitHub Geocomplete表单脚本并试图将发送数据添加到XML脚本但我看不到缺少的内容?

INDEX.PHP

<!DOCTYPE html>
<html>
  <head>
    <title>$.geocomplete()</title>
    <meta charset="UTF-8">

    <style type="text/css" media="screen">
      form { width: 300px; float: left; }
      fieldset { width: 320px; margin-top: 20px}
      fieldset strong { display: block; margin: 0.5em 0 0em; }
      fieldset input { width: 95%; }

      ul span { color: #999; }
    </style>
  </head>
  <body>

<?php
    $xmldoc = new DOMDocument();
    $xmldoc->load('sample.xml', LIBXML_NOBLANKS);

    $latitude = $xmldoc->firstChild->firstChild;
    if($latitude!=null){
        while($latitude!=null){
            echo $latitude->textContent.'<br/>';
            $latitude = $latitude->nextSibling;
        }
    }
?>

    <div class="map_canvas"></div>

    <form name='input' action='insert.php' method='post'>
      <input id="geocomplete" type="text" placeholder="Type in an address" value="Empire State Bldg" />
      <input id="find" type="button" value="find" />

      <fieldset>
        <h3>Address-Details</h3>


        <label>Latitude</label>
        <input name="latitude" type="text" value="">

        <label>Longitude</label>
        <input name="lng" type="text" value="">

        <label>Formatted Address</label>
        <input name="formatted_address" type="text" value="">

        <label>Locality</label>
        <input name="locality" type="text" value="">

      </fieldset>

<input type='submit' value='send'/>
    </form>






    <script src="http://maps.googleapis.com/maps/api/js?key=API KEY sensor=false&amp;libraries=places"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>

    <script src="findlocation/jquery.geocomplete.js"></script>

    <script>
      $(function(){
        $("#geocomplete").geocomplete({
          map: ".map_canvas",
          details: "form",
          types: ["geocode", "establishment"],
        });

        $("#find").click(function(){
          $("#geocomplete").trigger("geocode");
        });
      });
    </script>

  </body>
</html>

INSERT.PHP

<?php
    header('Location:index.php');
    $xmldoc = new DOMDocument();
    $xmldoc->load('sample.xml');

    $newAct = $_POST['latitude'];

    $root = $xmldoc->firstChild;

    $newElement = $xmldoc->createElement('latitude');
    $root->appendChild($newElement);
    $newText = $xmldoc->createTextNode($newAct);
    $newElement->appendChild($newText);

    $xmldoc->save('sample.xml');
?>

sample.xml中

<list>
    <latitude></latitude>
<list>

1 个答案:

答案 0 :(得分:0)

您的代码存在一些问题,因此我会在代码中发布代码和注释应该有所帮助。

<!DOCTYPE html>
<html>
<head>
<title>$.geocomplete()</title>
<meta charset="UTF-8">

<style type="text/css" media="screen">
form {
    width: 300px;
    float: left;
}

fieldset {
    width: 320px;
    margin-top: 20px
}

fieldset strong {
    display: block;
    margin: 0.5em 0 0em;
}

fieldset input {
    width: 95%;
}

ul span {
    color: #999;
}
</style>
</head>
<body>

<?php
$xmldoc = new DOMDocument ();
$xmldoc->load ( 'sample.xml', LIBXML_NOBLANKS );
// Fetch the latitude - using getElementsByTagName is a quick way of getting the
// data for a known element
$latitude = $xmldoc->getElementsByTagName ( 'latitude' ) [0]->nodeValue;
?>

    <div class="map_canvas"></div>

    <form name='input' action='insert.php' method='post'>
        <input id="geocomplete" type="text" placeholder="Type in an address"
            value="Empire State Bldg" /> <input id="find" type="button"
            value="find" />

        <fieldset>
            <h3>Address-Details</h3>
            <label>Latitude</label> 
            <input name="latitude" type="text"
                value="<?php echo $latitude; ?>"> 
            <label>Longitude</label> 
            <input
                name="lng" type="text" value=""> 
            <label>Formatted Address</label> 
            <input
                name="formatted_address" type="text" value=""> 
            <label>Locality</label>
            <input name="locality" type="text" value="">
        </fieldset>
        <input type='submit' value='send' />
    </form>
    <script
        src="http://maps.googleapis.com/maps/api/js?key=API KEY sensor=false&amp;libraries=places"></script>
    <script
        src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>

    <script src="findlocation/jquery.geocomplete.js"></script>

    <script>
      $(function(){
        $("#geocomplete").geocomplete({
          map: ".map_canvas",
          details: "form",
          types: ["geocode", "establishment"],
        });

        $("#find").click(function(){
          $("#geocomplete").trigger("geocode");
        });
      });
    </script>

</body>
</html>

insert.php 由于您的XML已包含纬度元素,因此无需创建新元素。

<?php
if ( isset($_POST['latitude']) ){
    $xmldoc = new DOMDocument();
    $xmldoc->load('sample.xml');

    // Fetch node to update (use [0] sd getElementsByTagName returns
    // an array of matching nodes)
    $latitude = $xmldoc->getElementsByTagName('latitude')[0];
    // Set node value
    $latitude->nodeValue = $_POST['latitude'];
    $xmldoc->save('sample.xml');

}

// Now the data is processed, redirect to main page
header('Location:index.php');

sample.xml中 (请注意,您的关闭列表元素是否而非

<list>
    <latitude></latitude>
</list>