[英]How to get control back from a php script to my main page?
I have a javascript code in my main HTML page Smartmeter.html by which I am displaying meter data which is getting changed every 5s. 我的主要HTML页面Smartmeter.html中有一个javascript代码,通过它我可以显示每5秒更改一次的仪表数据。
Now I need to insert this meter data which is getting changed in the javascript function into mysql via php every 30s. 现在,我需要每30秒钟通过php将仪表功能中正在更改的仪表数据插入到mysql中。
Means I need to stay on the Smartmeter.html page as the javascript function is getting executed 5s and in the meantime also insert this meter data(variable--target) into mysql every 30s. 意味着我需要留在Smartmeter.html页面上,因为javascript函数将在5s内执行,与此同时,还应每30s将这个电表数据(变量目标)插入mysql。
How can I achieve this? 我该如何实现? Can someone help me with some code or suggestions.
有人可以帮我一些代码或建议吗?
My Smartmeter.html page: 我的Smartmeter.html页面:
<html> <head></head> <script src="http://code.jquery.com/jquery-1.9.1.js"></script> <script type="text/javascript"> window.onload=dynamic; var target=100; function dynamic() { var1=Math.random(); target=target + var1 * 1000; target=Math.round(target); if(target>123457) { target=15647; } document.getElementById('hidden').value = target; func(); } function func() { //configuration of the reading displayed var primary = document.getElementById("d").getElementsByClassName("imge")[0].getElementsByTagName("h2")[0]; primary.innerHTML=target; alert('test'); document.forms["myForm"].submit(); // window.location.href = "http://localhost/SUA/Data_insertion.php?target=" + target; setTimeout(dynamic, 5000); } </script> <body> <style> .imge { position: absolute; top: 40%; left: 30%; margin-top: -150px; margin-left: -150px; } h2{ position: absolute; top: 116px; left: 165px; width: 100%; } p { position: absolute; top: 120px; left: 250px; width: 100%; } input { display: inline-block; float: bottom; } </style> <body bgcolor="#BDBDBD"> <form action="Data_insertion.php" method="post" id="myForm"> <input type="hidden" id="hidden" name="reading" value=""><br> <input type="submit" value="submit"> </form> <div id="d"> <div class="imge"> <img src="meter.jpg" width="450" height="350" alt="" /> <h2>1234578 </h2> <p><font size="5">kWh</font></p> </div> </div> </body> </html>
jQuery-Solution: jQuery解决方案:
function submitValue(url, key, value){
$.ajax({
type: "POST",
url: url,
data: {
key: key,
value: value
},
success: function(data){
alert("done: "+data);
},
error: function (xhr, ajaxOptions, thrownError) {
alert('Error');
}
});
}
Process your data like this: 像这样处理数据:
<?php
switch($_POST['key']){
case "voltage": setVoltage($_POST['value']); break;
case "current": setCurrent($_POST['value']); break;
default: http_response_code(404); die() break;
}
echo "successful set ".$_POST['key']." to ".$_POST['value'];
function setVoltage($voltage){
/* sql magic */
}
function setCurrent($current){
/* sql magic */
}
?>
EDIT 编辑
For your Problem this simple version should work: 对于您的问题,此简单版本应适用:
function sendValue(){
$.ajax({
type: 'POST',
url: 'Data_insertion.php',
data: {
reading: target
},
success: function(data){
alert('done');
},
error: function (xhr, ajaxOptions, thrownError) {
alert('Error');
}
});
}
function init(){
// remove this line from function func()
setTimeout(dynamic, 5000);
setTimeout(sendValue, 30000);
}
and Change 和变化
<body bgcolor="#BDBDBD">
<form action="Data_insertion.php" method="post" id="myForm">
<input type="hidden" id="hidden" name="reading" value=""><br>
<input type="submit" value="submit">
</form>
to 至
<body bgcolor="#BDBDBD" onLoad="init()">
<button onClick="sendValue(target)">Submit</button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.