I have a problem to replace an XML node while entering data into a form My code below :
$xml = new DOMDocument("1.0", "ISO-8859-1");
$xml->preserveWhiteSpace = FALSE;
$xml->formatOutput = TRUE;
$xml->load($fichier);
$xpath = new DOMXPATH($xml);
$query = $xpath->query("/$racine/annonce[@id = '$annonce_no']/flagplus");
foreach($query as $result){ $flagplus = $result->textContent;}
$query = $xpath->query("/$racine/annonce[@id = '$annonce_no']/entete");
foreach($query as $result){ $entete = utf8_encode($result->textContent);}
$query = $xpath->query("/$racine/annonce[@id = '$annonce_no']/nouveau");
foreach($query as $result){ $nouveau = $result->textContent;}
$query = $xpath->query("/$racine/annonce[@id = '$annonce_no']/description");
foreach($query as $result){ $description = utf8_encode($result->textContent);}
$query = $xpath->query("/$racine/annonce[@id = '$annonce_no']/couleur");
foreach($query as $result){ $couleur = $result->textContent;}
$query = $xpath->query("/$racine/annonce[@id = '$annonce_no']/detail");
foreach($query as $result){ $detail = utf8_encode($result->textContent);}
$detail = str_replace('<br/>', PHP_EOL, $detail);
$query = $xpath->query("/$racine/annonce[@id = '$annonce_no']/autre");
foreach($query as $result){ $autre = utf8_encode($result->textContent);}
$query = $xpath->query("/$racine/annonce[@id = '$annonce_no']/contact");
foreach($query as $result){ $contact = utf8_encode($result->textContent);}
$query = $xpath->query("/$racine/annonce[@id = '$annonce_no']/vente");
foreach($query as $result){ $vente = utf8_encode($result->textContent);}
$node = $xpath->query("/$racine/annonce[@id = '$annonce_no']");
if (isset($_POST['submitSave'])){
$id_new = $_POST['id'];
$flagplus_new = $_POST['flagplus'];
$entete_new = htmlentities($_POST['entete'], ENT_XML1);
$nouveau_new = $_POST['nouveau'];
$description_new = htmlentities($_POST['description'], ENT_XML1);
$couleur_new = $_POST['couleur'];
$detail_new = htmlentities($_POST['detail'], ENT_XML1);
$autre_new = htmlentities($_POST['autre'], ENT_XML1);
$contact_new = htmlentities($_POST['contact'], ENT_XML1);
$vente_new = htmlentities($_POST['vente'], ENT_XML1);
$node->replaceChild($id_new, $id);
$node->replaceChild($flagplus_new, $flagplus);
$node->replaceChild($entete_new, $entete);
$node->replaceChild($description_new, $description);
$node->replaceChild($couleur_new, $couleur);
$node->replaceChild($detail_new, $detail);
$node->replaceChild($autre_new, $autre);
$node->replaceChild($contact_new, $contact);
$node->replaceChild($vente_new, $vente);
$xml->save($fichier);
I want to change the new values with the old values. The DOM documentations say : public DOMNode DOMNode::replaceChild ( DOMNode $newnode , DOMNode $oldnode )
and the form
<form method="post" class="thumbnail">
<div>
<p>ID</br>
<input type="text" name="id" style="width:30px;" maxlength="3" value="<?php echo $annonce_no ?>" ></p>
</div>
<div>
<p>2ème drapeau</br>
<input type="text" name="flagplus" style="width:20px;" maxlength="2" value="<?php echo $flagplus ?>" > blanc,FR,GE,VD,VS</p>
</div>
<div>
<p>Nouveau</br>
<input type="text" name="nouveau" style="width:10px;" maxlength="1" value="<?php echo $nouveau ?>" > 0=Non, 1=Oui</p>
</div>
<div>
<p>Couleur description</br>
<input type="text" name="couleur" style="width:10px;" maxlength="1" value="<?php echo $couleur ?>" > blanc ou R = rouge</p>
</div>
<div>
<p>Entete</br>
<input type="text" name="entete" style="width:20%" value="<?php echo $entete ?>" ></p>
</div>
<div>
<p>Description</br>
<input type="text" name="description" style="width:80%" value="<?php echo $description ?>" ></p>
</div>
<div>
<p>Détail annonce</br>
<textarea name="detail" style="width:80%" cols="200" rows="8"><?=$detail;?></textarea></p>
</div>
<div>
<p>Autre</br>
<input type="text" name="autre" style="width:80%" value="<?php echo $autre ?>" ></p>
</div>
<div>
<p>Contact</br>
<input type="text" name="contact" style="width:80%" value="<?php echo $contact ?>" ></p>
</div>
<div>
<p>Vente</br>
<input type="text" name="vente" style="width:20%;" value="<?php echo $vente ?>" ></p>
</div>
<div align="center"><input type="submit" name="submitSave" value="Enregistrer" class="btn btn-primary" style="width:150px"></div>
</form>
I have the message : Fatal Error: Call to undefined function replaceChild I've looked to How to update xml file using PHP? it seems that works for him.I did the same think...
Any idea? thanks!