suppose in php file you are using $dropdownData array variable to store data from db.
//PHP file
<?php
//query to fetch data from db.
$dropdownData;
$this->view('get_project_form.html',$dropdownData);
?>
//Then use this array variable $dropdownData in ur HTML file with “for loop” to display
//HTML file
//you can also validate the $dropdownData if it’s not empty; otherwise php will give error.
<select id="first_query" name="queries" required>
<option value="" disabled selected>Make a Selection</option>
<? foreach ($dropdownData as $val => $text) { ?>
<option value="<? echo $val; ?>" > <? echo $text;?> </option>
<? } ?>
</select>
I’m trying to get data from a MySQL database, that is being selected from a dropdown in an HTML file, and have the data display on the same HTML page that it is being selected from. I already have the HTML and PHP working to retrieve the data and display it, but it is not displaying on the same page that the user requests it from, it is displaying on the PHP page.
Here is the HTML code for get_project_form.html :
<html>
<head>
<title>Get Project Form</title>
<!--Link this html page to the project_style.css page-->
<link rel="stylesheet" type="text/css" href="project_style.css">
</head>
<body>
<div class="container">
<form action="get_project_action.php" autocomplete="off" method="get">
<label for="Query Selections">Query Selections : (You can only select
all data from "Projects"
for now):</label>
<select id="first_query" name="queries" required>
<option value="" disabled selected>Make a Selection</option>
<option value="Projects">Projects</option>
</select><br />
<input type="submit" value="Submit"><br />
</div>
</body>
</html>
After you make the selection and click submit the data appears on a page with the URL,
http://localhost:8015/get_project_action.php?queries=Projects
Here is the code for the php file get_project_action.php :
<?php
echo "<table style='border: solid 1px black;'>";
class TableRows extends RecursiveIteratorIterator
{
function __construct($it) {
parent::__construct($it, self::LEAVES_ONLY);
}
function current() {
return "<td style='width: 150px; border: 1px solid black;'>" .
parent::current() . "</td>";
}
function beginChildren() {
echo "<tr>";
}
function endChildren() {
echo "</tr>" . "n";
}
}
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "pmo";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username,
$password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$query = $_GET["queries"];
$stmt = $conn->prepare("SELECT * FROM $query");
$stmt->execute();
// set the resulting array to associative
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
foreach (new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as
$k => $v) {
echo $v;
}
} catch (PDOException $e) {
echo "Error: " . $e->getMessage();
}
$conn = null;
echo "</table>";
?>
I’d like to keep the HTML and PHP separate if possible. But from what I’m reading I’m guessing that isn’t possible. I’m hearing that its best to keep the logic in a separate PHP file but that its best to put the rest of the PHP that does the displaying in the HTML file and change the .html file to a .php instead. But I was hoping there is a way to have the data display in the get_project_form.html page below the form somewhere without combining any of the PHP inside the get_project_form.html file and changing it to a .php. If this is possible, please show me how. If not, please feel free to show me how to do whatever is considered good practice for displaying the data on a web page. Any help will be highly appreciated!
You can achieve that with making the HTML file a PHP file and using a global variable. Like