0

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

4
  • 1
    Instead of $input = $_POST['input']; you can save this as array since you're now overriding the variable and in HTML you can use foreach to get all array values. Commented Sep 25, 2016 at 12:59
  • @EdvinTenovimas how can he will use foreach inside html because the html will not know the string from PHP? Commented Sep 25, 2016 at 13:18
  • @adi Assuming he's using HTML code in PHP file (from <input type="value" name="input" value="<?php echo $input; ?>"> line), this could work, actually. Commented Sep 25, 2016 at 13:25
  • @EdvinTenovimas I need array of string because I need to print out the first 3 rows of a file. Will that work for that? Commented Sep 27, 2016 at 8:33

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks for your comment! It doesn't work for me.. I need to print 3 rows of $handle - each loop..
@user3488862 What does fgets($handle) return? you could try a nested loop. Not sure what fgets return, so i'm not sure how to respond just yet.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.