Reputation: 147
Good morning,
I'm trying to figure out something here as I continue my journey with PHP. I have a tabbed HTML document. In one tab, I want to pass a parameter to the PHP file, do the calculation and show the result. The problem is as soon as the submit button is clicked, the whole tab experience is gone and the results appear on a fresh page. What I'm trying to figure out is how to echo back the answer to the tab a user is on. Here's the code for the tab in question and I have a field for the answers, which is where I want the results of a query to populate. I'll show you the division in question here:
<div id="tab1">
<p>Check a price</p>
<form action="rating.php" method="post">Miles: <input name="miles" type="text" />
<input name="submit" value="Submit" type="submit" /></form><br/>
<input type="text" value="<?php echo $ratepermile; ?>"></div>
where $ratepermile is one of the fields that comes from a query done by rating.php. And, of course, if I had any other calculations I want to do with that file, I'd like to be able to pass those answers and display within the tab.
In rating.php, it connects to the database fine and it will display the answer fine, but on a new screen. Am I missing something to make sure the values I select are available and passed back to the HTML file? Here's the part of the code I use for my select (I only have it printing the result right now in this code, assuming that the result of this query will still be globally available to me wherever I need it. I don't know if that's how it work though lol.)
$miles = (int)$_POST['miles'];
try {
$dbh = new PDO("mysql:host=$hostname;dbname=ratetable", $username, $password);
echo 'Connected to database<br />';
$sql = "SELECT * FROM rates WHERE mileage<=$miles ORDER BY mileage DESC LIMIT 1";
foreach ($dbh->query($sql) as $row)
{
print $row['ratepermile'] . <br />';
}
$dbh = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>
Thank you for having a look.
Upvotes: 0
Views: 99
Reputation: 9311
You need to employ AJAX to asynchronously send the request with the parameter to your PHP script and then to display the result on your tabbed page once the server returned a result. If you are using jQuery, take a look at $.post()
.
Upvotes: 1