[PHP] Come display data nella stessa pagina

mendia

Nuovo Utente
1 Ott 2018
8
1
3
Hanover
Ciao , sono un spagnolo che come non é trovato una soluzione alle mie domande in altri siti (spagnolo , inglese e tedesco) , spero di avere piu fortuna qua ... .

Per primo dire , che la mia conoscensa in php é molto semplice....

Cello due php File

contact.php dove cello il mio Code Form

PHP:
<?php

header('Content-Type: text/html; Charset=utf-8');

mb_internal_encoding('UTF-8');

date_default_timezone_set('Europe/Warsaw');

?>
Codice:
<!doctype html>
<html>
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />

  <title>Prove</title>

</head>
<body>

  <form  action="test.php" method="post" name="post">

        <label for="email">E-Mail</label>

                 <input type="email" id="email" name="email" />

                  <label for="name">Name</label>

                  <input id="name" name="name" />

      <br>

        <label> Message: <br><textarea cols="45" rows="6"  name="message"></textarea><br></label>

     <p>

        <button  type="submit" name="post"  value="POST COMMENT">POST  COMMENT</button>

     </p>

     <br>

  </form>

    <h2>Comments:</h2>

</body>

e Code.php , dove cello il Code che processa li data di contact.php .

PHP:
<?php
date_default_timezone_set('Europe/Warsaw');
?>

<!doctype html>
<html>
<head>
 <meta charset="utf-8"/>
</head>
<body>

<?php
include "contact.php";
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());

   echo "<br>"

      . "<b>From: </b>" . htmlspecialchars( $name     )
      . "<b> at: </b>" . htmlspecialchars( $datetime)
      . "<br><br>" . htmlspecialchars( $message  )
      . "<br><hr>";
}

?>

</body>
</html>

Il mio problema ... quando inserisco in contact.php , li datti ,Nome , e-mail e comenti loro sono display in contac.php , quando io voglio che vadano display anche in nello File test.txt .

Perche facio due File e non tutto in uno , per sicureza ....

spero che il mio italo-spagnolo sea compresive per voi... e molto gracie per il vostro aiuto !
 
Ultima modifica di un moderatore:
I dati li visualizzi nel file test.php perchè rispondo con un echo...
Dovresti fare restituire qualcosa di diverso, intanto il controllo 'empty' puoi farlo direttamente nell'html di contact.php aggiungendo l'attributo required agli input.

mentre in test.php puoi fare qualcosa del genere:
PHP:
header_remove();
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: X-Requested-With, content-type, access-control-allow-origin, access-control-allow-methods, access-control-allow-headers");

$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());

//...

header("Content-Type: application/json;", true, 200);
echo json_encode(array("status"=>200,"message"=>"OK","name"=>$name,"email"=>$email,"message"=>$message));

Di fatto ottieni una risposta simile a quella di una API (io utilizzo questo sistema in una applicazione in Angular 6)
 
  • Like
Reactions: mendia
Gracie per il tuo aiuto ! , in Giugno o iniziato un Curso Online di Html/Css , da un Mese e Mezzo sono imparando PHP e da un Mese cerco una soluzione per il mio problema .... e lavoro puliendo Piatti... , per fortuna non lavoro tutti gli giorni.... , te dico tutto questo perche avrei bisogno die "alcuni" giorni per studiare Json e Ajax , ti ringracio molto perche tu mi ai mostrato la Strada , io adesso devo camminarla...;)
 
  • Like
Reactions: Rikk73
scusarme per il ritardo della mia risposta... nelle ultimi messi ce avuto molto stress... ma meglio tardi che mai ;)

Alla fine in un Forum tedesco me hanno datto la soluzione per il mio problema , cioe , recivere il email , name e messagi nello due File , contact.php e test.txt

Nello File contact.php ho solo scrito il name dell File dove ho solo PHP Code , code.php .
PHP:
<?php

  include 'code.php';

?>

e cancellato il Form .

Nello code.php ho tutto anche il Form , alla fine una delle soluzione era fare con Class .

PHP:
<?php
class ClassProveContakt3
{

      private $Name;
      private $Email;
      private $Message;
      private $PostOK;
      private $DateTime;
      private $items;

    function __construct()
    {

         $this -> DateTime = date('m/d/Y h:i:s a');
         $this -> items = ['Name', 'Email', 'Message'];
       
         $flag = true;
          foreach ( $this -> items as $key ) {
                      if ( empty ( $_POST[$key] ) )  {
                        $flag = false; 
                      } else { 
                        $this -> $key = trim( filter_var( $_POST[$key], FILTER_SANITIZE_STRING ) );
                       } 
          }     
         $this -> PostOk = $flag;
    }

 

 
    function ShowForm()
    {
?>
      <form method="POST">
        <label for="name"><b>Name * </b></label>
        <input type="text" id="name" name="Name" value="">
        <label for="email"><b>E-mail * </b></label>
        <input type="email" id="email" name="Email" value="">
        <br><br>
        <label><b> Message * </b><br>
           <textarea cols="45" rows="6" name="Message"></textarea>   
        </label>
        <br><br>
        <input  type="submit" name="post" value="POST COMMENT" id="comment">
      </form>
<?php
    }
 
 
     
     
      function PostOkT()
      {

   
           
     

               if ($this -> PostOK)
               {
                 return;
               
               }   
                   if (empty($this->Name) ||  empty($this->Email) || empty($this->Message))
                   {
                 
                       echo "<br>" . "<b>" . "<h3>*** Please enter all required fields ***</h3>" . "</b>";
             
               
                   }
                   else
                   {
                 
                     
                   
                     
                       $file = "test.txt";
                     
                     
                     
                       $datetime = date('m/d/Y h:i:s a', time());
                       $data = array("name" => $this->Name, "email" => $this->Email, "message" => $this->Message, "datetime" => $datetime);
                     
                     
                          $data = json_encode($data);
                         file_put_contents($file, $data . "\n", FILE_APPEND|LOCK_EX);
                     
                     
                       $messages = file($file);
                   
                         foreach ($messages as $value) {
                           $data = json_decode($value, true);
                                   
     
                               
                                  echo "<br>"

                                      . "<b>From: </b>" . htmlspecialchars( $data["name"])
                                      . "<b> at: </b>" . htmlspecialchars( $data["datetime"])
                                      #. "<br><br>" . htmlspecialchars( $data["email"])                                   
                                      . "<br><br>" . htmlspecialchars( $data["message"])
                                      . "<br><hr>";
     
                       }
             
                    }

                 

     
       }

}     
   
         
$Newobject = new ClassProveContakt3();
$Newobject -> ShowForm();
$Newobject ->  PostOkT(); 
 

?>

Adesso Display in gli due File , in contact.php e test.txt :)
 
Ultima modifica di un moderatore:
@mendia
Quando posti codice PHP usa il tag
php.png
non il tag
code.gif

Mi raccomando!!
Grazie
 

Discussioni simili