user3488862
user3488862

Reputation: 1359

passing values from php to html

I want to pass values from my php code (in the same page) to my html - so I could print it out nicely

here is my php function (it's in a while cause it needs to print out 3 lines from a text)

    if(sizeof($words)==1)
    {
        $single = new single;
        $docres = $single->find($words);
            $dir = "source";
            foreach ($docres as $key=>$filename) {
                $counter = 0; 
                $file = $filename +1;
                $handle = fopen($dir."/".$file.'.txt',"r");
                if($handle)
                {
                    while($counter < 3)
                    {
                        echo fgets($handle);
                       //this is what i need to print in my html
                        $input = $_POST['input'];
                        $counter++;
                }
            }
    }
}

and here is my html

<html>
    <head> 
        <title>serach engine</title> 
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    </head> 
    <body>
        <form action="" method="POST">
            <center> 
                <h1> My Search Engine </h1>
                <input type = 'text' size='90'  value='' name = 'search' > <br> 
                <input type = 'submit' name = 'submit' value = 'Search source code' >         
               // <input type="value" name="input" value="<?php echo $input; ?>">
            </center> 
        </form >
      //I would like to print it out here
        </div>
    </body > 
</html >

I searched over the web and saw a solution using $_POST but it didn't work for me...

Upvotes: 0

Views: 59

Answers (1)

Luke Sweeney
Luke Sweeney

Reputation: 1055

You could just append it to a new variable and echo it out in the html.

For example:

while($counter < 3) {
     echo fgets($handle);
    //this is what i need to print in my html
    $input .= $_POST['input'] . "<br />";
    // You could also do:
    // $input .= "<li>" . $_POST['input'] . "</li> "
    // For a list item
    $counter++;
}

And then echo it in the html:

</form>
  <?php echo $input; ?>
</div>

Note: You did not increment your $counter in the while loop.

Upvotes: 1

Related Questions