Edit XML via HTML Form (PHP) - php

I'm trying to create a form that will let you edit the contents of an xml tag. i currently have a form.php:
<?php
$data=simplexml_load_file('welcome.xml');
$welcome=$data->item->name;
?>
<form method="post">
<textarea name="name"><?php echo $welcome ?></textarea>
<br>
<input type="submit" name="submit" value="submit">
</form>
<?php
if(isset($_POST['submit'])) {
$data=simplexml_load_file('welcome.xml');
$data->item->name=$_POST['name'];
$handle=fopen("welcome.xml","wb");
fwrite($handle,$xml->asXML());
fclose($handle);
}
?>
and welcome.xml:
<welcome>
<item>
<name>$welcome</name>
</item>
</welcome>
when i press submit it doesn't save what's entered, it just refreshes the page and deletes whatever the value in the xml file was before..
UPDATE
The form works now, but I've added a reset button, i need it to clear the xml file so it only has the <welcome> tags. I've changed $data->item->name=$_POST['welcome']; to $data=''; but it deletes the text and keeps the tags still.

You can do it with simplexml.
To read data from xml:
$data = simplexml_load_file('welcome.xml');
$welcome = $data->item[0]->name;
And to write data:
$data = simplexml_load_file('welcome.xml');
$data->item[0]->name = $_POST['welcome'];
$handle = fopen("welcome.xml", "wb");
fwrite($handle, $xml->asXML());
fclose($handle);
EDIT:
For the question in the comment:
<?php
if(isset($_POST['submit'])) {
$data=simplexml_load_file('welcome.xml');
$data->item->name=$_POST['name'];
$handle=fopen("welcome.xml","wb");
fwrite($handle,$data->asXML());
fclose($handle);
}
$data=simplexml_load_file('welcome.xml');
$welcome=$data->item->name;
?>
<form method="post">
<textarea name="name"><?php echo $welcome ?></textarea>
<br>
<input type="submit" name="submit" value="submit">
</form>

Related

send a php webpage to folder and dispay the content as html

would it be possible to have a html/php template on index.php say for example (a news webpage template and then anyone can edit the title, paragraphs only, then on submit it then sends the webpage with the data stored to a paste bin like url so who ever visits that url say http://localhost/news/jjeh3bndjks they would only be able to view to content and not edit.
I would like to use something like this
<?php
if ($_POST) {
$pasteID = uniqid();
$paste = fopen("pastes/".$pasteID.".php", "w");
$contents = $_POST['pasteContents'];
fwrite($paste, $contents);
header('Location: /pastes/'.$pasteID.'.php');
}
?>
<form action="" method="POST">
<input type="text" name="pasteContents" placeholder="write here" />
<button type="submit" tabindex="0">submit</button>
</form>
but for some reason when i add another input box or try to send anymore data it fails or just gives me the last input given is there a way to send a whole page this way?
any help would be appreciated
You can use file_get_contents with the following code:
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
parse_str(file_get_contents('php://input'));
echo param1 . '<br />' . param2;
} else {
?>
<form method="post">
<input type="text" name="param1" value="param1" />
<input type="text" name="param2" value="param2" />
<input type="submit" value="submit" />
</form>
<?php } ?>
(You can test it here)
Although, I did success to use $_POST too:
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
echo $_POST['param1'] . '<br />' . $_POST['param2'];
} else {
?>
<form method="post">
<input type="text" name="param1" value="param1" />
<input type="text" name="param2" value="param2" />
<input type="submit" value="submit" />
</form>
<?php } ?>
Here

How can I get the output for form without the page changing

I am trying to get information from textarea to be converted to a .py file.
Here's my file https://jsfiddle.net/girlwhocancode/68mb49gq/
<form action=action.php method="post">
<center>
<textarea placeholder="Code you want to execute in python..."></textarea>
</center><br/>
<center><input type="submit" class="button_example"></center>
</form>
and this is my php file:
<?php
$path = #PATH;
if (isset($_POST['field1'])) {
$fh = fopen($path,"a+");
$string = $_POST['field1'];
fwrite($fh,$string); // Write information to the file
fclose($fh); // Close the file
}
?>
For some reason my php isn't working and I have no idea how to keep the same webpage after submitting
Try this:
<form action="action.php" method="post">
<center>
<textarea name="script" placeholder="Code you want to execute in python..."></textarea> //added name
</center><br/>
<center><input name="submit" type="submit" class="button_example"></center>
</form> // added name submit
in php
<?php
$path = #PATH;
if (isset($_POST['submit'])) {
$fh = fopen($path,"a+");
$string = $_POST['script'];
fwrite($fh,$string); // Write information to the file
fclose($fh); // Close the file
}
?>
Remember that in php $_POST/$_GET keys are always your form field
names

Php button action event needed

How do i implement button click php when either one of the buttons is clicked? I am trying to do is, if sumbit button is clicked the php script below shows it written to a file and if view comments is hit shows comments
<html>
<body>
<form action="http://localhost/class/assignment10.php" method="post">
User Name: <input type="text" name="uname"><br>
User Comments: <textarea type ="text" name="tcomment"></textarea><br>
<input type="submit" value="Submit Comments"/>
<input type="submit" value="View Comments"/>
</form>
</body>
</html>
I have two form buttons IF submit comments is clickedthe php code should write the user’s name and comments to a text file (use fputs function to write to a file. Also you should insert a newline after each name and each comment). When user’s name and comments are successfully recorded, php should direct
On the other hand, if a user clicks on the ‘view all comments’ button, the php code should
show all users and their corresponding comments (use fgets function)
<html>
<head>
<title>PHPLesson10</title>
</head>
<body>
<?php
// Collect user name and add a line break at the end.
$customer = $_POST['uname']."\n";
// Collect comment and add a line break.
$comments = $_POST['tcomment']."\n";
$file = fopen("C:/data/feedback.txt", "a");
If ($file) // if the file open successfully
{
// Write the variables to the file.
fputs($file, $customer);
fputs($file, $comments);
fclose($file);
}
Else
{
Echo "Please try again.";
}
?>
</body>
</html>
<?php
// Open the file feedback for reading
$file = fopen("C:/data/feedback.txt", "r");
// While the end of the file has NOT reached
While (!feof($file))
{
// Print one line of file
Echo fgets($file);
Echo "<br />";
}
// close the connection
fclose($file);
?>
Your submit buttons should like below :
<input type="submit" name="submit_comment" value="Submit Comments"/>
<input type="submit" name="view_comment" value="View Comments"/>
And your php code for each buttons should like below:
<?php
if(isset($_POST['submit_comment']))
{
//the php code will be here for save comment
}
if(isset($_POST['view_comment']))
{
//the php code will be here for view comment
}
?>
use name attribute on buttons
<input type="submit" value="Submit Comments" name="btn_submit"/>
<input type="submit" value="View Comments" name="btn_view"/>
then on php you can check something like
if (isset($_POST['btn_submit']))...
or
if (isset($_POST['btn_view']))...
Best regards,
Nebojsa
For buttons, I usually use <button> tags and not <input> tags, as long as you're using <!doctype html> for HTML5.
<form action="http://localhost/class/assignment10.php" method="post" id="commentForm">
<button type="submit" name="action" value="submit" form="commentForm">
Submit Comments
</button>
<button type="submit" name="action" value="view" form="commentForm">
View Comments
</button>
</form>
Then use PHP to figure out what the user clicked like this:
<?php
$action = $_POST["action"];
if ($action == "submit") {
// submission code
}
if ($action == "view") {
// viewing code
}
else {
die("Your request could not be completed.");
}
?>
First give your submit buttons a name like so:
<input type="submit" name="button1" value="Submit Comments"/>
<input type="submit" name="button2" value="View Comments"/>
and then use this PHP:
<?php
if (isset(#$_POST['button1'])) {
//Code to execute
}
if (isset(#$_POST['button2'])) {
//Code to execute
}
?>

How to generate pdf with defined forms in php?

I'm php newbie. I'll try to explain my needs.
I would like to fill few forms in php that would be placed at appropriate locations in the document and then generate pdf file.
I'm using mPDF atm. because I need polish signs to show up correctly.
If you could help me anyhow I would appreciate that a lot. Peace.
include('mpdf/mpdf.php');
$html = file_get_contents("generated.html");
$mpdf=new mPDF('iso-8859-2');
$mpdf->allow_charset_conversion=true;
$mpdf->charset_in='ISO-8859-2';
$mpdf->WriteHTML($html);
$mpdf->Output("my_file.pdf","F");
exit;
That's for generating pdf from html, but now I would like to somehow generate the html with forms that I could define first, and then generate pdf.
So far I'm thinking about form for generating html like:
<?php
$test1 = $_POST['test1'];
$test2 = $_POST['test2'];
$test3 = $_POST['test3'];
$data = "$test1 | $test2 | $test3\n";
$fh = fopen("generated.html", "a");
fwrite($fh, $data);
fclose($fh);
print "Finished";
?>
<body>
<form name="form1" method="post" action="generate_html.php">
test1:
<input type="text" name="test1">
<br>
test2:
<input type="text" name="test2">
<br>
test3:
<select name="test3">
<option value="test3_1">test3_1</option>
<option value="test3_2">test3_2</option>
<option value="test3_3">test3_3</option>
</select>
<br>
<input type="submit" name="Submit" value="Generate">
</form>
</body>
EDIT
Thanks to #Gavin
<?php
include('mpdf/mpdf.php');
$html = file_get_contents("2.html");
$html = str_replace("%title_placeholder%", $_POST['title'], $html);
$mpdf=new mPDF('iso-8859-2');
$mpdf->allow_charset_conversion=true;
$mpdf->charset_in='ISO-8859-2';
$mpdf->WriteHTML($html);
$mpdf->Output("3.pdf","F");
?>
<body>
<form name="form1" method="post" action="1.php">
Title: <input type="text" name="title">
<br>
<input type="submit" name="Submit" value="Generate">
</form>
</body>
Works great,thanks!
You are loading the contents of a static HTML file so you could put place holders within the html...
<h1>%title_placeholder%</h1>
and then use file get_contents
$html = file_get_contents("my_file.html");
and replace the placeholders with your form data
$html = str_replace("%title_placeholder%", $_POST['title'], $html);
then write your new string to mPDF

PHP how to clear memory when refreshing

I am facing the following issue. I have a simple textarea where user will use to submit text which is subsequently written to a text file in the server. This is working.
But when I refresh the page it adds in the last added text into the text file again causing duplicate entries.
Any idea what I must do to prevent this? Below is the code I used for the textarea portion.
<html>
<body>
<form name="form" method="post">
<input type="text" name="text_box" size="50"/>
<input type="submit" id="search-submit" value="submit" />
</form>
</body>
</html>
<?php
if(isset($_POST['text_box'])) {
$a = $_POST['text_box'];
$myFile = "textfile.txt";
$fh = fopen($myFile, 'a+') or die("can't open file");
fwrite($fh, $a."\r\n");
fclose($fh);
}
?>
Pages that are loaded via POST will cause the browser to ask the user to resubmit the information to view the page resulting in the actions performed by that page happening again. If the pages is requested via GET and has variables in the querystring the same thing happens but silently (without the user being prompted to d it again).
The best to work around this is to use the POST/REDIRECT/GET pattern. I used it in an example about processing payments that I wrote for Authorize.Net. Hopefully that points you in the right direction.
A simpler so
You can just store a simple hash on session and regenerate it every time.
When the user reloads the page the php wont be executed.
<?php
if(isset($_POST['text_box']) && $_SESSION['formFix'] == $_POST['fix']) {
$a = $_POST['text_box'];
$myFile = "textfile.txt";
$fh = fopen($myFile, 'a+') or die("can't open file");
fwrite($fh, $a."\r\n");
fclose($fh);
}
?>
<html>
<body>
<form name="form" method="post">
<input type="text" name="text_box" size="50"/>
<?php
$value = md5(rand(1,999999));
$_SESSION['formFix'] = $value;
?>
<input type="hidden" name="fix" value="<?= $value; ?>" />
<input type="submit" id="search-submit" value="submit" />
</form>
</body>
</html>
ps: the order of the blocks will matter, so you need to invert em.
As John said, you need to redirect user after form submit.
fclose($fh);
// and
header("Location: success.php or where else");
exit;
Note: Your redirection won't work unless ob_start is not called before, cos your page contains html outputs.
// form.php
<?php ob_start(); ?>
<html>
<body>
<? if (isset($_GET['success'])): ?>
Submit OK! New submit
<? else: ?>
<form name="form" method="post" action="form.php">
<input type="text" name="text_box" size="50"/>
<input type="submit" id="search-submit" value="submit" />
</form>
<? endif; ?>
</body>
</html>
<?php
if(isset($_POST['text_box'])) {
$a = $_POST['text_box'];
$myFile = "textfile.txt";
$fh = fopen($myFile, 'a+') or die("can't open file");
fwrite($fh, $a."\r\n");
fclose($fh);
// send user
header("Location: form.php?success=1");
exit;
}
?>

Categories