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!
$node = $xpath->query("/$racine/annonce[#id = '$annonce_no']"); does not give you a DOM node, it is a DOM node list. So you at least need $node = $xpath->query("/$racine/annonce[#id = '$annonce_no']")->item(0); to have an object that exposes a replaceChild method. However, the other variables assigned with e.g. $flagplus_new = $_POST['flagplus']; are not nodes at all you could pass to that method, nor are the variables $flagplus = $result->textContent; which are all string values. If you want to replace an element's content with a new value then selecting that element and setting the textContent seems like the right approach.
So assuming e.g.
$node = $xpath->query("/$racine/annonce[#id = '$annonce_no']")->item(0);
gives you the right element then you can select the description child with e.g.
$descEl = $xpath->query('description', $node)->item(0);
and changes its contents with e.g.
$descEl->textContent = $newDescription;
The only drawback is that the documentation http://php.net/manual/en/class.domnode.php says "5.6.1 The textContent property has been made writable (formerly it has been readonly). ".
Related
I am trying to populate afew fields using a load button when I select from the dropdown list. However, the load button is not working and the php codes look fine to me. So I am wondering which part went wrong.
I am wondering if I should put the load button at AuthorityCode or below the form. However, I have tried both methods and both doesn't work.
<strong>Authority Code: </strong>
<select name=authorityid value=authorityid>Select Authority</option>
<option value = "">Select</option><br/>
<?php
$connection = new mysqli("localhost", "username", "password", "dbname");
$stmt = $connection->prepare("SELECT AuthorityId FROM AuthorityList");
$stmt->execute();
$stmt->bind_result($authorityid);
while($stmt->fetch()){
echo "<option value = '$authorityid'>$authorityid</option>";
}
$stmt->close();
$connection->close();
?>
</select>
<?php
if(isset($_POST["loadbtn"])){
$authorityid = $_POST["authorityid"];
$conn = new mysqli("localhost", "username", "password", "dbname");
$stmt = $conn->prepare("SELECT AuthorityName, Address, TelephoneNo, FaxNo FROM AuthorityList WHERE AuthorityId = '$authorityid'");
$stmt->execute();
$result = mysqli_query($stmt, $conn);
$details = mysqli_fetch_array($result);
$savedName = $details["AuthorityName"];
$savedAddress = $details["Address"];
$savedTel = $details["TelephoneNo"];
$savedFax = $details["FaxNo"];
}
// while ($row = $result->fetch_array(MYSQLI_NUM)){
// $authorityname = $row[0];
// $address = $row[1];
// $telephone = $row[2];
// $fax = $row[3];
// }
?>
<form action="" method="post" >
<input type="submit" value="Load" name="loadbtn"><br/><br/>
<strong>Authority Name: </strong> <input type="text" name="authorityname" value="<?php echo $savedName; ?>"/><br/>
<strong>Address: </strong> <input type="text" name="address" value="<?php echo $savedAddress; ?>"/><br/>
<strong>Telephone No: </strong> <input type="text" name="telephone" value="<?php echo $savedTel; ?>"/><br/>
<strong>Fax No: </strong> <input type="text" name="fax" value="<?php echo $savedFax; ?>"/><br/>
Change form to:
<form action="window.location.reload()" method="post" >
Your select values are not getting submitted as they are not part of the form. Also your 2nd query was not running properly. I've cheanged it below. Please take a look
Try this-
<strong>Authority Code: </strong>
<form action="" method="post" >
<select name="authorityid">Select Authority
<option value = "">Select</option><br/>
<?php
$connection = new mysqli("localhost", "username", "password", "dbname");
$stmt = $connection->prepare("SELECT AuthorityId FROM AuthorityList");
$stmt->execute();
$stmt->bind_result($authorityid);
while($stmt->fetch()){
echo "<option value = '$authorityid'>$authorityid</option>";
}
$stmt->close();
$connection->close();
?>
</select>
<?php
if(isset($_POST["loadbtn"])){
$authorityid = $_POST["authorityid"];
$conn = new mysqli("localhost", "username", "password", "dbname");
$qry = "SELECT AuthorityName, Address, TelephoneNo, FaxNo FROM AuthorityList WHERE AuthorityId = '$authorityid'";
$result = $conn->query($qry);
$details = mysqli_fetch_array($result);
$savedName = $details["AuthorityName"];
$savedAddress = $details["Address"];
$savedTel = $details["TelephoneNo"];
$savedFax = $details["FaxNo"];
}
?>
<input type="submit" value="Load" name="loadbtn"><br/><br/>
<strong>Authority Name: </strong>
<input type="text" name="authorityname" value="<?php echo isset($savedName) ? $savedName : ''; ?>"/>
<br/>
<strong>Address: </strong>
<input type="text" name="address" value="<?php echo isset($savedAddress) ? $savedAddress : ''; ?>"/>
<br/>
<strong>Telephone No: </strong>
<input type="text" name="telephone" value="<?php echo isset($savedTel) ? $savedTel : ''; ?>"/>
<br/>
<strong>Fax No: </strong>
<input type="text" name="fax" value="<?php echo isset($savedFax) ? $savedFax : ''; ?>"/>
<br/>
</form>
Your error seems to be from mysql part of the code. Below you'll find a sample code that has mysql part removed from it and is running-
<!DOCTYPE html>
<html>
<head>
<title>Fix</title>
</head>
<body>
<strong>Authority Code: </strong>
<form action="" method="post" >
<select name="authorityid">Select Authority
<option value = "">Select</option><br/>
<?php
echo "<option value = '123'>123</option>";
echo "<option value = '234'>234</option>";
echo "<option value = '345'>345</option>";
echo "<option value = '456'>456</option>";
?>
</select>
<?php
if(isset($_POST["loadbtn"])){
$authorityid = $_POST["authorityid"];
if ($authorityid == '123') {
$savedName = 'nameTest';
$savedAddress = 'addressTest';
$savedTel = 'telTest';
$savedFax = 'faxTest';
}
}
?>
<input type="submit" value="Load" name="loadbtn"><br/><br/>
<strong>Authority Name: </strong>
<input type="text" name="authorityname" value="<?php echo isset($savedName) ? $savedName : ''; ?>"/>
<br/>
<strong>Address: </strong>
<input type="text" name="address" value="<?php echo isset($savedAddress) ? $savedAddress : ''; ?>"/>
<br/>
<strong>Telephone No: </strong>
<input type="text" name="telephone" value="<?php echo isset($savedTel) ? $savedTel : ''; ?>"/>
<br/>
<strong>Fax No: </strong>
<input type="text" name="fax" value="<?php echo isset($savedFax) ? $savedFax : ''; ?>"/>
<br/>
</form>
</body>
</html>
Edited: changed the mysqli_query function so that it works properly.
I'm trying to sort one array by another array. Both these arrays get their content from a form.
Here's my form code:
<form method="post" action="">
<div class="groupcontainer">
<br/><label>Group One:</label><br/>
<input type="text" name="groupname[]" value="groupone" /><br/>
<br/><label>Variable Group One:</label><br/>
<input type="text" name="variable[]" value="variableone" />
<input type="text" name="variable[]" value="variabletwo" />
</div>
<br/>
<div class="groupcontainer">
<br/><label>Group Two:</label><br/>
<input type="text" name="groupname[]" value="grouptwo" /><br/>
<br/><label>Variable Group Two:</label><br/>
<input type="text" name="variable[]" value="variablethree" />
<input type="text" name="variable[]" value="variablefour" />
</div>
<br/>
<input type="submit" name="submit" value="Submit" />
</form>
Here's the PHP code:
<?php
if (!$_POST['submit'] == "") {
foreach($_POST['groupname'] as $groupname) {
$groupnum = 1;
foreach($_POST['variable'] as $variable) {
print "$".$groupname.$groupnum." = '".$variable."';<br/>";
$groupnum++;
}
print "$".$groupname." = array(";
for ($arrnum = 1; $arrnum <= count($_POST['variable']); $arrnum++) {
print "$".$groupname.$arrnum.", ";
}
print ");<br/><br/>";
}
}
?>
This is the result I get when I submit the form:
$groupone1 = '$variableone';
$groupone2 = '$variabletwo';
$groupone3 = '$variablethree';
$groupone4 = '$variablefour';
$groupone = array($groupone1, $groupone2, $groupone3, $groupone4, )
$grouptwo1 = '$variableone';
$grouptwo2 = '$variabletwo';
$grouptwo3 = '$variablethree';
$grouptwo4 = '$variablefour';
$grouptwo = array($grouptwo1, $grouptwo2, $grouptwo3, $grouptwo4, )
This is the result that I actually want:
$groupone1 = '$variableone';
$groupone2 = '$variabletwo';
$groupone = array($groupone1, $groupone2)
$grouptwo1 = '$variablethree';
$grouptwo2 = '$variablefour';
$grouptwo = array($grouptwo1, $grouptwo2)
The whole thing needs to be dynamic since I want to add as many groups and variables as I want.
I've been searching for an answer for days and already asked two people who didn't know an answer. Maybe you guys can help. Thanks!
Update:
Just to clarify a few points:
So basically I want to be able to add as many input forms as I want (I use jQuery for that) to create as many groups and variables as I want, for example like this:
$groupwuteva1 = 'hello';
$groupwuteva2 = 'bye':
$randomname1 = 'green';
$randomname2 = 'blue';
$randomname3 = 'red';
$blabla1 = 'abc';
$blabla2 = 'xyz';
$blabla3 = '123';
$blabla4 = 'bla';
Whatever I use as groupname will be used in array one, e.g. I call a group "Colors" and the variables I put into the form for that group are "blue", "red" and "green". Then I would get this code:
$colors1 = 'green';
$colors2 = 'blue';
$colors3 = 'red';
I hope this clairfies some questions. And thanks a ton for all responses so far!
You can take the group name as a container to store all associated variable values in it. And later, use variable variables and implode() function to process your html form.
HTML
<form method="post" action="">
<div class="groupcontainer">
<br/><label>Groupe One:</label><br/>
<input type="text" name="groupname[]" value="groupone" /><br/>
<br/><label>Variable Group One:</label><br/>
<input type="text" name="groupone[]" value="variableone" />
<input type="text" name="groupone[]" value="variabletwo" />
</div>
<br/>
<div class="groupcontainer">
<br/><label>Groupe Two:</label><br/>
<input type="text" name="groupname[]" value="grouptwo" /><br/>
<br/><label>Variable Group One:</label><br/>
<input type="text" name="grouptwo[]" value="variablethree" />
<input type="text" name="grouptwo[]" value="variablefour" />
</div>
<br/>
<input type="submit" name="submit" value="Submit" />
</form>
PHP
if(isset($_POST['submit'])){
foreach($_POST['groupname'] as $value){
$arr = array();
$i = 1;
foreach($_POST[$value] as $v){
$var = $value . $i;
$$var = $v;
echo $var . " = " . $$var . "<br />";
$arr[] = $$var;
++$i;
}
$output = $value . " = array(" . implode(",", $arr) . ")";
echo $output . "<br /><br />";
}
}
Output:
groupone1 = variableone
groupone2 = variabletwo
groupone = array(variableone,variabletwo)
grouptwo1 = variablethree
grouptwo2 = variablefour
grouptwo = array(variablethree,variablefour)
try this form:
<form method="post" action="">
<?php foreach(array('groupone', 'grouptwo') as $num):?>
<div class="groupcontainer">
<br/><label>Groupe <?php echo $num;?>:</label><br/>
<input type="text" name="groupname[]" value="<?php echo $num;?>" /><br/>
<br/><label>Variable Group <?php echo $num;?>:</label><br/>
<input type="text" name="variable[<?php echo $num;?>][]" value="<?php echo uniqid();?>" />
<input type="text" name="variable[<?php echo $num;?>][]" value="<?php echo uniqid();?>" />
</div>
<br/>
<?php endforeach;?>
<input type="submit" name="submit" value="Submit" />
</form>
and code:
if ($_POST) {
foreach($_POST['groupname'] as $groupname) {
$$groupname = array();
foreach($_POST['variable'][$groupname] as $variable) {
${$groupname}[] = $variable;
}
}
var_dump($groupone);
var_dump($grouptwo);
}
I want to send the "fill[]" and the "item[]" and my problem is the function write cant get the values of the variables, why?
What is wrong? Im new in this stuff.
<form action="/fill_sites/fill.php" method="get">
<input type="text" size="24" maxlength="20" name="fill[0]"/>
<input type="hidden" name="item[0]value="hella_apfelschorle"/>
<input type="text" size="24" maxlength="20" name="fill[1]"/>
<input type="hidden" name="item[1]" value="volvic_wasser"/>
..... so on
<input type="submit" value="Abschicken"/>
------------------------------------------------------------>[fill/php]>
$fill[0] = $_GET["fill[0]"];
$fill[1] = $_GET["fill[1]"];
$fill[2] = $_GET["fill[2]"];
$fill[3] = $_GET["fill[3]"];
$fill[4] = $_GET["fill[4]"];
$fill[5] = $_GET["fill[5]"];
$fill[6] = $_GET["fill[6]"];
$fill[7] = $_GET["fill[7]"];
$fill[8] = $_GET["fill[8]"];
$fill[9] = $_GET["fill[9]"];
$item[0] = $_GET["item[0]"];
$item[1] = $_GET["item[1]"];
$item[2] = $_GET["item[2]"];
$item[3] = $_GET["item[3]"];
$item[4] = $_GET["item[4]"];
$item[5] = $_GET["item[5]"];
$item[6] = $_GET["item[6]"];
$item[7] = $_GET["item[7]"];
$item[8] = $_GET["item[8]"];
$item[9] = $_GET["item[9]"];
write($counterFilename[$item[]], $fill);
you can do this by following code. it will get complete array.
$item = $_Get['item'];
$fill= $_Get['fill'];
use php's foreach statement to retrieve the content of fill and item
$fills = $_GET['fill'];
foreach($fills as $fill)
{
//do something with $fill
}
and the same for $item
i want to show user specific data in html form(in text fields or in select list)
I have a function ShowUserInformation() in class MyClass:
function ShowUserInformation()
{
$query = "SELECT id, name, email FROM saloni WHERE id = '$_SESSION[ID_korisnika]'";
$result = mysql_query($query);
while($row=mysql_fetch_array($result)):
$id= $row['id'];
$name= $row['name'];
$email = $row['email'];
$address= $row['address'];
$address2= $row['address2'];
$address3= $row['address3'];
endwhile;
return $result;
}
My question is: How can i display value of $name, or $email, $id... on another page in text box or in select list?
If i do it in procedural way it works when i do this:
<input type="text" value="<?php echo $name ?>" name="name" class="" />
But, how can i display the $name,$email,$ID... in oop way? Is there a way to call it directly and not declare it as class variable and then call it.
i've included file, created object...
$my_class = new MyClass; //create an object of class
HTML - i've tried something like this...
<input type="text" value="<?php echo $my_class->ShowUserInformation($name)?>" name="name" class="" />
I'm new in PHP and oop so be easy with me :)
Thank you
If you only plan on one row being returned, then why not use mysql_fetch_assoc()
class MyClass{
public function GetUserInformation(){
$query = "SELECT id, name, email FROM saloni WHERE id = '$_SESSION[ID_korisnika]'";
$result = mysql_query($query);
$info = mysql_fetch_assoc($result);
return $info;
}
}
$class = new MyClass;
$info = $class->GetUserInformation();?>
<input type="text" value="<?php echo $info['id']?>" name="id" class="" />
<input type="text" value="<?php echo $info['name']?>" name="name" class="" />
Note: mysql_* functions are deprecated, and you should move to use MySQLi or PDO
First, change the function to get the data:
function ShowUserInformation()
{
// Assuming you need only one user, I have set "LIMIT" to "1"
$query = "SELECT id, name, email, address, address2, address3 FROM saloni WHERE id = '$_SESSION[ID_korisnika]' LIMIT 1";
$result = mysql_query($query);
return mysql_fetch_array($result);
}
Now, get the information:
$my_class = new MyClass;
$userData = $my_class->ShowUserInformation();
// HTML
<input type="text" value="<?php echo $userData['name']; ?>" name="name" class="" />
kindly try this:
<?php
$show_info=ShowUserInformation();
$data = $show_info->fetchAll(PDO::FETCH_ASSOC);
foreach($data as $row){ ?>
<input type="text" value="<?php $row['name']; ?>" name="name" class="" />
<?php } ?>
OR
<?php $show_info=ShowUserInformation();
while ($row= mysql_fetch_assoc($show_info){ ?>
<input type="text" value="<?php $row['name']; ?>" name="name" class="" />
<?php } ?>
<form action="form.php" id="form">
<input type="text" name="name_1"><input type="text" name="city_1"><input type="text" name="country_1">
<input type="text" name="name_2"><input type="text" name="city_2"><input type="text" name="country_2">
<input type="text" name="name_3"><input type="text" name="city_3"><input type="text" name="country_3">
<input type="submit">
</form>
How is the best method for get this data in form.php file? This is generated with jQuery. Can be 3 (as now) or 30.
In form.php i have:
$data = new Data();
$data->name = $_POST['name_1'];
$data->city = $_POST['city_1'];
$data->country = $_POST['country_1'];
$data->save();
$data = new Data();
$data->name = $_POST['name_2'];
$data->city = $_POST['city_2'];
$data->country = $_POST['country_2'];
$data->save();
$data = new Data();
$data->name = $_POST['name_3'];
$data->city = $_POST['city_3'];
$data->country = $_POST['country_3'];
$data->save();
But if there is over 3? I would like use foreach, but how? How can i generated input or get this data?
Use name[], city[] and country[] as your input names. Then they'll come through to PHP as arrays which you can iterate with foreach.
If you can change the names of the input elements you could do it like that:
<input type="text" name="items[1][name]"><input type="text" name="items[1][city]"><input type="text" name="items[1][country]">
<input type="text" name="items[2][name]"><input type="text" name="items[2][city]"><input type="text" name="items[2][country]">
<input type="text" name="items[3][name]"><input type="text" name="items[3][city]"><input type="text" name="items[3][country]">
The numbers don't have to be adjacent but the have to be unique. To convert the $_POST-data to your data-model would then look like that:
$items = $_POST['items'];
$dataList = array();
foreach($items as $item) {
$data = new Data();
$data->name = $item['name'];
$data->city = $item['city'];
$data->country = $item['country'];
$dataList[] = $data;
}
Of course you can add as many items as you want.
If you can't change the names of the input elements then you could use one of the other solutions. I would only modify them so that the number of elements is not hard coded like:
$dataList = array()
for($i = 1; isset($_POST["name_$i"]); ++$i) {
$data = new Data();
$data->name = $_POST["name_$i"];
// ... and so on
$dataList[] = $data;
}
Of course, with that solution the numbers must be continuous/adjacent.
You can use your for each with on time built strings like 'name_.$i', with $i as a counter
You can use a for loop:
for ($i = 1; $i <= 3; $i++) {
$data = new Data();
$data->name = $_POST['name_'.$i];
$data->city = $_POST['city_'.$i];
$data->country = $_POST['country_'.$i];
$data->save();
}
But if you could change your html like:
<input type="text" name="name[]"><input type="text" name="city[]"><input type="text" name="country[]">
<input type="text" name="name[]"><input type="text" name="city[]"><input type="text" name="country[]">
<input type="text" name="name[]"><input type="text" name="city[]"><input type="text" name="country[]">
You will not need to know the count.(name_1,name_2 would be better to be the id but not name)
foreach ($_POST['name'] as $index => $name) {
$data = new Data();
$data->name = $name;
$data->city = $_POST['city'][$index];
$data->country = $_POST['country'][$index];
$data->save();
}