PHP:以纯文本格式生成HTML并放入文本框问题

时间:2016-01-22 15:44:26

标签: php html iframe textarea plaintext

在此处引用上一个问题HTML Generator: Convert HTML to PlainText and put in a textbox using PHP

即使答复产生预期结果,我也遇到了一些问题。

我有这3页:

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

现在我得到了纯文本文件但由于我已经将变量存储在会话中,所以变量没有通过。我得到$ var而不是值。

文本框只显示文件的一半,而不显示<link>和整个<style>标记。

1 个答案:

答案 0 :(得分:3)

<textarea>没有value

您需要在标记内回显该变量。

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

&#34;它必须从$ _SESSION获取变量并完成代码&#34;

另请注意,您正在使用会话。确保会话已在该页面顶部以及可能正在使用会话的任何其他页面上启动session_start();

示例:

session_start();

if(isset($_SESSION['var'])){
   $_SESSION['var'] = "var";
}

else{
   echo "Session is not set.";
}

N.B。:确保您没有在标题之前输出。

如果您收到标题已发送通知/警告,请参阅Stack上的以下内容:

error reporting添加到文件的顶部,这有助于查找错误。

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Then the rest of your code

旁注:只应在暂存时进行显示错误,而不是生产。

证明成功的测试示例,在var内回显<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>';

修改

基于以下模型,将GET数组分配给会话数组。

<?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>';

这是我能给你的最好的。

执行$html = $lb1_session . "\n". $lb2_session;您可以使用"\n"作为每个变量之间的分隔符来回显&#d; d。或者,<br>如果你想要的话;选择是你的。

以上将$html变量分配给链式变量。您可以添加可能需要添加的其他内容$lb3, $lb4, $lb5等。

祝你好运! (buon fortunato)