-3

所以我想要一个更新一个值并将其更改为“HIGH”的按钮,另一个更新相同的值并将其更改为“LOW”。这两个函数单独工作,但是当在同一个文件中时,只有输入首先出现的函数才起作用。这听起来可能令人困惑。如果您查看下面的代码,您可以看到有两个输入。如果 on 函数被第二个输入调用,它就可以工作。如果它被第二个输入函数调用,则不会。

<?php

function off(){
    mysql_connect("localhost", "root", "");
    mysql_select_db("arduino");
    mysql_query("UPDATE `leds` SET `state` = 'LOW' WHERE `id` = 13");
}

function on(){
    mysql_connect("localhost", "root", "");
    mysql_select_db("arduino");
    mysql_query("UPDATE `leds` SET `state` = 'HIGH' WHERE `id` = 13");
}



?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link rel="stylesheet"   href="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.css" />
    <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
    <script src="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.js">
</script>
    <title></title>
</head>
<body>
<input type = "button" value = "Turn On" onclick = "<?php on(); ?>">
<br />
    <br />
    <input type = "button" value = "Turn Off" onclick = "<?php off(); ?>">
</body>
</html>

我对此感到非常困惑。任何人都知道发生了什么。如果我不够清楚,请说出来,我很乐意提供更多细节。

4

2 回答 2

1

由于 raina77ow 和 Pekka 指出的原因,这将不起作用。

但这可能:

HTML(客户端):

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link rel="stylesheet"   href="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.css" />
    <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
    <script src="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.js">
</script>
    <title></title>
</head>
<body>
<script>
function set(bit_status){
    $.get('handle.php', {status: bit_status});
}
</script>
<input type = "button" value = "Turn On" onclick="set('HIGH')">
<br />
    <br />
    <input type = "button" value = "Turn Off" onclick="set('LOW'">
</body>
</html>

PHP(服务器端)handle.php 文件:

function off(){
    mysql_connect("localhost", "root", "");
    mysql_select_db("arduino");
    mysql_query("UPDATE `leds` SET `state` = 'LOW' WHERE `id` = 13");
}

function on(){
    mysql_connect("localhost", "root", "");
    mysql_select_db("arduino");
    mysql_query("UPDATE `leds` SET `state` = 'HIGH' WHERE `id` = 13");
}

$status = $_GET['status'];
if($status == 'HIGH') on();
else off();
于 2013-09-07T22:12:46.410 回答
0

您的输入标签都没有在表单内,也没有关闭。我不确定这是否是问题所在,但这无济于事。

于 2013-09-07T22:07:31.860 回答