繁体   English   中英

如何在此html输入字段中显示此php脚本生成的文本?

[英]How to show this php script generated text inside this html input field?

这是我的PHP代码:

<?php
     //Path of file
     $myFile = "test_file.txt";
     //Read file from array
     $lines = file($myFile);
     //Get number line of file
     $lineTotal = count($lines);
     //Remove 1 line (start from 0)
     $count = $lineTotal-1;
     //Get casual number
     $number_casual = rand(0,$count);
     //Print line 2 ([0] = 1 ; [1] = 2 ; ...)
     echo $lines[$number_casual];
?>

此php代码从test_file.txt文件生成随机文本。 我想在此输入字段中显示此php代码随机生成的文本:

<input name="accesspin" style="width: 300px" value="the text will be here" size="36">

更新:

这是我尝试但无法使用的方法:这是一个空白字段:

<?php
 //Path of file
 $myFile = "test_file.txt";
 //Read file from array
 $lines = file($myFile);
 //Get number line of file
 $lineTotal = count($lines);
 //Remove 1 line (start from 0)
 $count = $lineTotal-1;
 //Get casual number
 $number_casual = rand(0,$count);
 //Print line 2 ([0] = 1 ; [1] = 2 ; ...)
 echo $lines[$number_casual];
?>
<input name="accesspin" style="width: 300px" value="<?php echo htmlentities( $lines[$number_casual] ); ?>" size="36">

这也显示了一个空白字段:

<?php
 //Path of file
 $myFile = "test_file.txt";
 //Read file from array
 $lines = file($myFile);
 //Get number line of file
 $lineTotal = count($lines);
 //Remove 1 line (start from 0)
 $count = $lineTotal-1;
 //Get casual number
 $number_casual = rand(0,$count);
 //Print line 2 ([0] = 1 ; [1] = 2 ; ...)
 echo $lines[$number_casual];
?>

<input name="accesspin" style="width: 300px" value="<?php echo $lines[$number_casual]; ?>" size="36">

这使我的页面崩溃:

<?php
 //Path of file
 $myFile = "test_file.txt";
 //Read file from array
 $lines = file($myFile);
 //Get number line of file
 $lineTotal = count($lines);
 //Remove 1 line (start from 0)
 $count = $lineTotal-1;
 //Get casual number
 $number_casual = rand(0,$count);
 //Print line 2 ([0] = 1 ; [1] = 2 ; ...)
 echo '<input name="accesspin" style="width: 300px" value="' . htmlentities( $lines[$number_casual] ) . '" size="36">
?>

<input name="accesspin" style="width: 300px" value="<?php echo $lines[$number_casual]; ?>" size="36">

我的.txt文件不包含任何特殊字符。 看起来像这样:

text1
text2
text3

更新2:对不起大家。 我不小心在php代码中输入了错误的.txt文件名,因此该字段为空白。 这段代码有效:

<?php
 //Path of file
 $myFile = "random.txt";
 //Read file from array
 $lines = file($myFile);
 //Get number line of file
 $lineTotal = count($lines);
 //Remove 1 line (start from 0)
 $count = $lineTotal-1;
 //Get casual number
 $number_casual = rand(0,$count);
 //Print line 2 ([0] = 1 ; [1] = 2 ; ...)
?>
<input name="accesspin" style="width: 300px" value="<?php echo htmlentities( $lines[$number_casual] ); ?>" size="36">

只需添加到value标签:

<?php
 //Path of file
 $myFile = "test_file.txt";
 //Read file from array
 $lines = file($myFile);
 //Get number line of file
 $lineTotal = count($lines);
 //Remove 1 line (start from 0)
 $count = $lineTotal-1;
 //Get casual number
 $number_casual = rand(0,$count);
 //Print line 2 ([0] = 1 ; [1] = 2 ; ...)
 echo $lines[$number_casual];
?>

<input name="accesspin" style="width: 300px" value="<?php echo $lines[$number_casual]; ?>" size="36">

假设您的文本文件不包含正确编码的HTML内容,那么您需要确保使用htmlentities()

echo '<input name="accesspin" style="width: 300px" value="' . htmlentities( $lines[$number_casual] ) . '" size="36">';

要么

<input name="accesspin" style="width: 300px" value="<?php echo htmlentities( $lines[$number_casual] ); ?>" size="36">

将html添加到您的回声中。

 //Path of file
 $myFile = "test_file.txt";
 //Read file from array
 $lines = file($myFile);
 //Get number line of file
 $lineTotal = count($lines);
 //Remove 1 line (start from 0)
 $count = $lineTotal-1;
 //Get casual number
 $number_casual = rand(0,$count);
 //Print line 2 ([0] = 1 ; [1] = 2 ; ...)
 echo '<input name="accesspin" style="width: 300px" value="' . $lines[$number_casual]; . '" size="36">';

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM