I am struggling with the problem on my website. I should create an form to fill which will display new information/news. I have code like this:
<?php
include "0begin.php";
$title=$_POST["title"];
isset($title) or $title=$_GET["title"];
$msg=$_POST["msg"];
isset($msg) or $msg=$_GET["msg"];
?>
<h1>News</h1>
<form method="POST">
Title<br><input type=text input name="title" value=<?=$title?> ><br>
News<br>
<textarea input name="msg" cols=40 rows=5> </textarea><br>
<input type="submit">
<br><br>
</form>
<?php
$dateposted=date("YmdHis");
if (!empty($title) and !empty($msg)) {
$fp=fopen("/home/aqueen/public_html/news/".$dateposted."txt", "w");
fwrite($fp,$title,$msg);
fwirte($fp,$msg);
fclose($fp);
include "/home/aqueen/public_html/news/".$dateposted."txt"; }
?>
My questions:
1) How can I fix that code? It generates new file but without content inside.
2) It doesn't show the new file on the website /probably doesn't include it properly/
3) When it starts working- how I can let someone delete news from website without opening the code? /online/
Thank you in advance :)
Try this code:
<?php
//include "0begin.php";
$title = $_POST["title"];
$msg = $_POST["msg"];
?>
<h1>News</h1>
<form method="POST">
Title<br>
<input type="text" name="title"><br>
News<br>
<textarea name="msg" cols="40" rows="5"></textarea><br>
<input type="submit">
<br><br>
</form>
<?php
$dateposted=date("YmdHis");
if (!empty($title) and !empty($msg)) {
if (!file_exists('news/')) {
mkdir("news/", 0755);
}
if(file_exists($_SERVER['DOCUMENT_ROOT'] . "/news/")){
$pathGenerated = $_SERVER['DOCUMENT_ROOT'] . "/news/";
}
if(file_exists( "news/")){
$pathGenerated = "news/";
}
$pathGenerated = str_replace('//', '', $pathGenerated);
$fp=fopen($pathGenerated."".$dateposted.".txt", "w");
$textInsert = "Titolo: ".$title." \nMessaggio: ".$msg;
fwrite($fp,$textInsert);
fclose($fp);
include $pathGenerated."".$dateposted.".txt";
}
?>
Auto create folder /news/ if not exist.
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);
?>
Pretty much in the title. I want to make a solution that saves the input data of a html form and display the entire csv file in a separate page that isn't connected. Here's the code I'm currently using that isn't saving the input data nor display the csv file.
Input Data Page:
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<h2>PHP Form Validation Example</h2>
<form method="post" name="test1" action="" onsubmit="required()">
Name: <input type="text" name="name">
<br><br>
E-mail: <input type="text" name="email">
<br><br>
Website: <input type="text" name="website">
<br><br>
Comment: <textarea name="comment" rows="5" cols="40"></textarea>
<br><br>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$web = $_POST['website'];
$comment = $_POST['comment'];
$csv =
array($_POST['name'],$_POST['email'],$_POST['website'],$_POST['comment']);
$file = fopen('form.csv', 'a');
foreach ($csv as $line)
{
fputcsv($file,explode(',',$line));
}
fclose($file);
?>
</html>
PHP:Display CSV file
<!DOCTYPE html>
<html>
<?php
echo "</br >";
echo '<table style="width=100%">';
// Print other student details here (minus the private stuff)
//The CSV file is read and printed on the page when using the 'r' value.
$myfile = fopen("form.csv" , "r") or die ("Unable to open file");
while(!feof($myfile)) {
echo fgets($myfile) . "<br>";
}
fclose($myfile);
echo "</table>";
?>
<body>
</body>
</html>
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).
help i have bit knowledge in html,css but
im kinda new to php forms,database,etc stuffs
my php is working well, when user click submit all forms goes to carlist.txt
but i want it output it through webpage html instead of .txt so i can view it online
how i can able to do that?
and i dont want the users sees their submisions... only me can view it
html :
<html>
<form action"awesome.php" method=get>
First name: <input type="text" name="firstname"><br>
blah.. blahh.
<select name="cars">
<option value="volvo">Volvo XC90</option>
<option value="saab">Saab 95</option>
<option value="mercedes">Mercedes SLK</option>
<option value="audi">Audi TT</option>
</select>
<input type="submit" value="Submit">
</form>
etc. etc. blah blah
</html>
awesome.php:
<?php
header("Location: thanksforsubmiting.html");
$handle = fopen("carslist.txt", "a");
foreach($_GET as $variable => $value) {
fwrite($handle, $variable);
fwrite($handle, "=");
fwrite($handle, $value);
fwrite($handle, "\r\n");
}
fwrite($handle, "\r\n");
fclose($handle);
exit;
?>
thanksforsubmitting.html:
<html>
<title>thank you</title>
<h1> <font color=red>
thank you for submission </h1></font>
</html>
please be gentle to me im kinda newbie :)) TIA
<?php
foreach($_GET as $variable => $value) {
echo $value."<br />";
}
?>
If you want view it online create another page with something like this
$file = file_get_contents('carlist.txt');
$rows = explode("\n", $file);
foreach($rows as $row) {
print_r($row);
}
at awesome.php write following code
if(isset($_GET['submit'])){
$firstName = $_GET['firstname'];
$car = $_GET['cars'];
echo $firstname;
echo $car;
}
ok here is an example code you want. i'm using just 2 files form.php and form1.php. Form.php contains:
<?php
if(isset($_POST['submit'])){
$fname = $_POST['fname'];
$lname = $_POST['lname'];
header("Location:form1.php");
}
?>
<form name="test" action="" method="post">
<label for="fname">First Name:</label>
<input type="text" name="fname"/>
<label for="lname">Last Name:</label>
<input type="text" name="lname"/>
<input type="submit" name="submit" />
</form>
and form1.php contains following statement:
<?php
echo "Form Submitted!";
?>
On submition form will stay on the same page since action is null in form. In the if statement it will be redirected!
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.