我對PHP相當陌生,我試圖用PHP表單添加一些節點,但我非常確定要執行該操作。從表單中獲取價值以使用PHP創建XML節點
我想要做的是從標準形式獲取不同元素的值,每個值設置爲一個節點,以便追加。
這將允許我在我的XML中創建(在我的示例中)具有名稱和註釋的另一個人。
毫安錯誤日誌告訴我,這些錯誤:
[19-Dec-2016 16:11:10 Europe/Berlin] PHP Notice: Undefined variable: name in /Applications/MAMP/htdocs/test-php/form.php on line 30
[19-Dec-2016 16:11:10 Europe/Berlin] PHP Notice: Trying to get property of non-object in /Applications/MAMP/htdocs/test-php/form.php on line 30
[19-Dec-2016 16:11:10 Europe/Berlin] PHP Notice: Undefined variable: comment in /Applications/MAMP/htdocs/test-php/form.php on line 31
[19-Dec-2016 16:11:10 Europe/Berlin] PHP Notice: Trying to get property of non-object in /Applications/MAMP/htdocs/test-php/form.php on line 31
[19-Dec-2016 16:11:15 Europe/Berlin] PHP Warning: Creating default object from empty value in /Applications/MAMP/htdocs/test-php/form.php on line 5
[19-Dec-2016 16:11:15 Europe/Berlin] PHP Warning: Creating default object from empty value in /Applications/MAMP/htdocs/test-php/form.php on line 6
[19-Dec-2016 16:11:15 Europe/Berlin] PHP Fatal error: Uncaught TypeError: Argument 1 passed to DOMNode::appendChild() must be an instance of DOMNode, instance of stdClass given in /Applications/MAMP/htdocs/test-php/form.php:16
看來,辦法,我試圖讓我的形式的值是行不通的。我究竟做錯了什麼 ?
非常感謝您的幫助。
我的PHP
<?php
if (isset($_POST['submit'])){
$name->nodeValue = $_POST['namanya'];
$comment->nodeValue = $_POST['commentnya'];
$xml = new DOMDocument('1.0', 'utf-8');
$xml->formatOutput = true;
$xml->preserveWhiteSpace = false;
$xml->load('examples.xml');
$inventors = $xml->getElementsByTagName('inventors')->item(0);
$person_inventor = $xml->createElement('person');
$name_inventor = $xml->createElement('name');
$name_inventor->appendChild($name);
$comment_inventor = $xml->createElement('comment');
$comment_invenotr->appendChild($comment);
$person_inventor->appendChild($name_inventor);
$person_inventor->appendChild($comment_inventor);
$inventors->appendChild($person_inventor);
htmlentities($xml->save('examples.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 name="submit" type="submit"/>
</form>
我的XML
<?xml version="1.0" encoding="UTF-8"?>
<inventors>
<person>
<name>anie</name>
<comment>good</comment>
</person>
</inventors>
嘗試創建的元素* $ name *和* $ comment *在使用'nodeValue'之前。 – Parfait
@Parfait是啊,我寫道: $ name = $ _POST ['namanya']; $ comment = $ _POST ['commentnya']; –
但是,我仍然收到相同的錯誤。 –