Is there a way to dynamically change the background color of a <div>
based on database values when different options are selected from a dropdown list? I have been researching extensively and while I found valuable information in previous posts, I am struggling to implement it into my code.
My objective: I need to create a dropdown menu with various temperature options. Each temperature corresponds to a specific color code in the database which should be applied to the <div>
.
For example, 99 degrees would result in a yellow background, while 105 degrees would yield an orange background.
Current code: Dropdown menu page
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Alert Test</title>
<meta/>
</head>
<body>
<p>
Page copy
</p>
<script language="javascript" type="text/javascript">
<!--
//Browser Support Code
function ajaxFunction_1(){
var ajaxRequest;
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Oops! We apologize.");
return false;
}
}
}
ajaxRequest.onreadystatechange = function (){
if(ajaxRequest.readyState == 4){
var ajaxDisplay = document.getElementById('ajaxDiv_4');
ajaxDisplay.innerHTML = ajaxRequest.responseText;
}
}
var temp = document.getElementById('temp').value;
var queryString = "?temp=" + temp;
ajaxRequest.open("GET", "abc.php"+ queryString, true);
ajaxRequest.send(null);
}
//-->
</script>
<form>
<br />
<select id='temp'>
<option value=''>Choose Product</option>
<option value='90'>90</option>
<option value='91'>91</option>
<option value='92'>92</option>
<option value='93'>93</option>
<option value='94'>94</option>
<option value='95'>95</option>
<option value='96'>96</option>
<option value='97'>97</option>
<option value='98'>98</option>
<option value='99'>99</option>
<option value='100'>100</option>
<option value='101'>101</option>
<option value='102'>102</option>
<option value='103'>103</option>
<option value='104'>104</option>
<option value='105'>105</option>
<option value='106'>106</option>
<option value='107'>107</option>
<option value='108'>108</option>
<option value='109'>109</option>
<option value='110'>110</option>
<option value='111'>111</option>
<option value='112'>112</option>
<option value='113'>113</option>
</select>
<input type='button' onclick='ajaxFunction_1()' value='Select' />
</form>
<div id='ajaxDiv_4'></div>
</body>
</html>
Database query code snippet
<?php
include_once('conn.php');
// Retrieve data
if( isset($_GET['temp']) && $_GET['temp'] != '' ){
$temp = $_GET['temp'];
$temp = mysql_real_escape_string($temp);
//query
$query = "SELECT * FROM temps WHERE temp = '$temp'";
//Execute
$qry_result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($qry_result)){
$display_string = '<strong>WARNING</strong> '.$row['alert'].'<br>';
$display_string .= '<strong>BE AWARE</strong> '.$row['messa'].'<br>';
$display_string .= '<strong>RESPONSE</strong> '.$row['action'].'<br>';
$display_string .= '<br>';
}
echo $display_string;
} else {
return null;
}
?>
The dropdown and page functionality is working correctly. The final touch needed is to add some color coding. Any assistance will be greatly appreciated.