获取当前URL并保存到XML文件

and thanks in advance for any help you can offer.

I'm trying to get the current URL of the page and save it in the XML form. If I use the echo in the code, it shows the URL but the form doesn't replace the url in the XML.. what am I missing?

The code I'm using:

 <script src="http://code.jquery.com/jquery-latest.min.js"></script>
 <?php
 $xml = new DOMDocument('1.0', 'utf-8');
 $xml->formatOutput = true; 
 $xml->preserveWhiteSpace = false;
 $xml->load('data.xml');

 //Get item Element
 $element = $xml->getElementsByTagName('person')->item(0);  

 //Load child elements
 $name = $element->getElementsByTagName('name')->item(0);
 $comment = $element->getElementsByTagName('comment')->item(0);
 $currentUrl = $element->getElementsByTagName('currentUrl')->item(0);

 //Replace old elements with new
 $element->replaceChild($name, $name);
 $element->replaceChild($comment, $comment);
 $element->replaceChild($currentUrl, $currentUrl);

 

 ?>
<?php
$protocol = strpos(strtolower($_SERVER['SERVER_PROTOCOL']),'https') 
                === FALSE ? 'http' : 'https';
$host     = $_SERVER['HTTP_HOST'];
$script   = $_SERVER['SCRIPT_NAME'];
$params   = $_SERVER['QUERY_STRING'];
$currentUrl = $protocol . '://' . $host . $script . '?' . $params;
echo $currentUrl;
?>

 <?php
 if (isset($_POST['submit']))
 {
$name->nodeValue = $_POST['namanya'];
$comment->nodeValue = $_POST['commentnya'];
$currentUrl->nodeValue = $_POST['currentUrlya'];
htmlentities($xml->save('data.xml'));

 }

 ?>

 <form method="POST" action=''>
  name <input type="text-name" value="<?php echo $name->nodeValue  ?>" name="namanya" />
comment  <input type="text-comment" value="<?php echo $comment->nodeValue  ?>"  name="commentnya"/>
<input type="hidden" name="currentUrlya" value="<?php echo $currentUrl->nodeValue  ?>">

 <input name="submit" type="submit" />
 </form>

and for the XML:

<?xml version="1.0" encoding="UTF-8"?>
<inventors>
  <person>
    <name>david</name>
    <comment> yes, so powwow</comment>
    <currentUrl>http://google.com</currentUrl>
  </person>
</inventors>

Based on Joe's comments I changed it to:

 <script src="http://code.jquery.com/jquery-latest.min.js"></script>
 <?php
 $xml = new DOMDocument('1.0', 'utf-8');
 $xml->formatOutput = true; 
 $xml->preserveWhiteSpace = false;
 $xml->load('data.xml');



$protocol = strpos(strtolower($_SERVER['SERVER_PROTOCOL']),'https') 
                === FALSE ? 'http' : 'https';
$host     = $_SERVER['HTTP_HOST'];
$script   = $_SERVER['SCRIPT_NAME'];
$params   = $_SERVER['QUERY_STRING'];
$currentUrl = $protocol . '://' . $host . $script . '?' . $params;


 //Get item Element
 $element = $xml->getElementsByTagName('person')->item(0);  

 //Load child elements
 $name = $element->getElementsByTagName('name')->item(0);
 $comment = $element->getElementsByTagName('comment')->item(0);
 $currentUrl2 = $element->getElementsByTagName('currentUrl')->item(0);

 //Replace old elements with new
 $element->replaceChild($name, $name);
 $element->replaceChild($comment, $comment);
 $element->replaceChild($currentUrl2, $currentUrl2);

 ?>
 
<?php

?>

 <?php
 if (isset($_POST['submit']))
 {
$name->nodeValue = $_POST['namanya'];
$comment->nodeValue = $_POST['commentnya'];
$currentUrl2->nodeValue = $_POST['currentUrlya'];
htmlentities($xml->save('data.xml'));

 }

 ?>

 <form method="POST" action=''>
  name <input type="text-name" value="<?php echo $name->nodeValue  ?>" name="namanya" />
comment  <input type="text-comment" value="<?php echo $comment->nodeValue  ?>"  name="commentnya"/>
<input type="hidden" name="currentUrlya" value="<?php echo $currentUrl->nodeValue  ?>">

 <input name="submit" type="submit" />
 </form>

it still doesn't save the URL, though. any ideas on how to fix this?

</div>

Your code emits a Notice: Trying to get property 'nodeValue' of non-object in …

The reason is, that you are overriding the $currentUrl DOM node variable with a string value $currentUrl = $protocol . '://' . $host . …. Therefore, setting the new DOM node value later with $currentUrl->nodeValue = … will fail.

Always use different names for different things. And keep watching your error log – it should generally stay empty.