2016-12-19 98 views
0

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

嘗試創建的元素* $ name *和* $ comment *在使用'nodeValue'之前。 – Parfait

+0

@Parfait是啊,我寫道: $ name = $ _POST ['namanya']; $ comment = $ _POST ['commentnya']; –

+0

但是,我仍然收到相同的錯誤。 –

回答

0

若干問題的出現與當前的設置:

  1. 引用變量,$命名$評論是永遠初始化,因此未定義變量通知。
  2. 拼寫錯誤$ comment_invenotr
  3. 方法appendChild()不用於值,但用於元素。

考慮調整這樣的變量引用,甚至它們在代碼中的順序。而且因爲它似乎要分配$_POST值節點文本,可以考慮以下兩種方法之一:

的createElement參數

$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', $_POST['namanya']);  
$comment_inventor = $xml->createElement('comment', $_POST['commentnya']); 

$person_inventor->appendChild($name_inventor); 
$person_inventor->appendChild($comment_inventor); 
$inventors->appendChild($person_inventor); 

htmlentities($xml->save('examples.xml')); 

的nodeValue分配

$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->nodeValue = $_POST['namanya']; 

$comment_inventor = $xml->createElement('comment'); 
$comment_inventor->nodeValue = $_POST['commentnya']; 

$person_inventor->appendChild($name_inventor); 
$person_inventor->appendChild($comment_inventor); 
$inventors->appendChild($person_inventor); 

htmlentities($xml->save('examples.xml')); 
+0

我也必須更改PHP表單以刪除value屬性以刪除其他錯誤日誌!非常感謝您的幫助。 –