I have an HTML file that contains the following form:
<form action="" class="contact-form">
<div class="input-group tm-mb-30">
<input name="username" type="text" class="form-control rounded-0 border-top-0 border-end-0 border-start-0" placeholder="Name">
</div>
<div class="input-group tm-mb-30">
<input name="email" type="text" class="form-control rounded-0 border-top-0 border-end-0 border-start-0" placeholder="Email">
</div>
<div class="input-group tm-mb-30">
<textarea rows="5" name="message" class="textarea form-control rounded-0 border-top-0 border-end-0 border-start-0" placeholder="Message"></textarea>
</div>
<div class="input-group justify-content-end">
<input type="submit" class="btn btn-primary tm-btn-pad-2" value="Save">
</div>
</form>
It seems fine to me. I also have a php script:
> <?php
extract($_REQUEST);
> $file=fopen("form.txt.", "a");
fwrite($file, "----");
fwrite($file," name :");
fwrite($file, $username ."\n");
fwrite($file," email :");
fwrite($file, $email ."\n");
frwite($file," message .:");
fwrite($file, $message ."\n");
fclose($file);
> ?>
Both are in the same file, the form in between html tags, the php script after the /html tag.
If will not execute. No matter what I do, form.txt remains empty. form.txt is in the same directory and has 777.
Since I cannot find any problem with the script (there are no error messages in the apache log file), I am wondering if there is something wrong with the php on this server (there are also no entries in syslog). phpinfo page displays fine, and php --version tells me 8.2.1 is running.
I then changed action in form to "form.php" and added method=POST to test with separate script. form.php was simply:
<?php
echo "NAME:";
echo $username;
echo "EMAIL:";
echo $email;
echo "MESSAGE:";
echo $message;
echo "POSTNAME:";
echo $_POST['username'];
echo "POSTEMAIL:";
echo $_POST['email'];
echo "POSTMESSAGE:";
echo $_POST['message'];
?>
I did not get any output for the first three entries. But I got output for the last three entries. I expected to get output for all - since the form, by sending the inputs, should automatically create/define those variables. Am I wrong?
I then added this to form.php (in front of the code mentioned above) in order to define the variables:
if(isset($_POST['submit']))
{
//fetch form data
$username = $_POST['username'];
$email = $_POST['email'];
$message = $_POST['message'];
}
because I thought defining them would solve the problem. It did not. When executing form.php (by sending the form in the html), apache.log now tells me that $username, $email and $message are not defined. But I just defined them.... ärx
Remove the last dot (.) from filename form.txt.
WRONG FILE NAME form.txt.
$file=fopen("form.txt.", "a");
CORRECT FILE NAME
$file=fopen("form.txt", "a");
Now all entries will go to form.txt file.
Full Code is shared here:
Add form method as POST and add name='submit' to submit button
<form action="" class="contact-form" method="post">
<div class="input-group tm-mb-30">
<input name="username" type="text" class="form-control rounded-0 border-top-0 border-end-0 border-start-0" placeholder="Name">
</div>
<div class="input-group tm-mb-30">
<input name="email" type="text" class="form-control rounded-0 border-top-0 border-end-0 border-start-0" placeholder="Email">
</div>
<div class="input-group tm-mb-30">
<textarea rows="5" name="message" class="textarea form-control rounded-0 border-top-0 border-end-0 border-start-0" placeholder="Message"></textarea>
</div>
<div class="input-group justify-content-end">
<input type="submit" class="btn btn-primary tm-btn-pad-2" value="Save" name="submit">
</div>
</form>
If form is submitted then write to file. Also you have typo mistake in frwite which is corrected too here.
if(isset($_POST['submit'])){
extract($_REQUEST);
$file=fopen("form.txt", "a");
fwrite($file, "----");
fwrite($file," name :");
fwrite($file, $username ."\n");
fwrite($file," email :");
fwrite($file, $email ."\n");
fwrite($file," message .:");
fwrite($file, $message ."\n");
fclose($file);
}
So this solved it for me:
I added "name=submit" to the submit input in HTML. The form posts to form.php, which now looks like this:
<html>
<body>
<?php
// turn on error reporting
ini_set('error_reporting', 'on');
error_reporting(E_ALL);
// Remove all illegal characters from email
// $email_address = filter_var($email_address, FILTER_SANITIZE_EMAIL);
// check if form has been submitted
if(isset($_POST['submit']))
{
//fetch form data
$username = $_POST['username'];
$email = $_POST['email'];
$message = $_POST['message'];
//write form data
$fp = fopen('form.txt', 'a');
fwrite($fp, "---");
fwrite($fp,"name :");
fwrite($fp, $username ."\n");
fwrite($fp," email :");
fwrite($fp, $email ."\n");
fwrite($fp," message :");
fwrite($fp, $message ."\n");
fclose ($fp);
}
//show submitted data
echo "NAME:";
echo $username;
echo "<br>";
echo "EMAIL:";
echo $email;
echo "<br>";
echo "MESSAGE:";
echo $message;
echo "<br>";
echo "POSTNAME:";
echo $_POST['username'];
echo "<br>";
echo "POSTEMAIL:";
echo $_POST['email'];
echo "<br>";
echo "POSTMESSAGE:";
echo $_POST['message'];
echo "<br>";
// extract($_REQUEST); might need to be added
?>
The data has been submitted - thanks
</body>
</html>
For some reason, the extract command seems unnecessary; it works fine without it, but I am not sure why. Anyhow, the data is now processed into the file. I got it. Thanks so much, everyone!
Related
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);
?>
<?php
$fp = fopen("users.txt", "w");
if(!$fp) die ("Errore nella creazione dell'utente");
$fp=fwrite($email $password);
fclose($fp);
$name="";
$surname="";
$email="";
$password="";
$nazionalita="";
$telefono="";
$errors= array();
if($_SERVER["REQUEST_METHOD"]=="POST"){
$name = htmlspecialchars($_POST["name"]);
$surname = htmlspecialchars($_POST["surname"]);
$email = htmlspecialchars($_POST["email"]);
$password = htmlspecialchars($_POST["password"]);
$nazionalita = htmlspecialchars($_POST["nazionalita"]);
$telefono = = htmlspecialchars($_POST["nazionalita"]);
}
if(empty($name)) {
$errors[] = "Name is required!";
}
if(empty($surname)) {
$errors[] = "Surname is required!";
}
if(empty($email)) {
$errors[] = "Email is required!";
}
if(empty($password)) {
$errors[] = "Password is required!";
}
header("location: index.php");
?>
But of course, this doesn't work.
How can I save to a specific location with PHP?
Then I'll have to do an explode which will only get my email and password. on the login.php page.
Thank you.
I'm not going to fix your code for you, but give you three tips:
Think like a computer. The computer is going to run your program one statement at a time, in the order you've written them; so if you have a line that writes a variable to a file, it needs to come after the line where you've put a value into that variable. It is also going to do exactly what you ask it, not guess what you meant, and not let you get away with typos; read your code back carefully.
Make use of the PHP manual. If you type in https://php.net/ followed by the name of a built-in function, like https://php.net/fwrite, you will get a page that explains what the input and output of that function is. Often, there are examples which show various ways of using the function, which you can use as inspiration for how to write your own code.
Turn on the display_errors setting, or learn where your log file is. Most of the mistakes in the code you show will result in errors or warnings, telling you exactly which line you need to look at. Read the messages carefully, and they'll often point out exactly what you've done wrong.
If you want to write to the file you could do $fpw = fwrite($fp,$email.' : '.$password);
you should pass the $fp to fwrite() method as follow
$fp = fopen('user.txt', 'w+') or die('Unable to open file!');
$fp=fwrite($fp, $email.'~'.$password.'\n'); // make sure to choose whatever seperator suits your need and be careful when choosing it
fclose($fp);
Thanks everyone, I fixed my code!!!
Following the correct code!
<?php
$name="";
$surname="";
$email="";
$password="";
$nazionalita="";
$telefono="";
$errors= array();
//$fp = fopen("users.txt", "w");
//$fpw = fwrite($fp,$dati $email.' : '.$password);
//fclose($fp);
if(isset($_POST["submitPut"])){
$name = htmlspecialchars($_POST["name"]);
$surname = htmlspecialchars($_POST["surname"]);
$email = htmlspecialchars($_POST["email"]);
$password = htmlspecialchars($_POST["password"]);
$nazionalita = htmlspecialchars($_POST["nazionalita"]);
$telefono = htmlspecialchars($_POST["telefono"]);
$dati= "\n". $name . ",". $surname .",".$email.",". $password . ",". $nazionalita. ",". $telefono;
$file= fopen("users.txt", "a+");
$credenziali=fwrite($file, $dati);
fclose($file);
if(empty($name)) {
$errors[] = "Name is required!";
}
if(empty($surname)) {
$errors[] = "Surname is required!";
}
if(empty($email)) {
$errors[] = "Email is required!";
}
if(empty($password)) {
$errors[] = "Password is required!";
}
}
//header("location: index.php");
?>
<!DOCTYPE html>
<html lang="en">
<?php include_once "views/head.php" ?>
<body>
<?php include_once "views/navbar.php" ?>
<main>
<div class="container my-5">
<h2>Registration user</h2>
<form action="registration.php" method="post">
<div class="form-group">
<label for="name">Name:</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Enter name" >
</div>
<div class="form-group">
<label for="surname">surname:</label>
<input type="text" class="form-control" id="surname" name="surname" placeholder="Enter surname" >
</div>
<div class="form-group">
<label for="email">email:</label>
<input type="email" class="form-control" id="email" name="email" placeholder="Enter email" >
</div>
<div class="form-group">
<label for="password">password:</label>
<input type="text" class="form-control" id="password" name="password" placeholder="Enter password">
</div>
<div class="form-group">
<label for="nazionalita">nazionalita:</label>
<input type="text" class="form-control" id="nazionalita" name="nazionalita" placeholder="Enter nazionality" >
</div>
<div class="form-group">
<label for="telefono">telefono:</label>
<input type="text" class="form-control" id="telefono" name="telefono" placeholder="Enter phone" >
</div>
<button type="submit"name="submitPut" class="btn btn-primary">Submit</button>
</form>
</div>
</main>
<?php include_once "views/footer.php" ?>
<?php include_once "views/scripts.php" ?>
</body>
</html>
I am trying to appending emails that have signup'd to a csv file, but it shows success, whereas there is nothing in the file, nothing is appending and when i print_r the fwrite it says 15
This is my code
<form action="send.php" method="post" name="newsletter" class="">
<label class="sr-only">Email:</label>
<div class="input-group mb-2">
<input type="email" name="email" class="form-control rounded-right bg-transparent" placeholder="johndoe#example.com" aria-label="Username" aria-describedby="basic-addon1" style="border: 1px solid #bdbdbd;">
<button type="submit" class="home-signup-email-btn btn btn-blue btn-lg ml-3 rounded py-0">Sign Up</button>
</div>
</form>
$email = $_POST['email'];
//$filename = 'suscribers.txt';
//$somecontent = "$email\n";
// Let's make sure the file exists and is writable first.
$txt = '/n'.$_POST['email'].'/n';
$file = fopen('mailing_list.csv','a');
if (fwrite($file,$txt)){
// fwrite($file,$txt);
$message= "Success!. You have been added to our email list.";
$status = "success";
$data = array(
'status' => $status,
'message' => $message
);
echo json_encode($data);}
else
echo "fail";
Try using:
$file = 'people.txt';
// The new person to add to the file
$person = "John Smith\n";
// Write the contents to the file,
// using the FILE_APPEND flag to append the content to the end of the file
// and the LOCK_EX flag to prevent anyone else writing to the file at the same time
file_put_contents($file, $person, FILE_APPEND | LOCK_EX);
Use a+ mode:
Open a file for read/write. The existing data in file is preserved. File pointer starts at the end of the file. Creates a new file if the file doesn't exist
<form action="send.php" method="post" name="newsletter" class="">
<label class="sr-only">Email:</label>
<div class="input-group mb-2">
<input type="email" name="email" class="form-control rounded-right bg-transparent" placeholder="johndoe#example.com" aria-label="Username" aria-describedby="basic-addon1" style="border: 1px solid #bdbdbd;">
<button type="submit" class="home-signup-email-btn btn btn-blue btn-lg ml-3 rounded py-0">Sign Up</button>
</div>
</form>
$email = $_POST['email'];
// Let's make sure the file exists and is writable first.
$txt = '/n'.$_POST['email'].'/n';
$file = fopen('mailing_list.txt','a+');
if (fwrite($file,$txt)){
$message= "Success!. You have been added to our email list.";
$status = "success";
$data = array(
'status' => $status,
'message' => $message
);
echo json_encode($data);
fclose($file);
chmod($file, 0777);
}else {
echo "fail";
}
I found a solution it wasn't writing to the file because i didn't add an fclose to close the fopen
I tried to make a simple PHP program that writes text from an input field and puts it in a .txt file. What is wrong with my code? It doesn't leave spaces between items and copies the previous item and doubles it. The file is called email.txt . Here is the code:
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$file = fopen("email.txt", "r+") or die("<h1>Eroarea 1</h1>"); //In caz ca fisierul nu este gasit
$s = fread($file, filesize("email.txt"));
$s = $name . "\n";
fputs($file, $s) or die("<h1>Eroarea 2</h1>"); //In caz ca server-ul nu poate fi contactat
fclose($file);
echo "<h1></h1>";
} ?>
<section id="five" class="wrapper style2 special fade">
<div class="container">
<header>
<h2>Writer</h2>
<p>Put text here</p>
</header>
<form method="post" action="#" class="container 50%" onSubmit="post">
<div class="row uniform 50%">
<div class="8u 12u$(xsmall)"><input type="text" name="name" placeholder="Email" /></div>
<div class="4u$ 12u$(xsmall)"><input type="submit" name="submit" value="Send" class="fit special" /></div>
</div>
</form>
</div>
</section>
A much easier way to append content to a file would be to use file_put_contents():
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
file_put_contents('email.txt', $name . PHP_EOL, FILE_APPEND);
}
?>
That will create the file if it doesn't exist and append to it if it does.
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).