I have the following HTML:
<form method="post" id="print" action="writefile.php" onsubmit="Javascript:window.print(); return true;">
<div id="non-printable">
Enter First & Last Name:
<input id="who" name="who" type="text" /><br><br>
Enter Taken Date:
<input id="tdate" readonly name="todays_date" onfocus="showCalendarControl(this);" type="text" /><br><br>
<input id="submit" type="button" class="btn" value="Submit" />
<div id="printout" style="text-align:center;display:none;">
<input type=submit value="Print Certificate" />
</div>
</div>
</form>
<div id="parent">
<h1>THIS WILL PRINT WHEN 'PRINT CERTIFICATE' IS PRESSED</h1>
</div>
The following CSS:
#media print {
#non-printable { display: none; }
#parent { display: block; }
}
What I am looking to do is when the PRINT CERTIFICATE is pressed I want to write to a file located in my server and also display the Print window as it is currently doing right now. How can I accomplish that?
I know I can use the following PHP but the incorporation is the issue:
<?php
session_start();
// Clean up the input values
foreach($_POST as $key => $value) {
$_POST[$key] = stripslashes($_POST[$key]);
$_POST[$key] = htmlspecialchars(strip_tags($_POST[$key]));
}
$printfor = $_POST['who'];
$dte = $_POST['todays_date'];
$filename = "writefile.txt"; #Must CHMOD to 666, set folder to 777
if($printfor && $dte) {
$text = "\n" . str_pad($_SESSION['printlogin'], 15) . "" . str_pad($printfor, 30) . "" . str_pad($dte, 15);
$fp = fopen ($filename, "a"); # a = append to the file. w = write to the file (create new if doesn't exist)
if ($fp) {
fwrite ($fp, $text);
fclose ($fp);
#echo ("File written");
}
else {
#echo ("File was not written");
}
}
?>
Append to a file:
Well, if you change your html file, you can incorporate the php into it. You can trigger that php form by making the html button part of a form. When the form submits, the action can be whateveryounamedyourphpfile.php. If you have put hidden input fields on your form (the php you have there is looking for the 'userprint' and 'date' inputs, you can have it write to the file.
The HTML:
<form id="print" method="post" action="whateveryounamedyourphpfile.php" onsubmit="Javascript:window.print(); return true;">
<input type="hidden" name="date" value="yourvaluehere" />
<input type="hidden" name="userprint" value="yourvaluehere" />
<input type="submit" value="Print Certificate" />
</form>
The PHP:
<?php
$printfor = $_post['userprint'];
$date = $_post['date'];
if($printfor && $date) {
$text = "\n" . str_pad($_SESSION['printlogin'], 10) . "" . str_pad($printfor, 25) . "" . str_pad($dte, 15);
$fp = fopen ($filename, "a"); # a = append to the file. w = write to the file (create new if doesn't exist)
if ($fp) {
fwrite ($fp, $text);
fclose ($fp);
#$writeSuccess = "Yes";
#echo ("File written");
}
else {
#$writeSuccess = "No";
#echo ("File was not written");
}
}
//Echo your HTML here
?>
I created this JS Fiddle for you to get you on your way. http://jsfiddle.net/KEHtF/
It uses .ajax to submit the information to your PHP script while also invoking the print command.
Related
Here is my form, located in index.php:
<form method="post" action="newpage.php">
File name: <input type="text" name="filename"><br/>
File extension: <input type="text" name="fileext"><br/>
Title: <input type="text" name="title"><br/>
Body: <textarea style="height: 3em; width: 50em;" name="body"></textarea>
<input type="submit">
</form>
As you can see, this routes the results to newpage.php. Here is newpage.php:
<?php
$file = "/app/filecreation/newfiles/" . $_POST["filename"] . "." . $_POST['fileext'];
$writefile = fopen($file, 'w') or die('unable to open');
$code = "<html><body><h1><?php echo $_POST['title']; ?></h1><p><?php echo $_POST['body]; ?></body></html>";
fwrite($writefile, $code);
fclose($writefile);
?>
I am trying to create a file when the index.php form is submitted. For some reason, it is not creating a file as I would like. Is there something wrong with this code that it doesn't work?
Pls help!! Thanks so much!
Hi You have error in your line please change your line with this
$code = "<html><body><h1><?php echo $_POST['title']; ?></h1><p><?php echo $_POST['body]; ?></body></html>";
change into
$code = "<html><body><h1><?php echo '".$_POST['title']."' ?></h1><p><?php echo '".$_POST['body']."' ?></body></html>";
also make sure file_exist according to path $file. You can do this
if(file_exist($file)){
write your code here....
}
else{
echo "file not exist";
}
Hello I have a piece of code that writes to a text file, but I want the php file to completely overwrite the content of the text file, not just add to it, how must I rework my code to make it overwrite instead of just write. this is my code:
<form name="savefile" method="post" action="">
<input type="hidden" name="filename" value="code"><br/>
<textarea rows="40" cols="40" name="textdata" style ="font-family: "Lato", sans-serif;">
<?php echo file_get_contents('code.txt'); ?>
</textarea><br/>
<input type="submit" name="submitsave" value="Save">
</form>
<?php
if (isset($_POST)){
if ($_POST['submitsave'] == "Save" && !empty($_POST['filename'])) {
if(!file_exists($_POST['filename'] . ".txt")){
$file = tmpfile();
}
$file = fopen($_POST['filename'] . ".txt","a+");
while(!feof($file)){
$old = $old . fgets($file). "|___end message___|___begin message___|";
}
$text = $_POST["textdata"];
file_put_contents($_POST['filename'] . ".txt", $old . $text);
fclose($file);
}
}
?>
Overwrite is nothing but writing a same file, difference is file pointer is at the beginning of the file when you overrite. You can try with mode 'w'. php.net/manual/en/function.fopen.php
My program uses PHP to open a list of configuration settings from a txt file called "configurationSettings.txt" and puts the data from it onto a form.
What I'm trying to figure out is how to enable my program to update the data on the original txt file if the user changes anything through the form.
Here is an example of the txt file data:
Channel 7
4.0000
6.0000
Here is my code that reads the data and fills my form:
<?php
$configFile = fopen("configurationSettings.txt", "r");
$title1 = fgets($configFile);
$gain1 = fgets($configFile);
$offset1 = fgets($configFile);
fclose($configFile);
?>
<form action="program.php" method="post">
Channel 8 Title:<br>
<input type="text" name="channel0Title" value="<?php echo $title1 ?>">
<br>
Gain:<br>
<input type="text" name="channel0Gain" value="<?php echo $gain1 ?>">
<br>
Offset:<br>
<input type="text" name="Channel0Offset" value= "<?php echo $offset1 ?>">
<br>
<input type="submit" id ="submitButton" value="Submit">
</div>
</form>
And heres a picture of what it looks like:
What do I do to update the original txt file by pressing the submit button?
Tested, works 100%. You don't have to create .txt. Gets created automatically if not present.
index.html
<form action="program.php" method="post">
Channel 8 Title:<br><input type="text" name="channel0Title" value="Channel 7"><br>
Gain:<br><input type="text" name="channel0Gain" value="4.000"><br>
Offset:<br><input type="text" name="channel0Offset" value= "6.000"><br>
<input type="submit" id ="submitButton" value="Submit">
</form>
program.php
<?php
$title = $_POST["channel0Title"]; //You have to get the form data
$gain = $_POST["channel0Gain"];
$offset = $_POST["channel0Offset"];
$file = fopen('configurationSettings.txt', 'w+'); //Open your .txt file
ftruncate($file, 0); //Clear the file to 0bit
$content = $title. PHP_EOL .$gain. PHP_EOL .$offset;
fwrite($file , $content); //Now lets write it in there
fclose($file ); //Finally close our .txt
die(header("Location: ".$_SERVER["HTTP_REFERER"]));
?>
if(isset($_POST['field1']) && isset($_POST['field2'])) {
$data = $_POST['field1'] . '-' . $_POST['field2'] . "\n";
$ret = file_put_contents('/tmp/mydata.txt', $data, FILE_APPEND | LOCK_EX);
if($ret === false) {
die("There was an error writing this file");
}
else {
echo "$ret bytes written to file";
}}
I have two forms on one page that should give users posibility to load file to server(from URL or from user's PC)
<form method="post" action="bigorder.php" name="photourl">
<label for="photoorig">URL</label>
<input type="url" name="photoorig" placeholder="">
<input type="submit" value="Load" name="photoload">
<br>
</form>
<form method="post" action="bigorder.php" name="photofile" enctype="multipart/form-data">
<label for="photoloc">Load own file</label>
<input type="file" name="photoloc" id="photoloc">
<input type="submit" value="Load" name="photoload2">
</form>
And php
<?php
$tmpname=rand().".jpg";
if ($_POST['photoorig']) {
$file=file_get_contents($_POST['photoorig']);
$fp = fopen("/var/www/html/uploads/tmp/".$tmpname, "w");
fwrite($fp, $file);
fclose($fp);
}
if ($_POST['photoloc']) {
$tmpFile = $_FILES['photoloc']['tmp_name'];
$newFile = "/var/www/html/uploads/tmp/".$_FILES['photoloc']['name'];
$result = move_upload_file($tmpFile, $newFile);
echo $_FILES['photoloc']['name'];
if ($result) {
echo ' was uploaded<br />';
} else {
echo ' failed to upload<br />';
}
?>
First form loads files fine, but the second one doesn't work at all. I even don't receive any error message.
What am I doing wrong? Or missing something?
Here is the correct code,it is working.In your code curly braces was missing in second form and isset() function should use to check posted data set or not.
<?php
$tmpname=rand().".jpg";
if (isset($_POST['photoload'])) {
echo '1st';
$file=file_get_contents($_POST['photoorig']);
$fp = fopen("/var/www/html/uploads/tmp/".$tmpname, "w");
fwrite($fp, $file);
fclose($fp);
}
if (isset($_POST['photoload2'])) {
echo '2nd';
$tmpFile = $_FILES['photoloc']['tmp_name'];
$newFile = "/var/www/html/uploads/tmp/".$_FILES['photoloc']['name'];
$result = move_upload_file($tmpFile, $newFile);
echo $_FILES['photoloc']['name'];
if ($result) {
echo ' was uploaded<br />';
} else {
echo ' failed to upload<br />';
}
}
?>
I have a form that on submit creates a unique .txt filename and posts form data into that file. That works well. The issue is after it creates the file, I need a way to append it without creating another text file. Because it is a form, when it submits, the code reruns each time submit is called and I'm not sure how to handle it.
Here is the form:
<body>
<form action="write_to_txt_file2.php" method="POST">
<input name="field1" type="text" />
<input name="field2" type="text" />
<input type="submit" name="submit" value="Save Data">
</form>
Here is the php:
I don't have to use the microtime as the unique variable name so if there is a better way, I'm open to changing it.
<?php
$myDate = round(10*microtime(TRUE));
$filename = "tmp/".$myDate.".txt";
if(isset($_POST['field1']) && isset($_POST['field2'])) {
$data = $_POST['field1'] . '-' . $_POST['field2'] . "\n";
$ret = file_put_contents($filename, $data, FILE_APPEND | LOCK_EX);
if($ret === false) {
die('There was an error writing this file');
}
else {
echo "$ret bytes written to file";
}
}
else {
die('no post data to process');
}
?>
Alternatively, you could utilize sessions also to this, set the filename if not set using that same generator, so that you could use it again after the next submissions.
Simple example:
session_start();
if(!isset($_SESSION['file'])) {
$_SESSION['file'] = round(10*microtime(TRUE)); // set a filename on initial load
}
if(isset($_POST['submit'])) { // if submitted
$myDate = $_SESSION['file'];
$filename = "tmp/".$myDate.".txt";
if(isset($_POST['field1'], $_POST['field2'])) {
$data = $_POST['field1'] . '-' . $_POST['field2'] . "\n";
$ret = file_put_contents($filename, $data, FILE_APPEND | LOCK_EX);
if($ret === false) {
die('There was an error writing this file');
}
else {
echo "$ret bytes written to file";
}
}
else {
die('no post data to process');
}
}
if(isset($_POST['create'])) {
$_SESSION['file'] = round(10*microtime(TRUE)); // creates another filename
}
?>
<form action="" method="POST">
<input name="field1" type="text" />
<input name="field2" type="text" />
<input type="submit" name="submit" value="Save Data" />
<input type="submit" name="create" value="Create New" />
</form>