I am writing an app to delete and modify files in a directory. I deleted files successfully. And now I want to modify and update content to the same file using textarea on another page.
<form enctype="multipart/form-data" method="post">
<div class="span7">
<table>
<thead>
<tr>
<th> List of files </th>
<tr>
</thead>
<?php
$files = glob("UploadFile/"."*");
foreach($files as $txt)
{
if(mime_content_type($txt)=="text/plain")
{
$txtname = basename($txt);
echo "<tr><td><input type='radio' name='txt' value='$txtname'/> ".$txtname."</td></tr>";
}
}
?>
</table>
</div>
<div class="span8">
<button type="submit" name="submit" value="Edit">Edit</button>
<button type="submit" name="submit" value="Delete">Delete</button>
</div>
<?php
global $txtname;
$val = $_POST["submit"];
$txtname = $_POST["txt"];
if($val=="Delete")
unlink("UploadFile/".$txtname);
else if($val=="Edit")
{
$content=file_get_contents("UploadFile/".$txtname);
header('Location:/edit.php');
/* send content to this 'edit.php' page */
}
?>
</form>
And the edit.php page, i simply checked if the code is working. It works fine.
<?php
if($_POST['append'])
{
$file_open = fopen("UploadFile/file.txt","a+");
fwrite($file_open, $_POST['append']);
fclose($file_open);
}
?>
<form enctype="multipart/form-data" action="<?=$PHP_SELF?>" method="post">
<textarea name="append" value="">
<?php
echo $content;
$datalines = file ("UploadFile/file.txt");
foreach ($datalines as $zz)
{
echo $zz;
}
?>
</textarea>
<button type="submit" name="submit" value="save"> Save </button>
How do I get the contents of the file to textarea for modifying it.Update: I want to save changes to the same file. Thank you
<?php
if(isset($_POST['text']))
{
file_put_contents("file.txt",$_POST['text']);
header("Location: ".$_SERVER['PHP_SELF']);
exit;
}
$text = file_get_contents("file.txt");
?>
<form method="post">
<textarea name="text"><?=$text?></textarea>
<input type="submit">
</form>
with listing
if(isset($_POST['text']))
{
file_put_contents("UploadFile/".basename($_POST['file']),$_POST['text']);
header("Location: ".$_SERVER['PHP_SELF']);
exit;
}
if(isset($_GET['file']))
{
$text = file_get_contents("UploadFile/".basename($_GET['file']));
?>
<form method="post">
<input type="hidden" name="file" value="<?=urlencode($_GET['file'])?>">
<textarea name="text"><?=$text?></textarea>
<input type="submit">
</form>
<?
} else {
$files = glob("UploadFile/*");
foreach ($files as $f)
{
$f=basename($f);
?><?=htmlspecialchars($f)?><br><?
}
}
Related
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.
When I click on "Comment", it is not opening the new file $cname.txt hence not able to insert values in file and therefore nothing is posted. I want that cname and comment to be posted when the user clicks submit? Here is my code:
<?php
$name="";
/*print_r($_POST);*/
if(isset($_POST['submit'])) {
global $name;
$name=$_POST['name'];
$query=$_POST['query'];
$my_query="My query";
/* $files=fopen("$name.txt","w");
fclose($files); */
$po="$name <br> $my_query:$query \n";
$myfile=fopen("posts.txt","a+");
fwrite($myfile,$po);
fclose($myfile);
}
$mypost=fopen("posts.txt","r");
while($line = fgets($mypost)) {
echo '<form class="w3-container w3-card-4 w3-light-grey w3-text-blue w3-margin">
<div class="w3-row w3-section">
<div class="w3-col" style="width:50px"><i class="fa fa-question-circle-o" style="font-size:30px"></i></div>
<div class="w3-rest">
<p class="w3-input w3-border"><i class="fa fa-user-circle-o"></i>'.$line; echo '</p>
</div>
</div>
<p>comments</p>
<form method="post" action="' .htmlspecialchars($_SERVER['PHP_SELF']); echo '">
Name:<textarea name="cname" rows="1" cols="4"></textarea><textarea name="comment" rows="1" cols="60"></textarea><br>
<input type="submit" name="submits" value="Comment" class="w3-button w3-section w3-blue w3-ripple">
</form>
' ; }
$cname="";
if(isset($_POST['submits'])) {
global $cname;
$cname=$_POST['cname'];
$comment=$_POST['comment'];
$pos="$cname <br> $comment \n";
$myfiles=fopen("$cname.txt","a+");
fwrite($myfiles,$pos);
fclose($myfiles);
$myposts=fopen("$cname.txt","r");
while($lines = fgets($myposts)) {
echo $lines;
}
}'
</form>'
;
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>">
Here is the code. I want it this way --
Form submission --> page2.php --> redirect --> page1.php (Here is the message. Pop-up or whatever)
page1.php
<form action="page2.php" method="post" enctype="multipart/form-data" class="form-inline subscribe-form">
<input type="name" name="name" placeholder="Jack">
</div>
<button type="submit" name="sub" value="sub" >Submit</button>
</form>
page2.php
<?php
//include necessary
if(isset($_POST['sub'])) {
$nameget = mysqli_real_escape_string($dbconnect, $_POST['name']);
$sqlentry = .....bla bla......//insert into DB
}
$getsql = mysqli_query($dbconnect, $);
if($getsql){
mysql_close($dbconnect);
header('location:page1.php');
}
?>
Where you have:
header('location:page1.php');
append a variable on the location, like:
header('location:page1.php?status=success');
And on page1.php, do something like:
if( $_GET['status'] == 'success'):
echo 'feedback message goes here';
endif;
This way your flash message will not show up again and again after refresh.
<?php session_start();
if(isset($_SESSION['msg']) && $_SESSION['msg'] != ''){
echo $_SESSION['msg'];
unset($_SESSION['msg']);
}
?>
<form action="page2.php" method="post"
enctype="multipart/form-data" class="form-inline subscribe-form">
<input type="name" name="name" placeholder="Jack">
</div>
<button type="submit" name="sub" value="sub" >Submit</button>
</form>
And
<?php
session_start();
//include necessary
if(isset($_POST['sub'])) {
$nameget = mysqli_real_escape_string($dbconnect, $_POST['name']);
$sqlentry = .....bla bla......//insert into DB
}
$getsql = mysqli_query($dbconnect, $);
if($getsql){
mysql_close($dbconnect);
$_SESSIOM['msg'] = 'Value Inserted or whatever';
header('location:page1.php');
}
?>
I have this piece of code below, supposing it will display no request when no data in database and it will display the request with an accept and reject button when they is data in database. How should i write my code in order to make the accept and reject button perform some action?
<?php
$travelRequest = $user->userTravelRequest($userid);
if(!$travelRequest){
echo '<div class="requestbox">
<p> You have no request from others at the moment. </p>
</div>';
}
else {
foreach($travelRequest as $request){
echo '<div class= "requestbox">
<p>'. $request->trip_name.'</p>
<p>Organised by '.$request->username.'</p>';
?>
<form method="POST">
<div class = "AR-btn">
<input type="button" name="accept" value="Accept"/>
<input type="button" name="reject" value="Reject"/>
<p></p>
</div>
</form>
</div>
<?php
}
}
?>
Use type="submit" instead of type="button", and give them the same name. Then they'll submit the form, and the script can test which button was used from the value of that parameter.
<input type="submit" name="action" value="Accept"/>
<input type="submit" name="action" value="Reject"/>
You then test this with:
if (isset($_POST['action'])) {
if ($_POST['action'] == 'Accept') {
// Add to database
} elseif ($_POST['action'] == 'Reject') {
// Delete from database
}
}
Try this code :-
<?php
if (isset($_POST['accept']) && $_POST['accept'] == 'Accept' ) {
// do what you want
die("Accept is clicked");
} else if (isset($_POST['reject']) && $_POST['reject'] == 'Reject' ) {
// do what you want
die("Reject is clicked");
}
?>
<?php
$travelRequest = $user->userTravelRequest($userid);
if(!$travelRequest){
echo '<div class="requestbox">
<p> You have no request from others at the moment. </p>
</div>';
} else {
foreach($travelRequest as $request){
echo '<div class= "requestbox">
<p>'. $request->trip_name.'</p>
<p>Organised by '.$request->username.'</p>';
?>
<form method="POST">
<div class = "AR-btn">
<input type="submit" name="accept" value="Accept"/>
<input type="submit" name="reject" value="Reject"/>
<p></p>
</div>
</form>
</div>
<?php
}
}
?>
Just try this logic..may help you
$query = "select data from database";
.
.
.
$data = $row['data'];
if($data){ // reject button when they is data in database
echo '<input type="submit" name="action" value="Reject"/>';
}
else{ // viceversa
echo '<input type="submit" name="action" value="Accept"/>';
}
I have following code:
<html>
<head>
<meta charset="UTF-8"/>
</head>
<body>
<!--See siin all tekstiväli-->
<H3>Minu küsitlused </H3>
<hr>
<br>
<br>
<br>
<ol>
<?php
include_once 'init/init.funcs.php';
$result = mysql_query('SELECT * from katse_kysimustik_pealkiri');
while($row = mysql_fetch_assoc($result)) {
$titles[] = $row['pealkiri'];
}
foreach($titles as $title) {?>
<li>
<?php echo $title ?>
<form action='Minu_kysitlused_1.php' method="post">
<input type="submit" name = "saada" value="saada"/>
<input type="button" value="tulemused"/>
<input type="button" value="lõpeta ennetähtaegselt"/>
<input type="button" value="X"/>
</li>
</form>
<?php
}
?>
</ol>
</body>
</html>
<?php
if(isset($_POST['saada'])){
header("Location:http://localhost/Praks/saada.html");
}
?>
Right now there is button "saada" for each title in database. When I click "saada", page saada.html will appear.
This is saada.html
<!doctype html>
<html>
<head>
<meta charset="UTF-8"/>
<title>Saada</title>
</head>
<form action="test1.php" method="POST">
<body>
<header>Küsitluse pealkiri</header>
<br>
Lisa sõnumile pealkiri:
<br>
<textarea rows='1' name='pealkiri'>Küsitluse algne pealkiri</textarea>
<br>
Lisa Adressaadid: <br>
<textarea rows='1' name='email'></textarea>
<br>
Lisa sõnum:
<br>
<textarea rows='4' name='tekst'></textarea>
<br><footer>
<INPUT TYPE="submit" VALUE="Saada" NAME="Saada">
</form>
<FORM METHOD="LINK" ACTION="Minu_kysitlused.html">
<INPUT TYPE="submit" VALUE="Loobu">
</footer>
</body>
</html>
My question is how could I make it work so value of $title appears in saada.html. The value of this $title which is next to 'saada' button I clicked. I need this value to be in this line instead of Küsitluse algne pealkiri:
<textarea rows='1' name='pealkiri'>Küsitluse algne pealkiri</textarea>
You can not have a value through PHP in HTML type page....change page extension to .php
then pass your $title in input hidden field in your form
like this
foreach($titles as $title) {?>
<li>
<?php echo $title ?>
<form action='Minu_kysitlused_1.php' method="post">
<input type="submit" name = "saada" value="saada"/>
<input type="button" value="tulemused"/>
<input type="button" value="lõpeta ennetähtaegselt"/>
<input type="button" value="X"/>
<input type="hidden" name="title" value="<?php echo $title;?>"/>
</li>
</form>
<?php
}
?>
Now set the cookie
<?php
if(isset($_POST['saada']))
{
if(isset($_REQUEST['title']))
{
$title = $_REQUEST['title'];
setcookie("page_title", $title, time()+3600);
}
header("Location:http://localhost/Praks/saada.html");
}
?>
Now on Your saada.html you can use js to retrieve cookie like this
JS Function Reference
var title = getCookie("page_title"); // retrieve cookie
document.getElementById('page_title').value = title; // put title into textaread
function getCookie(name) {
var value = "; " + document.cookie;
var parts = value.split("; " + name + "=");
if (parts.length == 2) return parts.pop().split(";").shift();
}
<textarea rows='1' name='pealkiri' id="page_title" >Küsitluse algne pealkiri</textarea>