HTML PHP form not working [duplicate] - php

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 5 years ago.
I have made a form, which adds some lines inside another file, called data.php.
Here is the code:
<?php
if (isset($_POST['post'])){
// GET EMAIL
$post_text = $_POST["post_text"];
if($post_text == '') $error = "Post content is empty";
$filename = getcwd() . "data.php";
$line_i_am_looking_for = 1;
$lines = file( $filename , FILE_IGNORE_NEW_LINES );
$lines[$line_i_am_looking_for] = $post_text;
file_put_contents( $filename , implode( "\n", $lines ) );
?>
<html>
<head>
</header
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<?php
if ($error) {
?>
<script type="text/javascript">
// close the errordiv in secs
window.setTimeout("close();", 4000);
function close(){
document.getElementById("errordiv").style.display="none";
}
</script>
<?php
}
?>
<div id="errordiv" style="display:block; color:#FF3535">
<b><?php if($error) echo $error; ?></b>
</div>
Email:
<input name="post_text" size="30" type="text">
<input class="button" value="Submit" type="submit" name="post">
</form>
</body>
</html>
When I open index.php, enter some words in the text box and submit the form, I want to write the entered text in the second line of data.php file.
But it's not working.

I was looking into the code and I found the answer my self.
There was a missing } in my code. The edited code is here:
<?php
if (isset($_POST['post'])){
// GET EMAIL
$post_text = $_POST["post_text"];
if($post_text == '') $error = "Post content is empty";
$filename = getcwd() . "/data.php";
$line_i_am_looking_for = 1;
$lines = file( $filename , FILE_IGNORE_NEW_LINES );
$lines[$line_i_am_looking_for] = $post_text;
file_put_contents( $filename , implode( "\n", $lines ) );
}
?>
<html>
<head>
</header
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<?php
if($error){
?>
<script type="text/javascript">
// close the errordiv in secs
window.setTimeout("close();", 4000);
function close(){
document.getElementById("errordiv").style.display="none";
}
</script>
<?php
}
?>
<div id="errordiv" style="display:block; color:#FF3535"><b><?php if($error) echo $error; ?></b></div>
Email:
<input name="post_text" size="30" type="text">
<input class="button" value="Submit" type="submit" name="post">
</form>
</body>
</html>

Related

PHP - I can't save content from input

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.

I can not pass a PHP SESSION and write it to text

I'm trying to pass $_POST info as $_SESSION but when it doesn't work, I don't know what is wrong in my code.
<?php
session_start();
$_SESSION['nombre'] = $_POST['nombre'];
$_SESSION['edad'] = $_POST['edad'];
?>
<html>
<form action="accion.php" method="post">
<p>Name: <input type="text" name="nombre" /></p>
<p>Age: <input type="text" name="edad" /></p>
<p><input type="submit" /></p>
</form>
</html>
Second file
<?php
session_start();
if(isset($_SESSION['nombre']) && isset($_SESSION['edad'])) {
$data = $_SESSION['nombre'] . '-' . $_POST['edad'] . "\n";
$ret = file_put_contents('mydata.txt', $data, FILE_APPEND | LOCK_EX);
if($ret === false) {
die('There was an error writing this file');
}
else {
echo "$ret bytes written to file";
}
}
else {
die('no post data to process');
}
?>
The following code will solve your problem:
File1.php
<html>
<form action="File2.php" method="post">
<p>Name: <input type="text" name="nombre" /></p>
<p>Age: <input type="text" name="edad" /></p>
<p><input type="submit" /></p>
</form>
</html>
<?php
echo $_GET['msg'];
?>
File2.php
<?php
session_start();
if(isset($_POST['nombre']) && isset($_POST['edad'])) {
$_SESSION['nombre'] = $_POST['nombre'];
$_SESSION['edad'] = $_POST['edad'];
$data = $_SESSION['nombre'] . '-' . $_POST['edad'] . "\n";
$ret = file_put_contents('mydata.txt', $data, FILE_APPEND | LOCK_EX);
if($ret === false) {
header("Location:File1.php?msg=There was an error writing this file");
}
else {
header("Location:File1.php?msg=$ret bytes written to file";
}
}
else {
header("Location:File1.php?msg=no post data to process");
}
?>
When the form is submitted, it is submitted to File2.php
So, your first session being set and taking values as POST from it in File1.php won't work.
The best option is to set the session in the second file and then give back a message to the File1.php as shown above.
Check the code bellow. It is better to start session once at the begining of the file and check if post variable contains value to set in the session.
<?php
session_start();
if(isset($_POST['nombre']) && isset($_POST['edad'])) {
$_SESSION['nombre'] = $_POST['nombre'];
$_SESSION['edad'] = $_POST['edad'];
}
?>
<html>
<form action="accion.php" method="post">
<p>Name: <input type="text" name="nombre" /></p>
<p>Age: <input type="text" name="edad" /></p>
<p><input type="submit" /></p>
</form>
</html>
<?php
if(isset($_SESSION['nombre']) && isset($_SESSION['edad'])) {
$data = $_SESSION['nombre'] . '-' . $_POST['edad'] . "\n";
$ret = file_put_contents('mydata.txt', $data, FILE_APPEND | LOCK_EX);
if($ret === false) {
die('There was an error writing this file');
}
else {
echo "$ret bytes written to file";
}
}
else {
die('no post data to process');
}
?>
If you want to send the form in the same page instead using action="action.php" use action=""

PHP- Save input data into CSV file and display the entire file in another page

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>

Refreshing Page With Php

I'm working on a php code where I'm echoing out form values and a file onto the page. I have that working fine but I want to implement a start over button when pressed will display the page again without the echoes. Any suggestions or better ways to do this?
<html>
<head>
</head>
<body>
<div class="page">
<h1>File Loader</h1>
<form method="post">
<label>Name:</label><input type="text" name="name"></br>
<label>Date:</label><input type="text" name="date"></br>
<input type="submit" name="submit" value="Submit">
<input type="button" name="startOver" value="Start Over" onclick="phpfile.php"></br>
</form>
</div>
<?php
if ($_POST['submit']) {
$name = $_POST['name'];
$date = $_POST['date'];
echo "Name: $name</br></br>";
echo "Date: $date</br>";
$file_handle = fopen("file.csv", "r");
while (!feof($file_handle) ) {
$fields_of_text = fgetcsv($file_handle, 2024);
$f = $fields_of_text;
$format = "%s %s %s %s %s %s %s %s %s %s %s";
echo "<br>" . sprintf($format, $f[0], $f[1], $f[2], $f[3], $f[4], $f[5], $f[6], $f[7], $f[8], $f[9], $f[10]);
echo "<br>";
echo "<br>";
echo "<br>";
}
fclose($file_handle);
} else {
header("Location: phpfile.php");
}
?>
</div>
</body>
</html>
You can easily use query string/post params.
You can use $_GET like below or you can use a hidden input and change that to 1 or 2 with the onclick and javascript if you need to use post.
</head>
<body>
<div class="page">
<h1>File Loader</h1>
<form method="post">
<label>Name:</label><input type="text" name="name"></br>
<label>Date:</label><input type="text" name="date"></br>
<input type="button" value="Submit" onclick="window.location.href='phpfile.php?SubmitType=1';">
<input type="button" value="Start Over" onclick="window.location.href='phpfile.php?SubmitType=2';">
</br>
</form>
</div>
<?php
if ($_GET['SubmitType'] == '1') {
$name = $_POST['name'];
$date = $_POST['date'];
echo "Name: $name</br></br>";
echo "Date: $date</br>";
$file_handle = fopen("file.csv", "r");
while (!feof($file_handle) ) {
$fields_of_text = fgetcsv($file_handle, 2024);
$f = $fields_of_text;
$format = "%s %s %s %s %s %s %s %s %s %s %s";
echo "<br>" . sprintf($format, $f[0], $f[1], $f[2], $f[3], $f[4], $f[5], $f[6], $f[7], $f[8], $f[9], $f[10]);
echo "<br>";
echo "<br>";
echo "<br>";
}
fclose($file_handle);
}
else {
header("Location: phpfile.php");
exit();
}
?>
</div>
</body>
</html>

php Post txt file content to textarea on another page

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><?
}
}

Categories