save post from file into msword - php

i want to save a post from an html form into a msword file
<form method="post" action="process.php">
<text name="comment"></text>
<input type="submit" name="send" value="submit"/>
</form>
process.php
<?php
if(isset($_POST['submit']))
{
$body=$_POST['comment'];
$newfile = fopen("essay/last.docx", "w") ;
$content = "$body";
fwrite($newfile, $content);
fclose($newfile);
}
?>
the created file is not a valid msword file. any help is appreciated

Related

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

Simple HTML/PHP Refuses to work

Here is poster dot php
<?php
$FILENAME = "poster.php";
$txt = "data.txt";
if (isset($_POST['field1']) && isset($_POST['field2'])) { // check if both
fields are set
$fh = fopen($txt, 'a');
$txt=$_POST['field1'].' - '.$_POST['field2'];
fwrite($fh,$txt); // Write information to the file
fclose($fh); // Close the file
}
?>
Here is my HTML form
<!DOCTYPE html>
<html>
<head>
<title>Field 1 & 2</title>
</head>
<body>
<form action="poster.php" method="post">
What is your name?<br>
<input type="text" name="field1"><br>
<input type="text" name="field2"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
I tried everything to make this work when i put it in a debugger it says the file should end on the first line. When I run the file all i get is a white screen. I'm in desperate need of help.
You are not writing anything after appending your form data in your poster.php file. You can get the file contents in the following way...
Html form:
<!DOCTYPE html>
<html>
<head>
<title>Field 1 & 2</title>
</head>
<body>
<form action="poster.php" method="post">
What is your name?<br>
<input type="text" name="field1"><br>
<input type="text" name="field2"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
and in poster.php
<?php
$FILENAME = "poster.php";
$txt = "data.txt";
if (isset($_POST['field1']) && isset($_POST['field2'])) { // check if both
//fields are set
$fh = fopen($txt, 'a');
$txt=$_POST['field1'].' - '.$_POST['field2'].PHP_EOL;
fwrite($fh,$txt); // Write information to the file
fclose($fh); // Close the file
echo file_get_contents("data.txt");
}
?>
As per requirement, write PHP code and HTML code inside a single file and remove the action attribute of the form in the following code ...
<?php
//$FILENAME = "poster.php";
$txt = "data.txt";
if (isset($_POST['field1']) && isset($_POST['field2'])) { // check if both
//fields are set
$fh = fopen($txt, 'a');
$txt=$_POST['field1'].' - '.$_POST['field2'].PHP_EOL ;
fwrite($fh,$txt); // Write information to the file
fclose($fh); // Close the file
//echo file_get_contents("data.txt");
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Field 1 & 2</title>
</head>
<body>
<form action="" method="post">
What is your name?<br>
<input type="text" name="field1"><br>
<input type="text" name="field2"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
action attribute of form should be file name where you submit your form.
<form action="action.php" method="post">
Here it is 'action.php' but you have to submit it on poster dot php.
please check it:
<form action="poster.php" method="post">
Form action will be your main file name(poster.php) and after form post, you can easily get all the form fields data on poster.php file.
you wotre this file name -> poster dot php means poster.php ?

PHP GET and POST - not working

I am a beginner with php and have the following code for getting values from a form and inserting it into a csv file as an array
PHP:
if(isset($_GET['submitted'])){
$csvData = [$_GET["contract"],$_GET["article"],$_GET["specs"]];
$fp = fopen("order.csv","a");
if($fp) {
fputcsv($fp,$csvData); // Write information to the file
fclose($fp); // Close the file
}
}
HTML :
<form action="add.php" method="GET" >
<label class="wsite-form-label" >Contract No <span class="form-required">*</span></label>
<div class="wsite-form-input-container">
<input name="contract" id='contract'>
</div>
<label class="wsite-form-label" >Article <span class="form-required">*</span></label>
<div class="wsite-form-input-container">
<input name="article">
</div>
<label class="wsite-form-label"> SPECS<span class="form-required">*</span></label>
<input name="specs">
<button type='submit' name="submitted" value="true" >Submit </button>
</form>
Have tried both get and post but still not able to get inserted value to my php. please help.
Here is the code
<?php
if(isset($_GET['submitted']))
{
$csvData = array($_GET["contract"],$_GET["article"],$_GET["specs"]);
$fp = fopen("order.csv","a");
if($fp)
fputcsv($fp,$csvData);
file fclose($fp); // Close the file
}
?>
<html>
<head>
<title>dfsdfdsdfsdfs</title>
</head>
<body>
<form action="test.php" method="GET" >
/* Your Form */
</form>
</body>
</html>
You can check like this:
if(!empty($_GET["contract"]) && !empty($_GET["article"]) && !empty($_GET["specs"])){
$csvData = array($_GET["contract"],$_GET["article"],$_GET["specs"]);
$fp = fopen("order.csv","a");
if($fp) {
fputcsv($fp,$csvData); // Write information to the file
fclose($fp); // Close the file
}
}
It seems that there is no errors in your code. You can check this by using
var_dump($csvData);
if it works then check the file pointer $fp
You can find more details about fopen() here

PHP write data from HTML to text file

I need to write some data from HTML form to data.txt file (the txt file have 777 permissions)
The HTML that I use is index.html:
<form action="index.php" method="POST">
<input name="field1" type="text" />
<input name="field2" type="text" />
<input type="submit" name="submit" value="Save Data">
And the PHP file is index.php:
<?php
$txt = "data.txt";
if (isset($_POST['field1']) && isset($_POST['field2'])) { // check if both fields are set
$fh = fopen($txt, 'a');
$txt=$_POST['field1'].' - '.$_POST['field2'];
fwrite($fh,$txt); // Write information to the file
fclose($fh); // Close the file
}
?>
But when I use the form to save the data, it show me a message box to download the index.php
what I am doing wrong?

Edit XML via HTML Form (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>

Categories