我想显示onload a(sort-of)切换按钮,具体取决于mysql数据库中一个表内的值(该表只有一行),当我点击它时,我想更改一个值同一个表从0到1或相反。我已经在相关问题上看到了很多答案,但其中没有一个似乎对我有用。将需要一些帮助。非常感谢!
这是我的php代码:
<?php
include 'db_ecoheating.php';
mysql_connect($dbhost,$dbuser,$dbpass)
or die ("Unable to connect to database");
mysql_select_db($dbname)
or die ("Unable to select database");
$sql = "SELECT coil1 from coils ";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)){
$coil1 = $row['coil1'];
}
$arr = array(
'value1'=> $coil1,
);
echo json_encode($arr);
?>
我在js中的功能(包括jquery):
function buttonFunctions(){
var x1=document.getElementById("coil1");
var value = $.ajax({
type: "GET",
url: "coils.php",
data: query,
dataType: "json"
success: function(data){
console.log(data.value1);
}
});
if(value=='0') coil1.src = "Button2.png";
else if(value=='1') coil1.src = "Button1.png";
//coil1.src="Button1.png";
}
也是我的形象(按钮工作):
<input type="image" onclick="saveData()" id="coil1"/>
答案 0 :(得分:1)
虽然使用JS的想法没问题,但你可以用普通的PHP和HTML / CSS来做到这一点。 我还建议你开始使用mysqli / PDO。
/*CSS */
.button0 {background-image: url("Button1.png");}
.button1 {background-image: url("Button2.png");}
然后,要显示您的按钮,请使用以下PHP代码
<?php
include 'db_ecoheating.php';
mysql_connect($dbhost,$dbuser,$dbpass) or die ("Unable to connect to database");
mysql_select_db($dbname) or die ("Unable to select database");
$sql = "SELECT * FROM coils";
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result)){
$value = $row['coil1']; ?>
<input class="button<?php echo $value; ?>" type="image" onclick="saveData()" id="coil1"/>
<?php } ?>