Search code examples
phphtmliframetextareaplaintext

PHP: Generate HTML as plaintext and put in a textbox issue


refering as previous question here HTML Generator: Convert HTML to PlainText and put in a textbox using PHP

Now i got some problems even if the reply produce the expected result.

I got these 3 pages:

Page1.php

// This page contain two columns, one for the form that take the 
variables, and other one that contain the iframe that must to display the plaintext

Page2.php

// Cutted code that take $_GET variables and store in $_SESSION

$html = file_get_contents('page3.php');

echo '<textarea readonly style="border:none;resize:none" rows="50" cols="116" value="'. $html .'"></textarea>';

Page3.php

// This is the file page3.php that must to be in plaintext, but first
 it must take the variables from $_SESSION and complete the code

Now I get the plain text file but the variables aren't passed since i've stored them in session. i got $var instead of the value.

And the textbox displays only half of the file, not showing the <link> and the whole <style> tags.


Solution

  • <textarea> does not have value.

    You need to echo that variable inside the tags.

    $html = "Text here";
    echo '<textarea readonly style="border:none;resize:none" rows="50" cols="116">'. $html .'</textarea>';
    

    "it must take the variables from $_SESSION and complete the code"

    Also note that you are using sessions. Make sure the session was started having session_start(); at the top of that page and for any other pages that may be using sessions.

    Example:

    session_start();
    
    if(isset($_SESSION['var'])){
       $_SESSION['var'] = "var";
    }
    
    else{
       echo "Session is not set.";
    }
    

    N.B.: Make sure you are not outputting before header.

    Consult the following on Stack if you get a headers sent notice/warning:

    Add error reporting to the top of your file(s) which will help find errors.

    <?php 
    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    
    // Then the rest of your code
    

    Sidenote: Displaying errors should only be done in staging, and never production.


    Test example which proved successful, echoing var inside <textarea>:

    <?php 
    session_start();
    
    if(isset($_SESSION['var'])){
       $_SESSION['var'] = "var";
    
    $var = $_SESSION['var'];
    }
    
    else{
       echo "Session is not set.";
    }
    
    // $html = "Text here";
    
    $html = $var;
    echo '<textarea readonly style="border:none;resize:none" rows="50" cols="116">'. $html .'</textarea>';
    

    Edit:

    Base yourself on the following model to assign GET arrays to sessions arrays.

    <?php 
    session_start();
    
    $_GET ['lb1'] = "lb1";
    $lb1 = $_GET ['lb1'];
    $_GET ['lb1'] = $_SESSION["lb1"];
    $_SESSION["lb1"] = $lb1;
    //echo "Hey LB1 " . $lb1;
    $lb1_session = $lb1;
    
    $_GET ['lb2'] = "lb2";
    $lb2 = $_GET ['lb2'];
    $_GET ['lb2'] = $_SESSION["lb2"];
    $_SESSION["lb2"] = $lb2;
    //echo "Hey LB2" . $lb2;
    $lb2_session = $lb2;
    
    $html = $lb1_session . "\n". $lb2_session;
    echo '<textarea readonly style="border:none;resize:none" rows="50" cols="116">'. $html .'</textarea>';
    
    ?>
    
    <a href="check_get_sessions.php">Check GET sessions</a>
    

    check_get_sessions.php

    <?php 
    session_start();
    
    if(isset($_SESSION['lb1'])){
    
       $lb1_session = $_SESSION['lb1'];
    
       echo $lb1_session;
    }
    
    
    if(isset($_SESSION['lb2'])){
       $lb2_session = $_SESSION['lb2'];
    
       echo $lb2_session;
    }
    
    $html = $lb1_session . "\n". $lb2_session;
    echo '<textarea readonly style="border:none;resize:none" rows="50" cols="116">'. $html .'</textarea>';
    

    That's the best I can offer you.

    Doing $html = $lb1_session . "\n". $lb2_session; you can use "\n" as seperators between each variable to be echo'd. Or, <br> if you want; the choice is yours.

    The above assigns the $html variable to chained variables. You can add the others that may need to be added $lb3, $lb4, $lb5 etc.

    Good luck! (buon fortunato)