Remove XML elements based on user input
<p>I would like to remove a XML entry based on user input. </p>

<p>XML Example:</p>

<pre><code>&lt;YealinkIPPhoneDirectory&gt;
&lt;DirectoryEntry&gt;
&lt;Name&gt;Test321&lt;/Name&gt;
&lt;Telephone&gt;101&lt;/Telephone&gt;
&lt;Telephone&gt;102&lt;/Telephone&gt;
&lt;Telephone&gt;103&lt;/Telephone&gt;
&lt;/DirectoryEntry&gt;
&lt;DirectoryEntry&gt;
&lt;Name&gt;Test456&lt;/Name&gt;
&lt;Telephone&gt;107&lt;/Telephone&gt;
&lt;Telephone&gt;108&lt;/Telephone&gt;
&lt;Telephone&gt;109&lt;/Telephone&gt;
&lt;/DirectoryEntry&gt;
&lt;DirectoryEntry&gt;
&lt;Name&gt;Test789&lt;/Name&gt;
&lt;Telephone&gt;000&lt;/Telephone&gt;
&lt;Telephone&gt;111&lt;/Telephone&gt;
&lt;Telephone&gt;222&lt;/Telephone&gt;
&lt;/DirectoryEntry&gt;
&lt;/YealinkIPPhoneDirectory&gt;
</code></pre>

<p>So remove entries DirectoryEntry to the DirectoryEntry end. The entry works fine and submits the data properly formatted:</p>

<pre><code>&lt;?php 
if (isset($_REQUEST['ok'])) { 
$xml = new DOMDocument("1.0","UTF-8"); 
$xml-&gt;load("emergency.xml"); 
$rootTag = $xml-&gt;getElementsByTagName("YealinkIPPhoneDirectory")-&gt;item(0); 
$dataTag = $xml-&gt;createElement("DirectoryEntry"); 
$aTag = $xml-&gt;createElement("Name",$_REQUEST['a']); 
$bTag = $xml-&gt;createElement("Telephone",$_REQUEST['b']); 
$cTag = $xml-&gt;createElement("Telephone",$_REQUEST['c']);
$dTag = $xml-&gt;createElement("Telephone",$_REQUEST['d']);
$dataTag-&gt;appendChild($aTag); 
$dataTag-&gt;appendChild($bTag); 
$dataTag-&gt;appendChild($cTag);
$dataTag-&gt;appendChild($dTag);
$rootTag-&gt;appendChild($dataTag); 
$xml-&gt;save("emergency.xml");
 } 
 ?&gt; 
 &lt;form class="form-horizontal" action="emergencycontacts.php" method="post"&gt;
  &lt;form class="form-horizontal" action="emergencycontacts.php" method="post"&gt;
&lt;fieldset&gt;

&lt;!-- Form Name --&gt;
&lt;legend&gt; &lt;/legend&gt;

&lt;!-- Text input--&gt;
&lt;div class="form-group"&gt;
  &lt;label class="col-md-4 control-label"&gt;Full Name&lt;/label&gt;  
  &lt;div class="col-md-5"&gt;
 &lt;input class="form-control input-md" required="" type="text" name="a" /&gt; 
 &lt;span class="help-block"&gt;Required&lt;/span&gt;  
  &lt;/div&gt;
&lt;/div&gt;


&lt;!-- Text input--&gt;
&lt;div class="form-group"&gt;
  &lt;label class="col-md-4 control-label"&gt;Extension&lt;/label&gt;  
  &lt;div class="col-md-2"&gt;
 &lt;input class="form-control input-md" value=" " type="text" name="b" /&gt; 
 &lt;span class="help-block"&gt;Internal&lt;/span&gt;  
  &lt;/div&gt;
&lt;/div&gt;

 &lt;!-- Text input--&gt;
&lt;div class="form-group"&gt;
  &lt;label class="col-md-4 control-label"&gt;Direct Phone Line&lt;/label&gt;  
  &lt;div class="col-md-4"&gt;
 &lt;input class="form-control input-md" value=" " type="text" name="c" /&gt; 
 &lt;span class="help-block"&gt;External&lt;/span&gt;  
  &lt;/div&gt;
&lt;/div&gt;

 &lt;!-- Text input--&gt;
&lt;div class="form-group"&gt;
  &lt;label class="col-md-4 control-label"&gt;Cellular Phone&lt;/label&gt;  
  &lt;div class="col-md-4"&gt;
 &lt;input class="form-control input-md" value=" " type="text" name="d" /&gt;
 &lt;span class="help-block"&gt;External&lt;/span&gt;  
  &lt;/div&gt;
&lt;/div&gt;

 &lt;!-- Button (Double) --&gt;
&lt;div class="form-group"&gt;
  &lt;label class="col-md-4 control-label" for="button1id"&gt;&lt;/label&gt;
  &lt;div class="col-md-8"&gt;
    &lt;input class="btn btn-primary" input type="submit" name="ok" value="Submit Entry" &gt;


  &lt;/div&gt;
&lt;/div&gt;

&lt;/fieldset&gt;
 &lt;/form&gt;
</code></pre>

<p>When I placed a second form below and attempted to remove the node with a foreach I go no results added or removed. The form remains in tact as it was:</p>

<pre><code>                &lt;p&gt;Please fill in the below form to REMOVE a contact to the phone system.&lt;/p&gt;

&lt;?php 
if (isset($_REQUEST['ok2'])) { 
$xml = new DOMDocument("1.0","UTF-8"); 
$xml-&gt;load("emergency.xml"); 
$aTag = $_POST['a'];
$xpath = new DOMXPATH($xml);
foreach($xpath-&gt;query("/YealinkIPPhone/DirectoryEntry[a = '$aTag']") as $node)
{
    $node-&gt;parentNode-&gt;removeChild($node);
}
$xml-&gt;formatoutput = true;
$xml-&gt;save("emergency.xml");
 } 
 ?&gt; 
 &lt;form class="form-horizontal" action="emergencycontacts.php" method="post"&gt;
  &lt;form class="form-horizontal" action="emergencycontacts.php" method="post"&gt;
&lt;fieldset&gt;

&lt;!-- Form Name --&gt;
&lt;legend&gt; &lt;/legend&gt;

&lt;!-- Text input--&gt;
&lt;div class="form-group"&gt;
  &lt;label class="col-md-4 control-label"&gt;Full Name&lt;/label&gt;  
  &lt;div class="col-md-5"&gt;
 &lt;input class="form-control input-md" required="" type="text" name="a" /&gt; 
 &lt;span class="help-block"&gt;Required&lt;/span&gt;  
  &lt;/div&gt;
&lt;/div&gt;

 &lt;!-- Button (Double) --&gt;
&lt;div class="form-group"&gt;
  &lt;label class="col-md-4 control-label" for="button1id"&gt;&lt;/label&gt;
  &lt;div class="col-md-8"&gt;
    &lt;input class="btn btn-primary" input type="submit" name="ok2" value="Delete Entry" &gt;
</code></pre>

<p>I searched all over and didnt find much on removal of data based on uer entry. Is there a better way to remove elements? I dont see what Im missing.</p>