0

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

3 Answers 3

2

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">
Sign up to request clarification or add additional context in comments.

3 Comments

And what the echo $lines[$number_casual]; prints to you?
Random texts from a .txt file
This solution works, unless you have special chars. You can try the @MonkeyZeus solution with htmlentities().
2

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

2 Comments

Both of them are not working. Please see my question updated.
@user8481790 Your echo is missing a closing single-quote and semicolon; this is a common syntax error. You can either fix the echo or just use my second example.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.