I'm working on a project for work, and I've run into some issues. I'm creating dynamic elements based on PDO queries and ajax, and I've searched and searched to resolve my specific issue. Basically, I have two select boxes that are dynamically added from a database response, and regardless of which option is selected, all of the dynamically created buttons are made exactly the same. Selected Rack_G
I've also created timers for each Rack/Tray, and the same issue arises for the timers. There is only a record for Rack H Tray 8.Rack H Tray 8
My db_connect.php
$first = "odbc:TIMERODBC";
$user = "exampleuser";
$pass = "examplepass";
$pdo = new PDO($first,$user,$pass);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
Below is my class.db.php file that queries my database
public function selectOven()
{
try{
$stmt = $this->conn->query("SELECT sub.equipName FROM STR_EquipSubCategory sub LEFT JOIN STR_EquipCategory category ON category.categoryID = sub.categoryID GROUP BY sub.equipName");
$data = $stmt->fetchAll();
foreach($data as $row)
{
echo "<option value=" . $row['equipName'] . "><strong>" . $row["equipName"] . "</strong></option>";
}
}
catch(PDOException $e) {
return $e->getMessage();
}
}
public function selectRack($oven) {
try{
$sth = $this->conn->prepare('SELECT storageBayName FROM STR_EquipSubCategory WHERE equipName =:oven');
$sth->bindParam(':oven',$oven,PDO::PARAM_STR);
$sth->execute();
$data = $sth->fetchAll();
echo "<select id='rack' class='custom-select mb-2'>Rack";
echo "<option value ='Select Rack'></option>";
foreach($data as $row)
{
echo "<option id=".$row['storageBayName']." value=". $row["storageBayName"] . ">". $row["storageBayName"] . "</option>";
}
echo "<select>";
}
catch(PDOException $e) {
return $e->getMessage();
}
}
public function selectRackTray($oven,$rack) {
try{
$sth = $this->conn->prepare("EXECUTE STR_GetTimerOpenTraysSp ?, ?");
$sth->bindParam(1,$oven,PDO::PARAM_STR);
$sth->bindParam(2,$rack,PDO::PARAM_STR);
$sth->execute();
$data = $sth->fetchAll();
foreach($data as $row)
{
if(($row['material'] != 0) ) {
echo "<div class='row'>";
echo "<button type='button' id='tray".$row['storageBayName']." ". $row['sublevelID'] ."' class='btn btn-danger' data-toggle='popover'>Tray " .$row['sublevelID'] . "</button>";
echo "</div>";
}
else {
echo "<div class='row'>";
echo "<button type='button' id='emptyTray".$row['storageBayName']. " ".$row['sublevelID'] ."' class='btn btn-success'>Tray " .$row['sublevelID'] . "</button>";
echo "</div>";
}
}
}
catch(PDOException $e) {
return $e->getMessage();
}
}
And now my ajax jQuery & Ajax
$(document).ready(function(){
$("select#oven").change(function(){
var selectedOven = $("#oven option:selected").val();
$.ajax({
type: "POST",
url: "process-request.php",
data: { oven : selectedOven }
}).done(function(data){
$("#rackResponse").html(data);
});
});
});
$(document).on('change', "select#rack", function() {
var selectedRack = ($(this).val());
var selectedOven = ($("#oven option:selected").text());
$.ajax({
type: "POST",
url: "process-a-request.php",
data:
{
rack: selectedRack,
oven: selectedOven
},
}).done(function(data){
$("#tResponse").html(data);
});
});
If anyone can help me out, I would greatly appreciate it. Note, I'm not super experienced with PHP, but everything else seems to be working correctly.
My problem wasn't with ajax, php, or the PDO statements. I ran some tests on my stored procedure that I was calling, and found the problem there. Thanks for attempting to help me out.