• User

    Creare ANTEPRIMA articolo

    Un saluto a tutti.
    Mi occorerebbe sapere se e come sia possibile integrare una visualizzazione di anteprima (in pagina web) dei dati da inviare tramite form (nel mio caso un pannello di invio articoli, che non utilizza database mysql).

    Questo è lo script del mio pannello (Article Manager):

    
    <?
    include ("template.inc");
    include ("config.php");
    
    $subject = $_POST[subject];
    $summary = $_POST[summary];
    $passwd = $_POST[passwd];
    $date = $_POST[date];
    $body = $_POST[body];
    $article_id = $_POST[article_id];
    #foreach($GLOBALS as $a => $b){ print "<li>$a => $b";}
    $summary_template = "t_summary.html";
    $article_template = "t_article.html";
    $max_summary = 5;
    function summary_page ($subject, $date, $summary, $article_id)
    {
    global $summary_template;
    $t = new Template();
    $t->set_file("SummaryPage", $summary_template);
    $article_url = "article_".$article_id.".html";
    $date = nl2br($date);
    $summary = nl2br($summary); 
    $t->set_var( array(
    "subject" => $subject,
    "date" => $date,
    "summary" => $summary,
    "article_url" => $article_url
    ));
    $t->parse("Summary", "SummaryPage");
    return $t->get_var("Summary");
    }
    function main_page ($subject, $date, $summary, $article_id, $body)
    {
    global $article_template;
    $t = new Template();
    $t->set_file("ArticlePage", $article_template);
    $article_url = "article_".$article_id.".html";
    $date = nl2br($date);
    $summary = nl2br($summary);
    $body = nl2br($body);
    $t->set_var( array(
    "subject" => $subject,
    "date" => $date,
    "summary" => $summary,
    "body" => $body,
    "article_url" => $article_url
    ));
    $t->parse("Article", "ArticlePage");
    return $t->get_var("Article"); 
    }
    function add_article($filename, $news)
    {
    if(file_exists($filename)){
    $fh = fopen($filename, "r");
    $old_news = fread($fh, filesize($filename));
    fclose($fh); 
    }
    /* TODO: Multipage articles
    preg_match_all("<!--ARTICLE PAGE=(\d*)-->", $old_news, $matches;
    
    if( count($matches[0]) >= $max_summary){
    $oldfilename = $filename.($matches[0][0]+1);
    } 
    */
    $fh = fopen($filename, "w");
    $news = stripslashes($news);
    fwrite($fh, "\n<!--ARTICLE-->\n$news $old_news");
    fclose($fh);
    }
    if(strcmp($subject, "")){ 
    if(!(strcmp($passwd, $password))){ 
    add_article("article_summary.html", summary_page($subject, $date, $summary, $article_id));
    add_article("article_$article_id.html", main_page($subject, $date, $summary, $article_id, $body));
    echo "<p> <a href=article_$article_id.html>Article</a> has been added! <p>";
    }else{
    echo "<p><b> Password is wrong! </b>";
    }
    }
    ?>
    
    

    E questo il form da cui inviare i dati:

    
    <form action=news.php method=post>
    <table border=0>
    <tr> <td> (Password): </td><td> <input type=password name=passwd size=20> </td></tr>
    <tr> <td> Subject: </td><td> <input type=text name=subject size=50> </td></tr>
    <tr> <td> Article ID: </td><td> <input type=text name=article_id value=<? echo date("Y_m_j_is"); ?> size=30> </td></tr>
    <tr> <td> Date/Author/etc: </td><td> <textarea name=date rows=2 cols=50 wrap=soft><? echo date("M j, Y\n"); ?>Author: </textarea> </td></tr>
    <tr> <td> Summary: </td><td> <textarea name=summary rows=5 cols=50 wrap=soft></textarea> </td></tr>
    <tr> <td> Body: </td><td> <textarea name=body rows=15 cols=50></textarea> </td></tr>
    </table>
    <input type=submit name=submit value=Add>
    </form>
    
    

    Il tutto è piuttosto semplice..ma non riesco a trovare il modo di inserire un'anteprima web dell'articolo da postare..
    Chiedo il vostro aiuto :fagiano:


  • User Attivo

    Ciao Darkel, Io farei una modifica nella pagina con il form, aggiungerei un altro bottone con preview.
    <input type=preview name=submit value=Preview>

    Dopodiche' modifichi la pagina news.php, mettendoci un codice simile a quello che riceve i dati, ma che si esegue quando value=Preview, e che non fa altro che stampare il riepilogo, e sotto mette i bottoni, modifica ancora e submit...
    Se hai problemi, potresti postare la pagina news.php per intero? Magari riesco a capire meglio come è impostato il tutto.


  • User

    Grazie per l'attenzione Jantima ;)..Il Form è integrato nella pagina *news.php *..

    Questo è il codice per intero della pagina..

     
    <?
            include ("template.inc");
            include ("config.php");
     $summary_template = "t_summary.html";
     $article_template = "t_article.html";
     $max_summary = 5;
     function summary_page ($subject, $date, $summary, $article_id)
     {
      global $summary_template;
             $t = new Template();
             $t->set_file("SummaryPage", $summary_template);
      $article_url = "article_".$article_id.".html";
      $date = nl2br($date);
      $summary =  nl2br($summary);  
      $t->set_var( array(
        "subject" => $subject,
        "date"    => $date,
        "summary" => $summary,
        "article_url" => $article_url
        ));
      $t->parse("Summary", "SummaryPage");
      return $t->get_var("Summary");
     }
     function main_page ($subject, $date, $summary, $article_id, $body)
     {
      global $article_template;
                    $t = new Template();
                    $t->set_file("ArticlePage", $article_template);
                    $article_url = "article_".$article_id.".html";
                    $date = nl2br($date);
                    $summary =  nl2br($summary);
                    $body =  nl2br($body);
                    $t->set_var( array(
                                    "subject" => $subject,
                                    "date"    => $date,
                                    "summary" => $summary,
                                    "body" => $body,
                                    "article_url" => $article_url
                                    ));
                    $t->parse("Article", "ArticlePage");
                    return $t->get_var("Article"); 
     }
     function add_article($filename, $news)
     {
      if(file_exists($filename)){
       $fh = fopen($filename, "r");
       $old_news = fread($fh, filesize($filename));
       fclose($fh); 
      }
      /* TODO: Multipage articles
       preg_match_all("<!--ARTICLE PAGE=(\d*)-->", $old_news, $matches;
     
       if( count($matches[0]) >= $max_summary){
        $oldfilename = $filename.($matches[0][0]+1);
       } 
      */
      $fh = fopen($filename, "w");
      $news = stripslashes($news);
      fwrite($fh, "\n<!--ARTICLE-->\n$news $old_news");
      fclose($fh);
     }
    ?>
    <?
     if(strcmp($subject, "")){ 
      if(!(strcmp($passwd, $password))){ 
       add_article("article_summary.html", summary_page($subject, $date, $summary, $article_id));
       add_article("article_$article_id.html", main_page($subject, $date, $summary, $article_id, $body));
       echo "<p> Article has been added! <p>";
      }else{
       echo "<p><b> Password is wrong! </b>";
      }
     }
    ?>
     
    <form action=news.php method=post>
    <table border=0>
    <tr> <td> (Password): </td><td> <input type=text name=passwd size=30> </td></tr>
    <tr> <td> Subject: </td><td> <input type=text name=subject size=30> </td></tr>
    <tr> <td> Article ID: </td><td> <input type=text name=article_id value=<? echo date("Y_m_j_is"); ?> size=30> </td></tr>
    <tr> <td> Date/Author/etc: </td><td> <textarea name=date rows=2 cols=30 wrap=soft><? echo date("M j, Y\n"); ?>Author: </textarea> </td></tr>
    <tr> <td> Summary: </td><td> <textarea name=summary rows=5 cols=30 wrap=soft></textarea> </td></tr>
    <tr> <td> Body: </td><td> <textarea name=body rows=15 cols=30></textarea> </td></tr>
    </table>
    <input type=submit name=submit value=Add>
    </form>
    
    

    E questo quello della pagina dove verrà 'stampato' l'articolo..

    <p>
    <b>{subject}</b>
    <br>
    <font size=1>{date}</font>
    <p>
    {body}
    <p>
    

  • User

    Dopodiche' modifichi la pagina news.php, mettendoci un codice simile a quello che riceve i dati, ma che si esegue quando value=Preview

    come realizzo questo codice?..
    grazie:)


  • User

    basta visualizzare l'articolo come lo visualizzeresti quando lo prendi dal db (anche se nel tuo caso non hai un db), nel senso, come se dovessi pescare i dati da qualche parte..
    in questo caso i dati li prendi dalla form con $_POST[]

    per farlo avviare basta fare

    [php]

    if (isset($_POST['preview']) {

    anteprima();

    } else {

    // codice normale della form di inserimento

    }

    [/php]molto semplicemente è così..


  • User

    Grazie per la risposta.
    A dirla tutta, però, da quel che ho capito, questo codice comporterebbe la modifica della maggior parte dello script originale. Ho già provato ad integrarlo, tra l'altro, ma senza risultati positivi.
    Visto che non sono pratico (per nulla :(), vorrei sapere se esiste un codice da integrare direttamente al mio script.


  • User Attivo

    Ciao, ho provato lo script con una semplice anteprima.
    Questo il codice finale
    [php]
    <?
    if (isset($_POST['preview'])) {
    //anteprima();
    echo 'Preview articolo:<br>';
    echo 'Subject: '.$subject .'<br>';
    echo 'Date: '.$date. '<br>';
    echo 'Summary: '.$summary. '<br>';
    echo 'Article ID: '.$article_id. '<br>';
    echo 'Body: '.$body. '<br>';
    ?>
    <form action=news.php method=post>
    <input type=hidden name=passwd size=30 value=<? echo $passwd ?>>
    <input type=hidden name=subject size=30 value=<? echo $subject ?>>
    <input type=hidden name=article_id value=<? echo $article_id ?>>
    <input type=hidden name=date rows=2 cols=30 wrap=soft value=<? echo $date ?>>
    <input type=hidden name=summary rows=5 cols=30 wrap=soft value=<? echo $summary ?>>
    <input type=hidden name=body rows=15 cols=30 value=<? echo $body ?>>
    <input type=button name=back value=back onclick="history.go(-1);return false;">
    <input type=submit name=submit value=Add >
    </form>
    <?php
    } else {

    // codice normale della form di inserimento
    include ("template.inc");
    include ("config.php");
    $summary_template = "t_summary.html";
    $article_template = "t_article.html";
    $max_summary = 5;

    function summary_page ($subject, $date, $summary, $article_id)
    {
    global $summary_template;
    $t = new Template();
    $t->set_file("SummaryPage", $summary_template);
    $article_url = "article_".$article_id.".html";
    $date = nl2br($date);
    $summary = nl2br($summary);
    $t->set_var( array(
    "subject" => $subject,
    "date" => $date,
    "summary" => $summary,
    "article_url" => $article_url
    ));
    $t->parse("Summary", "SummaryPage");
    return $t->get_var("Summary");
    }
    function main_page ($subject, $date, $summary, $article_id, $body)
    {
    global $article_template;
    $t = new Template();
    $t->set_file("ArticlePage", $article_template);
    $article_url = "article_".$article_id.".html";
    $date = nl2br($date);
    $summary = nl2br($summary);
    $body = nl2br($body);
    $t->set_var( array(
    "subject" => $subject,
    "date" => $date,
    "summary" => $summary,
    "body" => $body,
    "article_url" => $article_url
    ));
    $t->parse("Article", "ArticlePage");
    return $t->get_var("Article");
    }
    function add_article($filename, $news)
    {
    if(file_exists($filename)){
    $fh = fopen($filename, "r");
    $old_news = fread($fh, filesize($filename));
    fclose($fh);
    }
    /* TODO: Multipage articles
    preg_match_all("<!--ARTICLE PAGE=(\d*)-->", $old_news, $matches;

    if( count($matches[0]) >= $max_summary){
    $oldfilename = $filename.($matches[0][0]+1);
    }
    */
    $fh = fopen($filename, "w");
    $news = stripslashes($news);
    fwrite($fh, "\n<!--ARTICLE-->\n$news $old_news");
    fclose($fh);
    }
    ?>
    <?
    if(strcmp($subject, "")) {
    if(!(strcmp($passwd, $password))){
    add_article("article_summary.html", summary_page($subject, $date, $summary, $article_id));
    add_article("article_$article_id.html", main_page($subject, $date, $summary, $article_id, $body));
    echo "<p> Article has been added! <p>";
    }else{
    echo "<p><b> Password is wrong! </b>";
    }
    }
    ?>

    <form action=news.php method=post>
    <table border=0>
    <tr> <td> (Password): </td><td> <input type=text name=passwd size=30> </td></tr>
    <tr> <td> Subject: </td><td> <input type=text name=subject size=30> </td></tr>
    <tr> <td> Article ID: </td><td> <input type=text name=article_id value=<? echo date("Y_m_j_is"); ?> size=30> </td></tr>
    <tr> <td> Date/Author/etc: </td><td> <textarea name=date rows=2 cols=30 wrap=soft><? echo date("M j, Y\n"); ?>Author: </textarea> </td></tr>
    <tr> <td> Summary: </td><td> <textarea name=summary rows=5 cols=30 wrap=soft></textarea> </td></tr>
    <tr> <td> Body: </td><td> <textarea name=body rows=15 cols=30></textarea> </td></tr>
    </table>
    <input type=submit name=preview value=preview>
    <input type=submit name=submit value=Add>
    </form>
    <?php
    }
    ?>
    [/php]

    Provo a spiegarti come funziona,
    la prima volta che accedi alla pagina news.php, post non è settato, quindi viene eseguita la parte finale dello script che ti presenta il form da compilare.
    Se premi su preview, ricarichi la pagina inviando i dati e post è settato = preview, quindi la parte iniziale del codice viene eseguita, ti mostra il preview di quello che hai inserito, e contiene il form in modalità hidden. Mostra due bottoni, back e add, se fai add il form hidden che contiene il form precedentemente compilato viene inviato, alla pagina, lo script continua ed esegue la parte centrale del codice che aggiunge il post.
    Mentre se fai back, ti manda indietro dove puoi continuare a modificare il form.
    [url=http://www.alice.bo.it/news.php]Questa è la mia pagina di prova


  • User

    Grazie MILLE ! ..In effetti stavo provando ad abbozzare un procedimento simile ^^ ..
    Comunque avrei soltanto un ultimo quesito e la chiudo.. C'è un modo per far si che cliccando il pulsante "Anteprima" la preview si apra in finestra pop-up?


  • User Attivo

    E' veramente complicato da fare! Non posso aiutarti in questo, mi spiace.


  • User

    Fa nulla, il tuo aiuto è già stato preziosissimo 🙂
    Grazie!


  • User

    http://www.worldsfinest.it/public/superman/dossier/prova.php
    purtroppo c'è un problema quando nel "Body" aggiungo dei codici HTML e visualizzo la Preview..
    Se vuoi provare a vedere cosa accade clicca sulla mia pagina di prova e visualizza una preview aggiungendo nel testo qualche piccolo codice..


  • User

    Tutto risoloto ^^.. ```
    <input type=hidden name=body rows=15 cols=30 value="<? echo $body ?>">

     
    GRAZIE ANCORA A TUTTI (in particolare Jantima)

  • User Attivo

    Cia Darkel, in effetti non avevo pensato a quel possibile problema.
    Ora che hai value="<? echo $body ?>" con le doppie virgolette, potrebbe darti problemi se nel testo del Body hai delle doppie virgolette poichè causerebbero la chiusura della stringa per l'interprete php.
    Esistono funzioni tipo [url=http://us3.php.net/addslashes]AddSlashes() e simili, usate in php per ovviare a questo problema, che è anche molto pericoloso poichè utenti malintenzionati possono sfruttarlo per inserire del codice php che poi viene eseguito, e tramite questo possono creare files e poi usarli a tuo danno.


  • User Attivo

    Ciao ancora, ho un altra considerazione/modifica da aggiungere.
    L'altra sera mentre provavo degli script miei php ho trovato una specie di bug che mi permetteva di ottenere i permessi da amministratore senza inserire la password.
    Il bug funzionava così: tramite la pagina di "password recover" o la pagina di "sign-up" se inserivo nel campo username un qualsiasi username e facevo invia, e poi cliccavo il bottone Back (del browser) automaticamente mi trovavo loggata con quel nome utente.
    Questo avveniva perchè la variabile $username era settata e associata alla mia sessione php tramite cookie, e veniva poi usata per autenticarmi, poichè il codice php credeva che $username fosse il mio username di utente loggato.

    Riguardo a questo bug posso dire due cose:
    ? La prima è che lo script usato avrebbe dovuto essere scritto meglio, tipo anche solo usando dei nomi di variabili diverse, tipo $username_passwordrecover e $username_signup eccetera, invece che usare sempre $username.
    ? La seconda è che c'è una configurazione in php.ini che permette tutto questo che nelle nuove versioni di php è disabilitata di default (io la avevo attiva poichè avevo agguiornato php senza però toccare il file ini.)
    L'opzione si chiama [url=http://us.php.net/register_globals]register_globals, e se la avessi avuta settata su OFF il bug non si sarebbe potuto riprodurre poichè quelle variabili sarebbero state considerate locali alla pagina corrente e non globali e non avrebbero potuto permettermi di ottenere login usando il mio trick.

    Tutto questo discorso perchè?
    Perchè ora che ho register_globals = Off
    la mia pagina di prova con il tuo script per la anteprima delle news NON funziona più!
    Se a te funziona ne deduco che hai register_globals = On.

    Per modificare il codice in modo che funzioni con register_globals = Off
    basta modificarlo in questo modo:
    [php]
    <?
    if (isset($_POST['preview'])) {
    //anteprima();
    echo 'Preview articolo:<br>';
    echo 'Subject: '.$_POST['subject'] .'<br>';
    echo 'Date: '.$_POST['date']. '<br>';
    echo 'Summary: '.$_POST['summary']. '<br>';
    echo 'Article ID: '.$_POST['article_id']. '<br>';
    echo 'Body: '.$_POST['body']. '<br>';
    ?>
    <form action=news.php method=post>
    <input type=hidden name=passwd size=30 value=<? echo $_POST['passwd'] ?>>
    <input type=hidden name=subject size=30 value=<? echo $_POST['subject'] ?>>
    <input type=hidden name=article_id value=<? echo $_POST['article_id'] ?>>
    <input type=hidden name=date rows=2 cols=30 wrap=soft value=<? echo $_POST['date'] ?>>
    <input type=hidden name=summary rows=5 cols=30 wrap=soft value=<? echo $_POST['summary'] ?>>
    <input type=hidden name=body rows=15 cols=30 value=<? echo $_POST['body'] ?>>
    <input type=button name=back value=back onclick="history.go(-1);return false;">
    <input type=submit name=submit value=Add >
    </form>
    <?php
    } else {

    // codice normale della form di inserimento
    include ("template.inc");
    include ("config.php");
    $summary_template = "t_summary.html";
    $article_template = "t_article.html";
    $max_summary = 5;

    function summary_page ($subject, $date, $summary, $article_id)
    {
    global $summary_template;
    $t = new Template();
    $t->set_file("SummaryPage", $summary_template);
    $article_url = "article_".$article_id.".html";
    $date = nl2br($date);
    $summary = nl2br($summary);
    $t->set_var( array(
    "subject" => $subject,
    "date" => $date,
    "summary" => $summary,
    "article_url" => $article_url
    ));
    $t->parse("Summary", "SummaryPage");
    return $t->get_var("Summary");
    }
    function main_page ($subject, $date, $summary, $article_id, $body)
    {
    global $article_template;
    $t = new Template();
    $t->set_file("ArticlePage", $article_template);
    $article_url = "article_".$article_id.".html";
    $date = nl2br($date);
    $summary = nl2br($summary);
    $body = nl2br($body);
    $t->set_var( array(
    "subject" => $subject,
    "date" => $date,
    "summary" => $summary,
    "body" => $body,
    "article_url" => $article_url
    ));
    $t->parse("Article", "ArticlePage");
    return $t->get_var("Article");
    }
    function add_article($filename, $news)
    {
    if(file_exists($filename)){
    $fh = fopen($filename, "r");
    $old_news = fread($fh, filesize($filename));
    fclose($fh);
    }
    /* TODO: Multipage articles
    preg_match_all("<!--ARTICLE PAGE=(\d*)-->", $old_news, $matches;

    if( count($matches[0]) >= $max_summary){
    $oldfilename = $filename.($matches[0][0]+1);
    }
    */
    $fh = fopen($filename, "w");
    $news = stripslashes($news);
    fwrite($fh, "\n<!--ARTICLE-->\n$news $old_news");
    fclose($fh);
    }
    ?>
    <?
    if(strcmp($_POST['subject'], "")) {
    if(!(strcmp($_POST['passwd'], $password))){
    add_article("article_summary.html", summary_page($_POST['subject'], $_POST['date'], $_POST['summary'], $POST['article_id']));
    add_article("article
    $article_id.html", main_page($_POST['subject'], $_POST['date'], $_POST['summary'], $_POST['article_id'], $_POST['body']));
    echo "<p> Article has been added! <p>";
    }else{
    echo "<p><b> Password is wrong! </b>";
    }
    }
    ?>

    <form action=news.php method=post>
    <table border=0>
    <tr> <td> (Password): </td><td> <input type=text name=passwd size=30> </td></tr>
    <tr> <td> Subject: </td><td> <input type=text name=subject size=30> </td></tr>
    <tr> <td> Article ID: </td><td> <input type=text name=article_id value=<? echo date("Y_m_j_is"); ?> size=30> </td></tr>
    <tr> <td> Date/Author/etc: </td><td> <textarea name=date rows=2 cols=30 wrap=soft><? echo date("M j, Y\n"); ?>Author: </textarea> </td></tr>
    <tr> <td> Summary: </td><td> <textarea name=summary rows=5 cols=30 wrap=soft></textarea> </td></tr>
    <tr> <td> Body: </td><td> <textarea name=body rows=15 cols=30></textarea> </td></tr>
    </table>
    <input type=submit name=preview value=preview>
    <input type=submit name=submit value=Add>
    </form>
    <?php
    }
    ?>
    [/php]


  • User

    Ancora grazie mille per l'attenzione!... Ho seguito i tuoi preziosi consigli (molto valido anche quello sullo stripslash!)... Ora però mi sorge un dubbio. Le pagine pubblicate nella cartella 'public' non sono rintracciabili dai motori di ricerca. Esiste un modo per divulgarle?
    Ancora Grazie 😉


  • User Attivo

    Ti consiglio di continuare la discussione nel forum apposito "Posizionamento Nei Motori di Ricerca". Quando posti il tuo quesito nell'altra sezione prova a dare più informazioni tipo: Come fai a dire che non sono rintracciabili? Le hai cercate e non le trova? Hai segnalato il sito a google? Se puoi, indica il nome del sito così i vari esperti possono controllare e darti tutti i suggerimenti migliori per modo che le pagine vengano trovate!