【发布时间】:2019-02-12 16:13:04
【问题描述】:
提前感谢您提供的任何帮助。
我正在尝试获取页面的当前 URL 并将其保存在 XML 表单中。如果我在代码中使用回显,它会显示 URL,但表单不会替换 XML 中的 url。我错过了什么?
我正在使用的代码:
<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>
对于 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>
根据 Joe 的 cmets,我将其更改为:
<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>
不过,它仍然不保存 URL。有关如何解决此问题的任何想法?
【问题讨论】:
-
$element->replaceChild($currentUrl, $currentUrl);看起来很奇怪。 -
我不知道这是什么意思..
-
您正在用自己替换子节点。我猜你的意思是
$currentUrl->nodeValue = $currentUrlString,其中$currentUrlString = $protocol . '://' . … -
嗨乔,谢谢..我只是这方面的初学者,我认为其他子节点是相似的(例如 $name)并且它适用于那些,所以它会是当前 URL 也是如此。我如何构建它以便实际保存 URL?