0

I have two File , index.php and ClassProveContakt.php .I have to build a Form data on my ClassProveContakt.php , in index.php display the data and Webformular .Every time that write a data(Name, E-mail and Message) display it, but if I write a new data , the old date missing and change to my new data ....

My Question , how can I do that all data , old and new to remain on index.php.

ClassProveContakt.php Code:

<?php
header('Content-Type: text/html; Charset=utf-8');
mb_internal_encoding('UTF-8');
date_default_timezone_set('Europe/Paris');
error_reporting(-1);
class ClassProveContakt
{
 private $Name;
 private $Email;
 private $Message;
 function __construct()
 {
 $this->Name="";
 $this->Email="";
 $this->Message="";
 }
 function Form()
 {
 echo('<table>');
 echo('<label for="name">Name </label>');
 echo('<input type="text" name="Name" value="'.$this->Name.'">');
 echo('<label for="email"> Email </label>');
 echo('<input type="email" value="'.$this->Email.'" name="Email" ');
 echo('<tr>');
 echo('<td>');
 echo('<br>');
 echo('<label> Message: <br><textarea cols="45" rows="6" name="Message">'.$this->Message.'</textarea></label>');
 echo('<br><br>');
 echo('<input type="submit" name="post" value="POST COMMENT" id="comment">');
 echo('</td>');
 echo('</tr>');
 echo('</table>');
 }
 function PostOk()
 {
 if(empty($_POST['Name']) || 
 empty($_POST['Email']) || 
 empty($_POST['Message'])) 
 {
 echo "<br>" . "<b>" . "<h3>*** Please enter all required fields ***</h3>" . "</b>"; 
 $this->Name=$_POST["Name"];
 $this->Email=$_POST["Email"];
 $this->Message=$_POST["Message"];
 }
 else {
 $name = filter_input( INPUT_POST, 'Name', FILTER_SANITIZE_STRING);
 $email = filter_input(INPUT_POST, 'Email', FILTER_SANITIZE_STRING);
 $message = filter_input(INPUT_POST, 'Message', FILTER_SANITIZE_STRING);
 $datetime = date('m/d/Y h:i:s a', time());
 echo "<br>"
 . "<b>From: </b>" . htmlspecialchars( $name)
 . "<b> at: </b>" . htmlspecialchars( $datetime)
 . "<br><br>" . htmlspecialchars( $message)
 . "<br><hr>";
 } 
 }
} 
?>

index.php Code:

<?php
 include 'ClassProveContakt.php';
 header('Content-Type: text/html; Charset=utf-8');
 mb_internal_encoding('UTF-8');
 date_default_timezone_set('Europe/Paris');
 error_reporting(-1);
$ProveContackt=new ClassProveContakt();
?> 
<!DOCTYPE html>
<html lang="en_mx">
<head>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
 <form name="form" id="form" method="post" action="" >
<?php
 $ProveContackt->form();
 $ProveContackt->PostOk();
?>
</form>
</body>
</html>

screenshot how my Page to show https://i.imgur.com/xSstxcD.png

asked Oct 9, 2018 at 17:32
8
  • Store it somewhere. Session, text file, database. Commented Oct 9, 2018 at 17:34
  • i have not database.. Commented Oct 9, 2018 at 17:35
  • There's still session or plain file. Commented Oct 9, 2018 at 17:36
  • plain file.. , sorry about my english... , i have this two file on my computer , and the file index.php show with the browser firefox Commented Oct 9, 2018 at 17:43
  • Data between requests is not saved, you must save it manually. To a file, to a database, to a session. And load this data on next request. Commented Oct 9, 2018 at 17:47

2 Answers 2

0

So, as the comment I send you this solution.

To recover the data:

$json_get_data = file_get_contents('myfile.json');

To save the data:

file_put_contents('myfile.json', $save_data);

ClassProveContakt.php Code:

 <?php
header('Content-Type: text/html; Charset=utf-8');
mb_internal_encoding('UTF-8');
date_default_timezone_set('Europe/Paris');
error_reporting(-1);
class ClassProveContakt {
 private $Name;
 private $Email;
 private $Message;
 function __construct() {
 $this->Name="";
 $this->Email="";
 $this->Message="";
 }
 function Form() {
 echo('<table>');
 echo('<label for="name">Name </label>');
 echo('<input type="text" name="Name" value="'.$this->Name.'">');
 echo('<label for="email"> Email </label>');
 echo('<input type="email" value="'.$this->Email.'" name="Email" ');
 echo('<tr>');
 echo('<td>');
 echo('<br>');
 echo('<label> Message: <br><textarea cols="45" rows="6" name="Message">'.$this->Message.'</textarea></label>');
 echo('<br><br>');
 echo('<input type="submit" name="post" value="POST COMMENT" id="comment">');
 echo('</td>');
 echo('</tr>');
 echo('</table>');
 }
 function PostOk() {
 if(empty($_POST['Name']) || empty($_POST['Email']) || empty($_POST['Message'])) {
 echo "<br>" . "<b>" . "<h3>*** Please enter all required fields ***</h3>" . "</b>"; 
 $this->Name=$_POST["Name"];
 $this->Email=$_POST["Email"];
 $this->Message=$_POST["Message"];
 } else {
 $json_get_data = file_get_contents('myfile.json');
 $array_data = (array)json_decode($json_get_data);
 $name = filter_input( INPUT_POST, 'Name', FILTER_SANITIZE_STRING);
 $email = filter_input(INPUT_POST, 'Email', FILTER_SANITIZE_STRING);
 $message = filter_input(INPUT_POST, 'Message', FILTER_SANITIZE_STRING);
 $datetime = date('m/d/Y h:i:s a', time());
 $data = new stdClass();
 $data->name = $name;
 $data->email = $email;
 $data->message = $message;
 $data->datetime = $datetime;
 $array_data[] = $data;
 $save_data = json_encode($array_data);
 file_put_contents('myfile.json', $save_data);
 foreach ($array_data as $key => $value) {
 echo "<br>"
 . "<b>From: </b>" . htmlspecialchars( $value->name)
 . "<b> at: </b>" . htmlspecialchars( $value->datetime)
 . "<br><br>" . htmlspecialchars( $value->message)
 . "<br><hr>";
 }
 }
 }
} 
?>

And index.php Code keeps as same:

<?php
 include 'ClassProveContakt.php';
 header('Content-Type: text/html; Charset=utf-8');
 mb_internal_encoding('UTF-8');
 date_default_timezone_set('Europe/Paris');
 error_reporting(-1);
$ProveContackt=new ClassProveContakt();
?> 
<!DOCTYPE html>
<html lang="en_mx">
<head>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
 <form name="form" id="form" method="post" action="" >
<?php
 $ProveContackt->form();
 $ProveContackt->PostOk();
?>
</form>
</body>
</html>
answered Oct 9, 2018 at 20:41
Sign up to request clarification or add additional context in comments.

4 Comments

@ Hugo , thanks for your answer ! , sorry because now answer your but as i working doing not answer your before .... today working with you Code , it working , a question.. it is possible with only php Code to does it ?
It's possible in almost all languages, just try in the language to find about they libraries of "file system" and JSON management.
but i to means , it is possible to does ONLY with PHP Code without Json ?
Oh, sorry. you can use PHP coockies link please see the link to get more info about PHP coockies.
0

so , now i can does it ONLY mit php Code , WITHOUT Json...

@ Hugo , Thanks , you indicate me the way....

index.php stay identical , in ClassProveContakt.php i having to change to...

<?php
header('Content-Type: text/html; Charset=utf-8');
mb_internal_encoding('UTF-8');
date_default_timezone_set('Europe/Paris');
error_reporting(-1);
class ClassProveContakt {
 private $Name;
 private $Email;
 private $Message;
function __construct() {
 $this->Name="";
 $this->Email="";
 $this->Message="";
}
function Form() {
 echo('<table>');
 echo('<label for="name">Name </label>');
 echo('<input type="text" name="Name" value="'.$this->Name.'">');
 echo('<label for="email"> Email </label>');
 echo('<input type="email" value="'.$this->Email.'" name="Email" ');
 echo('<tr>');
 echo('<td>');
 echo('<br>');
 echo('<label> Message: <br><textarea cols="45" rows="6" name="Message">'.$this->Message.'</textarea></label>');
 echo('<br><br>');
 echo('<input type="submit" name="post" value="POST COMMENT" id="comment">');
 echo('</td>');
 echo('</tr>');
 echo('</table>');
}
function PostOk() {
 $file = "test.txt"; 
 $this->Name=$_POST["Name"];
 $this->Email=$_POST["Email"];
 $this->Message=$_POST["Message"]; 
 if(empty($_POST['Name']) || empty($_POST['Email']) || empty($_POST['Message'])) {
 echo "<br>" . "<b>" . "<h3>*** Please enter all required fields ***</h3>" . "</b>"; 
 } 
 else 
 {
 $name = filter_input( INPUT_POST, 'Name', FILTER_SANITIZE_STRING);
 $email = filter_input(INPUT_POST, 'Email', FILTER_SANITIZE_STRING);
 $message = filter_input(INPUT_POST, 'Message', FILTER_SANITIZE_STRING);
 $datetime = date('m/d/Y h:i:s a', time());
 $data = array("name" => $name, "email" => $email, "message" => $message, "datetime" => $datetime);
 $data = serialize($data);
 file_put_contents($file, $data . "\n", FILE_APPEND|LOCK_EX); 
 } 
 $messages = file($file);
 foreach ($messages as $value) {
 $data = unserialize($value);
 echo "<br>"
 . "<b>From: </b>" . htmlspecialchars( $data["name"])
 . "<b> at: </b>" . htmlspecialchars( $data["datetime"])
 . "<br><br>" . htmlspecialchars( $data["message"])
 . "<br><hr>";
 } 
 }
}
?>

My screeshot how to show now it

answered Oct 13, 2018 at 14:13

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.