2017-08-29 298 views
0

我试图插入/提交表单数据到XML,但它没有保存任何东西到XML文件中..目前只发送Latitude行来让它工作。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> 

回答

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> 
+0

现在我明白了!谢谢! – Steve