form outcome getting used as a variable in a different file - php

im working on a website where i want to take the outcome from a from and want them saved in a seperate file as php variables so i can read them out later on a diffrent part of the site. i can write some php code to a file but i writes the name of the variable and not the value.
<form action="change.php" method="post">
<input type="text" class="form-control" id="inputlocation1" placeholder="<?php include 'data1.php'; echo $location1 ?>">
<br>
<input type="text" class="form-control" id="inputdate1" placeholder="<?php include 'data1.php'; echo $date1?>">
<br>
<textarea name="description1" class="form-control"></textarea>
<br>
<input type="submit" name="submit" value="update data" class="btn btn-primary">
i use numbers behind the id's because i have multiple dates and locations i want to change. this is my current php code:
<?php
$file = fopen("data1.php", "w") or die("Unable to open file!");
$date = $_POST["inputdate1"];
$loc = $_POST["inputlocation1"];
$desc = $_POST["description1"];
fwrite($file, '<?php $loc1 = $loc' );
fwrite($file, '$date1 = $date');
fwrite($file, '$desc1 = $desc');
fwrite($file, '?>');
fclose($file);
?>
who can help me?

$data = '$desc1 =' . $desc;
fwrite($file, $data);
you must use SIGLE QUOTES

Related

Posts are duplicating in my php page when refreshing site

I cannot make my post system work because posts in comment section duplicate if I refresh the page
I am using only HTML and PHP. It's a forum for me and my friends.
There is also code above this but it is unimportant
<form action="" method="POST">
<label> Topic:
<input type="text" name="Topic" class="Input" style="width: 300px" required>
</label>
<br><br>
<label> Name:
<input type="text" name="Name" class="Input" style="width: 225px" required>
</label>
<br><br>
<label> Comment: <br>
<textarea name="Comment" class="Input" style="width: 300px" required></textarea>
</label>
<br><br>
<input type="Submit" name="Submit" value="Submit" class="Submit">
<!--idk-->
</form>
</center>
<hr>
<br>
</body>
<!--posts-->
</html>
<html>
<center>
</html>
<?php
if($_POST['Submit']){
print "<h1>Your comment has been submitted!</h1>";
}
?>
<html>
</center>
</html>
<?php
$Topic = $_POST['Topic'];
$Name = $_POST['Name'];
$Comment = $_POST['Comment'];
#Get old comments
$old = fopen("comments.txt", "r+t");
$old_comments = fread($old, 1024);
#Delete everything, write down new and old comments
$write = fopen("comments.txt", "w+");
$string = "<b>".$Topic."</b><br>".$Name."</b><br>".$Comment."</br>\n".$old_comments;
fwrite($write, $string);
fclose($write);
fclose($old);
#Read comments
$read = fopen("comments.txt", "r+t");
echo "<br><br>Comments<hr>".fread($read, 1024);
fclose($read);
?>
The problem you are facing is due to the way you are appending the comments to the "comments.txt" text file. The problem is that every time a comment is sent, all old and new comments are written to a text file. So when you refresh the page, the same comment is repeated.
Hope the following code helps
<?php
$Topic = $_POST['Topic'];
$Name = $_POST['Name'];
$Comment = $_POST['Comment'];
# Write the new comment to the top of the file
$write = fopen("comments.txt", "a+");
$string = "<b>".$Topic."</b><br>".$Name."</b><br>".$Comment."</br>\n";
fwrite($write, $string);
fclose($write);
# Read comments from the file
$read = fopen("comments.txt", "r");
echo "<br><br>Comments<hr>";
while(!feof($read)){
$line = fgets($read);
echo $line."<br>";
}
fclose($read);
?>

How can I save my form data to a local text file using php? [duplicate]

This question already has answers here:
Syntax error - unexpected ":" [closed]
(4 answers)
Closed 4 years ago.
I have a form in which I want to capture the name and email of a user and store it in a text file. I'm not sure if it's my code or if I have to save my script in a specific place. When the submit button is clicked I get an error that says "404 file or directory not found". My client is uploading them through ftp so I have no way to check if it works locally. The PHP script(process-form-data.php) and text file (formdata.txt) are saved within the public HTML folder. I'm very new to PHP so sorry if this is a dumb question. Any help on what I'm doing wrong and the process of uploading the files would be greatly appreciated, thanks!
<form name=”web_form” id=”web_form” method=”post” action=”process-
form-data.php”>
<label for="exampleInputEmail1"><h3 style="color: #eee; margin: 0;
font-weight: 400;">Request Information</h3></label>
<input type="text" name=”name” class="form-control" id="name"
placeholder="Your Name">
<input type="text" class="form-control" name=”email” id=”email”
placeholder="youremail#domain.com">
<button type="submit" name=”s1″ id=”s1″ value="submit" class="btn
btn-primary btn2">Submit</button>
</form>
<?php
$name = $_POST[‘name’];
$email = $_POST[’email’];
$fp = fopen(”formdata.txt”, “a”);
$savestring = $name . “,” . $email . “n”;
fwrite($fp, $savestring);
fclose($fp);
echo “<h1>You data has been saved in a text file!</h1>”;
?>
You should mind about your double quotes ("") written in your html code
<form name='web_form' id='web_form' method='post' action=''>
<label for="exampleInputEmail1"><h3 style="color: #eee; margin: 0;
font-weight: 400;">Request Information</h3></label>
<input type='text' name='name' class="form-control" id="name"
placeholder="Your Name">
<input type="text" class="form-control" name='email' id='email'
placeholder="youremail#domain.com">
<button type="submit" name='s1' id='s1' value="submit" class="btn btn-
primary btn2">Submit</button>
</form>
this code writes email and name in a text file
<?php
if (isset($_POST['s1'])) {
echo $_POST['name'];
echo $_POST['email'];
$myfile = fopen("data.txt", "w") or die("Unable to open file");
$name = $_POST['name']."\n";
fwrite($myfile, $name);
$email = $_POST['email']."\n";
fwrite($myfile, $email);
fclose($myfile);
}
?>

Something wrong with PHP

I'm trying to make a forum-like section for my website. It's not posting, and I don't know why. Here's my PHP and html
<?php
if ($_POST) {
$title = $_POST['title'];
$name = $_POST['name'];
$content = $_POST['commentContent'];
$handle = fopen("comments.html", "a");
fwrite($handle, "<h2 class='Roboto-Slab'>$title</h2>", "<br>", "<h3
class='Roboto-Slab'>By $name</h3>", "<p class='Roboto-Slab'>$content</p>");
fclose($handle);
}
?>
<form action="" method="POST">
<textarea class="comment-boxmain" rows="20" cols="40" name="commentContent"
placeholder="Start Typing...."></textarea><br>
<input class="comment-boxname" placeholder="Title" type="text"
name="title">
<input class="comment-boxname" placeholder="Your Name" type="text"
name="name">
<input class="comment-btn" type="submit" value="post"><br>
</form>
<?php include "comments.html"; ?>
Please check out the answer at cvmblog.com/forum.php if that will help.
String concatenation is done with dots (.), and not commas (,).
Replace:
fwrite($handle, "<h2 class='Roboto-Slab'>$title</h2>", "<br>", "<h3
class='Roboto-Slab'>By $name</h3>", "<p class='Roboto-Slab'>$content</p>");
With:
fwrite($handle, "<h2 class='Roboto-Slab'>$title</h2>". "<br>". "<h3
class='Roboto-Slab'>By $name</h3>". "<p class='Roboto-Slab'>$content</p>");
And it will work. However, this concatenation is useless. You can do simply:
fwrite($handle, "<h2 class='Roboto-Slab'>$title</h2><br><h3 class='Roboto-Slab'>By $name</h3><p class='Roboto-Slab'>$content</p>");
Also check if comments.html file has CHMOD 777. Furthermore, enable error_reporting on your php.ini file, as the PHP error thrown on this case could guide you to the error line easily.
Here's an implementation of your code secured against stored XSS (the vulnerability that allows people to insert HTML and Javascript code on your page) as well as RCE (remote code execution):
<?php
if ($_SERVER['REQUEST_METHOD']=='POST') {
$title = strip_tags($_POST['title']);
$name = strip_tags($_POST['name']);
$content = nl2br(htmlspecialchars($_POST['commentContent']));
$handle = fopen("comments.html", "a");
fwrite($handle, "<h2 class='Roboto-Slab'>$title</h2><br><h3
class='Roboto-Slab'>By $name</h3><p class='Roboto-Slab'>$content</p>");
fclose($handle);
}
?>
<form action="" method="POST">
<textarea class="comment-boxmain" rows="20" cols="40" name="commentContent"
placeholder="Start Typing...."></textarea><br>
<input class="comment-boxname" placeholder="Title" type="text"
name="title">
<input class="comment-boxname" placeholder="Your Name" type="text"
name="name">
<input class="comment-btn" type="submit" value="post"><br>
</form>
<?php echo file_get_contents("comments.html"); ?>
Also, do some searching about database engines (if you want to still using files, take a look on implementation of flat-files databases, as it's called).

Define php var after textbox

So I have a variable well defined in a php page and I'm using it in an HTML page using include.
I am currently building a page where I can change the Var ( because it's a long text, more than one actually, and to change them it will be nice to have a page with a layout just for that) so I'm using a textbox and a submit button just like this:
<?php
$titre= 'Bienvenido a PARIS EXPERT LIMOUSINE ! ' ;
?>
<form method="post">
Titre: <input name="titre" type="text" id="titre" value="<?php echo htmlspecialchars($titre); ?>" size="50" maxlength="50">
<input type="submit" name="submit">
</form>
<?php
if (isset($_POST['submit']))
{
$titre = $_POST['titre'];
echo($titre);
}
?>
The problem is that in the echo it shows the new text but if I do a refresh it will show the old one...
any ideas how can I do this?
EDIT: Added extra fields and data handler. See extra code below original answer.
Here is some code I came up with to write content to a file.
Note: To add to the file with content written one under the other, use the a or a+ switch.
To create and write content to file and overwrite previous content, use the w switch.
This method uses the fwrite() function.
(tested)
Added to OP's code: action="write.php"
FORM
<?php
$titre= 'Bienvenido a PARIS EXPERT LIMOUSINE ! ' ;
?>
<form method="post" action="write.php">
Titre: <input name="titre" type="text" id="titre" value="<?php if(isset($_POST['titre'])){echo htmlspecialchars($_POST['titre']); }
else echo htmlspecialchars($titre); ?>" size="50" maxlength="50">
<input type="submit" name="submit">
</form>
PHP write to file handler (write.php)
This example uses the w switch.
<?php
if (isset($_POST['submit']))
{
$titre = $_POST['titre'];
echo($titre);
}
?>
<?php
$filename = "output.txt"; #Must CHMOD to 666 or 644
$text = $_POST['titre']; # Form must use POST. if it uses GET, use the line below:
// $text = $_GET['titre']; #POST is the preferred method
$fp = fopen ($filename, "w" ); # w = write to the file only, create file if it does not exist, discard existing contents
if ($fp) {
fwrite ($fp, $text. "\n");
fclose ($fp);
echo ("File written");
}
else {
echo ("File was not written");
}
?>
EDIT: Added extra fields and data handler.
Extra fields can be added, and must be followed in the same fashion in the file handler.
NEW FORM with extra fields
File data example: test | email#example.com | 123-456-7890
<?php
$titre= 'Bienvenido a PARIS EXPERT LIMOUSINE ! ' ;
?>
<form method="post" action="write.php">
Titre: <input name="titre" type="text" id="titre" value="<?php if(isset($_POST['titre'])){echo htmlspecialchars($_POST['titre']); }
else echo htmlspecialchars($titre); ?>" size="50" maxlength="50">
<br>
Email: <input name="email" size="50" maxlength="50">
<br>
Telephone: <input name="telephone" size="50" maxlength="50">
<input type="submit" name="submit">
</form>
<?php
if (isset($_POST['submit']))
{
$titre = $_POST['titre'];
echo($titre);
}
?>
PHP write to file handler
<?php
$titre = $_POST['titre'];
$email = $_POST['email'];
$telephone = $_POST['telephone'];
$data = "$titre | $email | $telephone";
$fp = fopen("data.txt", "a"); // a-add append or w-write overwrite
if ($fp) {
fwrite ($fp, $data. "\n");
fclose ($fp);
echo ("File written successfully.");
}
else{
echo "FAILED";
}
?>
<?php
if(!($titre = file_get_contents("filename.txt"))){
$titre= 'Bienvenido a PARIS EXPERT LIMOUSINE ! ' ;
}
?>
<form method="post">
Titre: <input name="titre" type="text" id="titre" value="<?php echo htmlspecialchars($titre); ?>" size="50" maxlength="50">
<input type="submit" name="submit">
</form>
<?php
if (isset($_POST['submit'])) {
$titre = $_POST['titre'];
if(#file_put_contents("filename.txt", $titre))){
echo 'Success - var stored.';
} else { echo 'Some error.'; }
echo($titre);
}
?>
Try this :
Titre: <input name="titre" type="text" id="titre" value="<?php if(isset($_POST['titre'])){echo htmlspecialchars($_POST['titre']); }
else echo htmlspecialchars($titre); ?>" size="50" maxlength="50">
If you need to keep your value for ever, you should store it in a database or save it in a file (could be .txt).
[EDIT]
Here is the code for .txt solution (you first create a file.txt in the same folder):
<?php
$file = 'file.txt';
$lines = file("file.txt");
if (!isset($lines[0])) {$titre='Bienvenido a PARIS EXPERT LIMOUSINE ! ';}
else {$titre=$lines[0];}
?>
<form method="post">
Titre: <input name="titre" type="text" id="titre" value="<?php if(isset($_POST['titre'])){echo htmlspecialchars($_POST['titre']); }
else echo htmlspecialchars($titre); ?>" size="50" maxlength="50">
<input type="submit" name="submit">
</form>
<?php
if (isset($_POST['submit']))
{
echo($_POST['titre']);
$titre = $_POST['titre']."\n".$titre;
file_put_contents($file, $titre);
}
?>
Hope it helps :)
this is normal because you're showing the new content upon form submission. When you refresh the page, unless you tell it to send the POST data again with the refresh (which the browser asks you for confirmation), your form (and hence the input field) will have nothing in.

store offline html form data in a csv file

I'm looking for a simple solution to what is hopefully a simple problem. I want to have a laptop set up with an offline html file with a very short form that feeds a csv file. I've been eyeing the fputcsv() function to do this, but I'm not the most talented programmer. If I have a simple form that looks like this:
<?php
if(isset($_POST['submit']))
{
$myfile = fopen('file.csv', 'w');
fputcsv($myfile,
array($_POST['first-name'], $_POST['last-name'], $_POST['email']));
fclose($myfile);
}
?>
<article role="main">
<header role="banner">
<h1>Email Updates</h1>
</header>
<section>
<form id="form1" name="form1" method="post" action="<?=$_SERVER['PHP_SELF'];?>">
<input type="text" id="first-name" maxlength="100" autocorrect placeholder="First name" />
<input type="text" id="last-name" maxlength="100" autocorrect placeholder="Last name" />
<input type="text" id="email" maxlength="100" autocorrect placeholder="Email address" />
<button type="submit" id="submit" class="oneup">Submit</button>
</form>
</section>
</article>
what kind of code do I need to have it feed a simple csv file?
When (if) the form is submitted (correctly), do this:
if( $fp = fopen('file.csv', 'w') )
{
fputcsv($fp, $_POST);
}
fclose($fp);
When this form is submitted, it will populate the $_POST array.
So, you should add some PHP code that handles the submitted values.
For example:
<?php
if(isset($_POST['submit']))
{
$myfile = fopen('file.csv', 'w');
fputcsv($myfile,
array($_POST['first-name'], $_POST['last-name'], $_POST['email']));
fclose($myfile);
}
?>
Similar to djot's answer, I'd use this:
if( $fp = fopen('file.csv', 'w') ){
fputcsv($fp, print_r($_POST, true));
}
fclose($fp);
Note the print_r with the true flag, as this makes it more human readable.
If you actually wanted to write it as a CSV, just use:
$data = implode(',' $_POST);
if( $fp = fopen('file.csv', 'w') ){
fputcsv($fp, $data);
}
fclose($fp);

Categories