echoing to text box in php
<p>I'm trying to do something simple to learn how to do a query and then put the answer into a box. I have a HTML file that looks like this:</p>

<pre><code>&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Test&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;form action="results.php" method="post"&gt;Search: &lt;input name="term" type="text" /&gt;&lt;br /&gt;    
&lt;input name="submit" value="Submit" type="submit" /&gt;
&lt;p&gt;Answer: &lt;input name="answer" type="text" /&gt;&lt;/p&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p>And the php code is: </p>

<pre><code>&lt;?php
$hostname = 'host.com';
$username = 'ratetable';
$password = 'mypassword';
$term = (int) $_GET['term'];

try 
{
   $db = new PDO("mysql:host=$hostname;dbname=ratetable", $username, $password);
   //echo 'Connected to database&lt;br /&gt;';

   foreach($db-&gt;query('SELECT * FROM rates WHERE mileage&lt;= ' . $term . ' ORDER BY mileage DESC LIMIT 1') as $row) {
       echo "&lt;input type='text' name='answer' value='" . $row['ratepermile'] . "'&gt;";
   }
}         
catch (PDOException $e) {
       echo $e-&gt;getMessage();
       throw($e);
   }
?&gt;
</code></pre>

<p>So I'm trying to put ratepermile, a field in the database, into the 'answer' text box. What I get is the screen clears, but no form and no result, even after I comment out the 'connected to database' echo line and use something that I know exists in the database.</p>

<p>How can I keep the form on screen and echo to the text box? </p>

<p>Thanks for looking. </p>
