Home > Net >  How to show php string in html input
How to show php string in html input

Time:07-19

I have a flat file TestFile.txt that has about 40 lines of data, each item on a separate row, so 40 lines. I have a PHP code that finds the row containing a string I want to find using using $Search_String. Then displays only the row containing $Search_String. This works exactly as I want. However; it displays the result in text area. How do I display the result into a label box? The php reads the flat just as I would expect. I have 2 html input fields, but the string $line only contains data inside the if statement. Any help with this would be great!

Here is part of my flat file, filename is TestFile.txt:

RXFrequency=432675000
TXFrequency=432675000
RXOffset=260
TXOffset=120
Network=mnet.hopto.org
Password=8Yg81xrqK0313zt
Latitude=34.657783
Longitude=-3.784595
Port=62021

Here is my PHP:

<!DOCTYPE html>
<html>
<body>
<?php
$line = '';
?>

<input type="text" ID="message" value="<?php echo $line;?>"/>

<?php

// Place text to look for in string $Search_String.
// The $Search_String will remain hard coded in my production
// code. The users will not be able to select $Search_String.
$Search_String = "RXF";
// Identify Text File, open File and Read the File.
$MyFile = fopen("TestFile.txt", "r") or die("Unable to open file!");
$found= "False";
// Create the while loop.  Test each line with the if statement,
// looking for $Search_String, and place the result into string $line.
// Next, echo string $line which containes the found line in the 
// flat text file.  It will return the entire line even from a 
// partial $Search_String, which is what I want.
while ( $line = fgets( $MyFile ) )
  {
    if ( str_contains( $line, $Search_String ) ) 
     {
     echo $line;
     echo $Search_String;
     }
  } 

// Properly close the text file.
   fclose($MyFile);
?>

<input type="text" id="message" value="<?php echo $line;?>"/>

</body>
</html>

This code does properly open the flat text file, it does the search and finds the line containing the data I am trying to retrieve. I see the correct gata using echo in the if statement. Now I need to place the found data into the html input text box. The string $line seems to contain no data outside the if block of code. Can I make the variable $line static or global so the data is available throughout the code? Or am I using the html input text improperly? I think I do need to use input text, because I want to be able to modify the data and then write the line back to the flat file.

Thanks for any suggestions, Mitch

CodePudding user response:

This is a small grasp of your code including the proposed solution to loop through and show all the lines matching the criteria from your file:

<?php

/*...*/

$lines = [];

while ( $line = fgets( $MyFile ) ) {
    if ( str_contains( $line, $Search_String ) ) {
        //here you are keeping track of each line matching criteria
        $lines[] = $line;
        
        //echo $line;
        //echo $Search_String;
    }
} 
?>

<?php foreach($lines as $line): ?>
    <input type="text" value="<?= $line; ?>"/> 
<?php endforeach; ?>
  • Related