I want to make an html document update with input from a form
The goal is to be able to enter the URL, enter the description, enter the date created and output to a file.
My thoughts are to break the HTML document into pieces, begin.txt newarticle.txt and end.txt
Then piece it back together using fopen and fwrite.
I'm sure there's an easier way, but this is how I'm currently trying to do it.
<html>
<body bgcolor="#FFFFFF>
<H1>Add new Article</h1>
<form action="newarticle.php" method="post">
Paste the link address
<input type="text" name="url">
</br>
Paste the description:
<input type="text" name="description">
</br>
Paste the date article was released:
<input type="text" name="date">
<p>
<input type=submit value="Create Article">
</form>
</body>
</html>
newarticle.php
<?php
$v1 = $_POST["url"]; //You have to get the form data
$v2 = $_POST["description"];
$v3 = $_POST["date"];
$file = fopen('newarticle.txt', 'w+'); //Open your .txt file
ftruncate($file, 0); //Clear the file to 0bit
$content = $v1. PHP_EOL .$v2. PHP_EOL .$v3;
fwrite($file , $content); //Now lets write it in there
fclose($file ); //Finally close our .txt
die(header("Location: ".$_SERVER["HTTP_REFERER"]));
?>
This gives me the output on three separate lines.
How do I have it create a file with content formatted it into an actual piece of code :
<li><a href=$v1>$v2</a></li>
<li>$v3</li>
If you don't mind the format of the html always being exactly the same with the same set of elements, but different attribute values and inner HTML, you can use a heredoc to build up the html. Basically a multi-line string. For example:
$v1 = "info from the form";
$v2 = "more info!";
$built = <<<EOF
<li>$v1</li>\n
<li>$v2</li>
EOF;
echo $built;
this will output:
<li>info from the form</li>
<li>more info!</li>
Related
I want to get images from different URL and save them on my current folder, so I cant get them later, here is my code :
in my #upload.html I made this :
<html>
<body>
<div>
<form method="post" action="upload_image.php">
<input type="text" name="img_url" placeholder="Enter Image URL">
<input type="submit" name="get_image" value="Submit">
</form>
</div>
</body>
</html>
and in my #upload_image.php I made this following code:
<?php
if(isset($_POST['get_image']))
{
$url=$_POST['img_url'];
$data = file_get_contents($url);
$new = 'new_image.jpg';
file_put_contents($new, $data);
echo "<img src='new_image.jpg'>"; } ?>
And by the way guys its my first time here, thanks for answering
Is this good for you?
you put the image URL one for row in a textarea then get the textarea rows and cycle to get the images.
I also added a random number to have different image names but you can put whatever you want in the filename
php in upload_image.php
<?php
if(isset($_POST['get_image']))
{
$text = trim($_POST['images_url']);
$textAr = explode("\n", $text);
$textAr = array_filter($textAr, 'trim'); // remove any extra \r characters left behind
foreach ($textAr as $url) {
$data = file_get_contents($url);
$new = 'new_image_'.rand(10, 3000).'.jpg';
file_put_contents('upload/'.$new, $data);
echo '<img src="upload/'.$new.'">';
}
}
?>
HTML form
<form method="post" action="upload_image.php">
<textarea name="images_url" placeholder="insert one url per row"></textarea>
<input type="submit" name="get_image" value="Submit">
</form>
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
I have a very basic PHP file. i want to have two textboxes for user input, and a submit button. The user will enter their first and last name, then i would like to append or create a TXT file with the data entered from field1 and field2.
Possibly i am going about this the wrong way. I will post two of the ways i have been tinkering around with.
<html>
<head>
<title>Field1 & 2</title>
</head>
<body>
<form>
What is your name?<br>
<input type="text" name="field1"><br>
<input type="text" name="field2"><br>
<input type="submit" value="Submit">
</form>
<?php
$txt= $_POST['field1'].' - '.$_POST['field2'];
$var_str3 = var_export($txt, true); //is this necessary?
$var3 = "$var_str3"; //is this necessary?
file_put_contents('fields.txt', $var3.PHP_EOL, FILE_APPEND);
?>
</body>
</html>
I cant figure out how to get the data from field1 and field2 into a string variable.
I have also messed around with using this php instead of the section listed above
<?php
$txt= "data.txt";
if (isset($_POST['field1']) && isset($_POST['field2'])) {
$fh = fopen($txt, 'a');
$txt=$_POST['field1'].' - '.$_POST['field2'];
fwrite($fh,$txt); // Write information to the file
fclose($fh); // Close the file
}
?>
You should learn about HTML Forms And PHP Form Handling.
In your code you have to use a form HTTP method. And the form data must sent for processing to a PHP file.
In this code i use HTTP PSOT method you can also use GET method the result will be same. This two method is used for collecting the form data. And the php file name is "action.php".
index.html
<html>
<head>
<title>Field 1 & 2</title>
</head>
<body>
<form action="action.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>
action.php
<?php
$path = 'data.txt';
if (isset($_POST['field1']) && isset($_POST['field2'])) {
$fh = fopen($path,"a+");
$string = $_POST['field1'].' - '.$_POST['field2'];
fwrite($fh,$string); // Write information to the file
fclose($fh); // Close the file
}
?>
Let's take a snippet from http://www.w3schools.com/html/html_forms.asp
<form action="action_page.php" method="post">
First name:<br>
<input type="text" name="firstname" value=""><br>
Last name:<br>
<input type="text" name="lastname" value=""><br><br>
<input type="submit" value="Submit">
</form>
Note the first line: upon form submission a php script is called: action_page.php.
action_page.php is your webpage with the form and the embedded php script. action_page.php both displays the empty form and then process the submitted data.
On the first line also it is specified that the submitted data is sent with the POST method.
The php part will look like this:
<?php
if( isset($_POST['firstname'] ) && isset( $_POST['lastname'] ) )
{
$txt= $_POST['firstname'].' - '.$_POST['lastname'] . PHP_EOL;
file_put_contents('fields.txt', $txt, FILE_APPEND);
}
?>
The if statement is there because the first time the script action_page.php is loaded its purpose is only to display the form and don't receive any POST data.
As the form is submitted by the user the script will receive the data and store to file.
The script will also (with this approach) display again an empty form ready for the submission of another entry.
You can rearrange things in order to have two web pages: one with just the form, another one with a "Thank you" message and the data processing php script.
I have a text file in the following format, let's call it stats.txt:
valueA1, valueA2, valueA3, valueA4
valueB1, valueB2, valueB3, valueB4
valueC1, valueC2, valueC3, valueC4
valueD1, valueD2, valueD3, valueD4
I want four text boxes or textareas to populate with this code in an html document and when form is submitted, append to the corresponding lines of the text file. I'm not very knowledgeable with php so can't figure out a way to do this.
My current html file looks like this:
$url = 'editor.php';
$file = 'stats.txt';
// check if form has been submitted
if (isset($_POST['text1']))
{
// save the text contents
file_put_contents($file, $_POST['text1']);
// redirect to form again
header(sprintf('Location: %s', $url));
printf('Updated.', htmlspecialchars($url));
exit();
}
// read the textfile
$text = file_get_contents($file);
?>
I couldn't figure out how to loop between the lines of the text file, so have this chunk of horrible code to get the value of each line:
<?php
//for Line 1
$myLine1 = 1 ;
$linefile1 = new SplFileObject($file);
$linefile1->seek($myLine1-1);
//for Line 2
$myLine2 = 2 ;
$linefile2 = new SplFileObject($file);
$linefile2->seek($myLine2-1);
//for Line 3
$myLine3 = 3 ;
$linefile3 = new SplFileObject($file);
$linefile3->seek($myLine3-1);
//for Line 4
$myLine4 = 4 ;
$linefile4 = new SplFileObject($file);
$linefile4->seek($myLine4-1);
?>
And the form looks like this:
<form action="" method="post">
#stuff 1
<textarea id="stuff" name="stuff[]"><?php echo htmlspecialchars($linefile1) ?></textarea>
#stuff 2
<textarea id="stuff" name="stuff[]"><?php echo htmlspecialchars($linefile2) ?></textarea>
#stuff 3
<textarea id="stuff" name="stuff[]"><?php echo htmlspecialchars($linefile3) ?></textarea>
#stuff 4
<textarea id="stuff" name="stuff[]"><?php echo htmlspecialchars($linefile4) ?></textarea>
<input type="submit" />
<input type="reset" />
</form>
Can someone help me with this please?
Just loop through the lines and display textareas. Using file() to put the file into array
<?php $file = file('stats.txt');?>
<form action="" method="post">
<?php
foreach($file as $line) {
echo "<textarea id='stuff' name='stuff[]'>".htmlspecialchars($line)."</textarea>";
};?>
<input type="submit" />
<input type="reset" />
</form>
Then when you submit the form
if(isset($_POST['stuff'])){
$text = join("\n", $_POST['stuff']);
file_put_contents($file, $text);
};
Here is a code I wrote to store and retrieve data from a text file, that you can use as an example:
// Save Check Boxes in Serial Form for later retrieval
$myFile = "equipment.txt";
if($save == "yes"){
$stringData = serialize(array($container_20,$container_40,$container_45,$container_RF,$container_HQ,$container_Chassis));
file_put_contents($myFile, $stringData);
}
// Read Saved File and Populate Check Boxes
$recoveredData = file_get_contents($myFile);
list($container_20,$container_40,$container_45,$container_RF,$container_HQ,$container_Chassis) = unserialize($recoveredData);
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