【发布时间】:2014-12-01 14:15:56
【问题描述】:
我正在尝试通过 ajax 和 php 在串行设备(arduino)上启动一个功能的按钮,但似乎无法弄清楚。
这是我的html:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript">
$(function() {
$('#contact_form').on('submit', function (e) {
e.preventDefault();
$.ajax({
type: "POST",
url: '/test/SubmitFormWORefresh.php',
data: $('#contact_form').serialize(),
success: function() {
alert('form was submitted');
}
});
return false;
});
});
</script>
<meta charset="utf-8">
<title>Enroll</title>
</head>
<div id="contact_form">
<form name="contact" action="">
<fieldset>
<input type="submit" name="rcmd" class="button" id="submit_btn" value="Enroll" /><br />
</fieldset>
</form>
</div>
这是我的 php:
<?php
$verz="1.0";
$comPort = "/dev/ttyACM0"; /*change to correct com port */
$PHP_SELF="index.php"; //This php file locate it from root
if (isset($_POST["rcmd"])) {
$rcmd = $_POST["rcmd"];
switch ($rcmd) {
case Stop:
$fp =fopen($comPort, "w");
sleep(2);
fwrite($fp, 1); /* this is the number that it will write */
fclose($fp);
break;
case Enroll:
$fp =fopen($comPort, "w");
sleep(2);
fwrite($fp, 3); /* this is the number that it will write */
fclose($fp);
break;
default:
die('Crap, something went wrong. The page just puked.');
}/*end switch case*/
}/*end if statement*/
?>
当我运行它时,我得到一个对话框,说表单已提交,但串行设备没有响应它。任何帮助将不胜感激。
【问题讨论】:
-
将
case Stop:更改为case "Stop":和case Enroll:的相同内容 -
输入类型submit不在jquery的序列化函数中!
-
好吧,我不知道!对不起,我只是在拼凑一些东西。
-
我什至应该使用序列化功能将提交发送到php吗?
标签: javascript php jquery ajax arduino