I want to create an accordion MENU (Don't know if it will be created using divs
or <ul>
) and when I click on a submenu I need to execute a mysql query
.
I mean, when I click on the submenu I need to update the data/content of the table that is beside the menu. I'm not sure how to do it and/or if its better to use Ajax
or PHP
, I'm new to both.
All I could do so far is to create the accordion menu using divs.
<?php
include("conexao.php");
function loadTable(){
$pdo = conectar();
$query_select = $pdo->prepare("SELECT prof.nome AS PROFESSOR, disc.nome AS DISCIPLINA, prof.ch AS ch
FROM professor prof
INNER JOIN prof_disc pd ON prof.id = pd.fk_prof
INNER JOIN disciplina disc ON pd.fk_disc = disc.id");
$query_select->execute();
$line = $query_select->fetchAll(PDO::FETCH_OBJ);
foreach($line as $row){
echo "
<h3><span class='arrow-r'></span>".$row->PROFESSOR."</h3>
<div style='display: none;' class=''>
<p>".$row->DISCIPLINA."</p>
</div>
";
}
echo '</table>';
}
?>
We'll have something like:PROFESSOR | DISCIPLINA
Math - | History
Geografia | Physics
If you want a new mysql query to be executed when you click on a submenu download jQuery. When you have jQuery use the jQuery .click() function and .post() function.
Edit:
Handle the event like this:
Ps. The jQuery selecting logic is the same as in css.
$(document).ready(function()
{
$("#subMenuItemId").click(function()
{
//Post action here
});
});
Complementing Jojo01
answer in how to use post
in Ajax:
var value1 = "somevalue";
var productsURL = "getproducts.php?key1="+value1;
$.ajax({
type: "POST",
url: productsURL,
cache: false,
success: function(result){
//do something with result
}
});
In your PHP
file (getproducts.php
) you may have a variable for the value/key you sent using ajax and then fetch the database for some value, when you have the value, you can just echo
it and it will return to the result
variable in the Ajax code.
e.g. (it will be something like that)
<?php
if(isset($_POST["key1"])) {
$key1 = $_POST["key1"];
$sql = "SELECT column1 FROM table1 WHERE key1=".$key1;
//fetch values from database
//get values
echo $values;
}
?>