adding data to xml using php - php

have a form that a name has been entered and need to add it to xml file.
index.html
<form name="form" action="insert.php" method="post">
<label for="name">Name:</label> <br />
<input type="text" name="name" id="name" /> <br />
<button type="submit" id="button">Submit</button>
<br />
<span id="validate"></span>
</form>
insert.php
header('Location:index.php');
$xmldoc = new DOMDocument();
$xmldoc->load('recentUploads.xml');
$Name = $_POST['name'];
$root = $xmldoc->firstChild;
$fileName = $xmldoc->createElement('name');
$root->appendChild($fileName);
$newText1 = $xmldoc->createTextNode($Name);
$fileName->appendChild($newText1);
$xmldoc->save('recentUploads.xml');
but i can not add anything to the xml file?
Help!

You are placing a lot of burden on poor variable $fileName:
$fileName = $_POST['name'];
$fileName = $xmldoc-> createElement('name');
On the other hand, $Name is not defined when you use it in line
$newText1 = $xmldoc->createTextNode($Name);
Me thinks these two incidents are related and one $fileName should actually be a $Name.

http://www.php.net/manual/en/domdocument.save.php
Are you remembering to save() it back?
I don't see that in your example code...

Do you need to call $xmldoc->saveXML();?

Related

This is the code to replace .txt files with each other so i would like to do this but i dont know where i am going wrong any tips?

this is the new code I am trying to replace the .txt file with the new car makes and model right now I am getting errors on those lines also I am entering the model and make but only adds to the txt files not replaces.
Notice: Undefined index: car_make in C:\Users\Ali\Desktop\USBWebserver v8.5\8.5\root\txtFiles\carcode.php on line 10
the contents of the.txt file are this '5bmw i5audii8' in one txt file but in the other, no contents are there but I only want one single.txt file with two forms
<?PHP
session_start();
?>
<body>
Car Make & Model
<form name="submit" method="POST" action="carcode.php">
Car Make <input type="text" name="car_make"> <br />
Car Model <input type="text" name="car_model"> <br/>
<input type="submit" name="submit" value="submit">
</form>
</body>
<?PHP
$car = $_POST['car_make'];
$car2 = $_POST['car_model'];
$Data = "Car make & Model.txt";
$File = fopen($Data, 'a+');
$name = $car;
$name2 = $car2;
fwrite($File, $name );
fwrite($File, $name2);
fclose($File);
?>
<html>
<body>
Car Replacements
<form name="submit" method="POST" action="carcode.php">
Car Make <input type="text" name="car_make"> <br />
Car Model <input type="text" name="car_model"> <br/>
<input type="submit" name="submit" value="submit">
</form>
</body>
<?PHP
if (!empty($_POST['car_make'])&& !empty($_POST['car_model']))
{
$new_car_make=$_POST['car_make'];
$new_car_model=$_POST['car_model'];
$Data="New Car make & Model.txt";
$haystack=file($Data);
for($i=0; $i<count($haystack);$i++)
{
$haystack[$i]=str_ireplace($new_car_make,$new_car_model,$haystack[$i]);
}
file_put_contents($Data, $haystack);
}
?>
To add data, yes your codes fopen with 'a+' parameter is correct.
To replace existing data, we may use fopen with 'w+' (which clear the existing data and do the data insertion)
Since you are trying to work in one single form, you need to tell the system which action (add / replace) you want to execute. The simplest way is to use a hidden field (e.g. xaction) to let the php knows what part you want to do.
On the other hand, I also suspect that you want the "replacement" to work by replacing a particular make and model with a new one (instead of clearing the whole txt file, if that is the case I suggest you first read the contents of the txt file and then do a string replacement and after that save the data)
Try the codes below:
<?php
// session_start();
?>
<body>
Car Make & Model (Add more data)
<form name="submit" method="POST" action="carcode.php">
Car Make <input type="text" name="car_make"> <br />
Car Model <input type="text" name="car_model"> <br/>
<input type=hidden name=xaction value="Add">
<input type="submit" name="submit" value="submit">
</form>
</body>
<?php
if ($_REQUEST["xaction"]=="Add") {
$car = $_POST['car_make'];
$car2 = $_POST['car_model'];
$Data = "Car make & Model.txt";
$File = fopen($Data, 'a+');
$name = $car;
$name2 = $car2;
fwrite($File, " " );
fwrite($File, $name );
fwrite($File, " " );
fwrite($File, $name2);
fclose($File);}
?>
<html>
<body>
Car Replacements (replace whole file data with the following new data)
<form name="submit" method="POST" action="carcode.php">
Car Make <input type="text" name="car_make"> <br />
Car Model <input type="text" name="car_model"> <br/>
<input type=hidden name=xaction value="Replace">
<input type="submit" name="submit" value="submit">
</form>
</body>
<?php
if ($_REQUEST["xaction"]=="Replace") {
$car = $_POST['car_make'];
$car2 = $_POST['car_model'];
$Data = "Car make & Model.txt";
$File = fopen($Data, 'w+');
$name = $car;
$name2 = $car2;
fwrite($File, " " );
fwrite($File, $name );
fwrite($File, " " );
fwrite($File, $name2);
fclose($File);
}
?>
For your information, I have commented the line "session_start()" because in your case you do not need to work on any session variables, but if other part of your code require to work on session variables, please un-comment this line)
Actually the manipulation of insertion / update / deletion of data should be performed by using a database. If you have time , please try to use database instead of using a txt file to do the job.

Candidate note data unable to be posted to PHP section of page?

My php page has the HTML and PHP all on the same page.
For the HTML section, user data is inputted and posted to the PHP section.
</div>
</header>
<pre>
</pre>
<h1 style="font-family: 'Lucida Sans Typewriter'"> Note entry:</h1>
<form method="post">
<div>
<div style="margin-right:5px;">
<input type="text" class="fileName" name="fileName" id="fileName" size="35"
value="untitled">
<input type="text" name="noteData" id="notes" size="250">
<input type="Submit" name="Submit" value="Save">
</div>
</div>
</form>
As for the PHP section, I check to see if a POST has occured, then save file and filename to disk, yet this does not occur.
if($_POST){
$noteName = $_POST['fileName'];
$noteData = $_POST['notes'];
$notes = fopen('' + $noteName,"wb");
fwrite($notes,$noteData);
fclose($notes);
}
The errors thrown are here:
https://gyazo.com/99646e2705ed7ea927d27c67cddb87b4
Any suggestions? I need this finished soon and cannot for the life of me discover why this is deciding not to work.
You have to use names to get data using $_POST. And use . to concatenate words in php. So change the line as:
if($_POST){
$noteName = $_POST['fileName'];
$noteData = $_POST['noteData'];
$notes = fopen('' . $noteName,"wb");
fwrite($notes,$noteData);
fclose($notes);
}
You should use names, not ID. Try $_POST[‘noteData’].

Issue with php and xml in Linux Instance

I currently have a Linux Instance running CentOS and I am trying to create a php form that takes input and updates an xml file with what is submitted.
It works on my local network exactly how I want it to but when I run it from the server it does not update the xml file.
The server is running php 5.3 and it php works.
Is there something different I need to do?
The php file is below:
<?php
if (isset($_POST['submit']))
{
$ID = $_POST['ID'];
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$xml = simplexml_load_file("test.xml");
$sxe = new SimpleXMLElement($xml->asXML());
$entry = $sxe->addChild("item");
$entry->addChild("name", $name);
$entry->addChild("email", $email);
$entry->addChild("phone", $phone);
$sxe->asXML("test.xml");
}
?>
<!doctype html>
<head>
</head>
<body>
<form method="POST" action=''>
ID# <input type="text" value="" name="ID"/>
Name <input type="text" value="" name="name"/>
Email <input type="text" value="" name="email"/>
Phone <input type="text" value="" name="phone"/>
<input name="submit" type="submit" />
</form>
</body>
Make sure you have write permissions to that file. Set chmod value to 666 if you want to write to that file.

Can't save textarea to file using php?

I have issue saving textarea to file. I used POST method to send the form to the other page then, in the next page I can't include the textarea content with the file Im not sure what is the problem.
Is there any idea about what is the problem?
Here are the two pages:
page1:
<!DOCTYPE HTML>
<html>
<head>
<title>Save</title>
</head>
<body>
<form action="page2.php" method="post">
<span>name:</span>
<input type="text" name="name"><br>
<span>file extension: </span>
<select name="ext" id="ext">
<option value=".txt">.txt</option>
<option value=".doc">.doc</option>
</select>
<textarea name="txt1" id="txt1" cols="15" rows="10"></textarea>
<br>
<input type="submit" name="submit" id="submit" value="Save">
</form>
<br>
</body>
</html>
-page2.php
$txt1 = $_POST['txt1']; //textarea
$name = $_POST['name'];
$ext = $_POST['ext']; //choose from multiple extensions
if ($ext == '.txt') // In case if I want to add more than extension.
{
$file = "'. $name$ext.'" ;
$output = "$txt1";
file_put_contents($file, $output);
$text = file_get_contents($file);
header("Content-Description: File Transfer");
header("Content-Type: application/text/plain");
header("Content-Disposition: attachment; filename=".basename($file));
ob_clean();
flush();
readfile($file);
exit;
}
Without seeing your html I can't be sure of what the problem is. But its been my experience that when your having trouble accessing POST vars on the server side that it's probably a simple spelling error. Make sure the name attributes in your form line up with your POST vars. Just my two cents.
I have no idea what did you mean with your code, so, I'd just rewrite it
To save a file on the server you need these 2 lines
$name = basename($_POST['name']).'.txt';
file_put_contents($name, $_POST['txt1']);
$file = "'. $name$ext.'";
should be:
$file = $name.$ext;
At least that's what I had to change to get it to work on my server.
You need to add an id to the form, then add the form Id to the textarea element. For example:
<form action="page2.php" method="post" id="myform">
<textarea name="txt1" id="txt1" cols="15" rows="10" form="myform"></textarea>
Try using the wrap element in your textarea
<textarea name="txt1" id="txt1" cols="15" rows="10"></textarea>
add wrap
<textarea name="txt1" id="txt1" cols="15" rows="10" wrap="virtual"></textarea>
you can also use wrap: off, hard, soft and physical
In your database make sure the field txt1 is defined properly (i.e type text).

document object confusion

In file1.php, I execute file2.php with
<form action="file2.php" method="POST">
Within file2, I want to access html elements from file1, but their document objects are different.
How can I access file1's elements from within file2?
well rather then using javascript you can access what you need via PHP's $_POST function so if you had the elements name and password you could access them with:
$_POST['name'];
$_POST['password'];
so something like this for file2.php:
<?php
if(array_key_exists('submit', $_POST))
{
$username = $_POST['name'];
$password = $_POST['password'];
echo("Hello $username, your password is $password");
}
?>
That goes on the assumption of file1.php looking like:
<form action="file2.php" method="post">
Username:
<input type="text" name="name" />
<br />
Password:
<input type="password" name="password" />
<br />
<input type="submit" name="submit" />
</form>
Why don't you try hiding in file2.php the elements that you still need?.
Just render them again with php in file2.php, and hide them via CSS, then they will be easily accesible through javascript.

Categories