SudoGuy
SudoGuy

Reputation: 165

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

Here is my php code:

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

This php code generates random texts from test_file.txt file. I want to show the randomly generated texts by this php code inside this input field:

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

Update:

Here is what I have tried but not working: This one is showing a blank field:

<?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">

This one is also showing a blank field:

<?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">

This one is crashing my page:

<?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">

My .txt file doesn't contain any special chars. It looks like this:

text1
text2
text3

Update 2: Sorry everyone. I accidentally put wrong .txt file name in the php code so the field was blank. And this code worked:

<?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">

Upvotes: 0

Views: 129

Answers (3)

MonkeyZeus
MonkeyZeus

Reputation: 20747

Assuming that your text file does not contain properly encode HTML content then you need to make sure to use htmlentities()

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

or

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

Upvotes: 2

Felippe Duarte
Felippe Duarte

Reputation: 15131

Just add to the value tag:

<?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">

Upvotes: 2

Andreas
Andreas

Reputation: 23958

Add the html to your echo.

 //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">';

Upvotes: 0

Related Questions