Salve...
Ho una pagina per comandare Arduino: http://www.abbici.org/reloadter/
Arduino non centra nulla sul problema.
Il problema riguarda il rinnovo pagina premendo un tasto (Form).
Praticamente cambia pagina... strano perché con i target sta in regola.
-
index.php
Codice PHP:
<html>
<head>
<title>Auto Refresh Div Content Using jQuery and AJAX</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<style>
body
{
margin:0;
padding:0;
background-color:#f1f1f1;
}
.box
{
width:500px;
border:1px solid #ccc;
background-color:#fff;
border-radius:5px;
margin-top:100px;
}
#load_posts
{
padding:16px;
background-color:#f1f1f1;
margin-bottom:30px;
}
#load_posts p
{
padding:12px;
border-bottom:1px dotted #ccc;
}
</style>
</head>
<body>
<div class="container box">
<br />
<br />
<div id="load_posts"></div>
<!-- Refresh this Div content every second!-->
<!-- For Refresh Div content every second
we use setInterval() !-->
</div>
</body>
</html>
<script>
$(document).ready(function(){
$('#post_button').click(function(){
var post_name = $('#post_name').val();
//trim() is used to remover spaces
if($.trim(post_name) != '')
{
$.ajax({
url:"post.php",
method:"POST",
data:{id:id,login:login,valore:valore},
dataType:"text",
success:function(data)
{
$('#post_name').val("");
}
});
}
});
setInterval(function(){//setInterval() method execute on every interval until called clearInterval()
$('#load_posts').load("display.php").fadeIn("slow");
//load() method fetch data from fetch.php page
}, 1000);
});
</script>
display.php
Codice PHP:
<?php
//fetch.php
$connect=mysqli_connect("xxxx.xxx.xxx.xxx","login","password","bancadati");
$query = "SELECT * FROM robot;";
$result = mysqli_query($connect, $query);
if(mysqli_num_rows($result) > 0)
{
$query = "SELECT * FROM robot ORDER BY id DESC";
$result = mysqli_query($connect, $query);
if(mysqli_num_rows($result) > 0)
{
echo "<center>\n";
echo "<table border=1>\n";
while($row = mysqli_fetch_array($result))
{
echo "<tr>\n";
echo "<td>\n";
switch ($row["tipo"]) {
case 0:
if($row["valore"] == 0):
echo $row["nome"].": SPENTO";
//echo " - ". $row["id"];
echo "</td>\n";
echo "<td align=\"rigth\" colspan=\"2\">\n";
echo "<form id=\"". $row["id"] ."\" action=\"post.php\" method=\"post\">\n";
echo "<input type=\"hidden\" id=\"". $row["id"] . "\" name=\"login\" value=\"". $row["log"] ."\">\n";
echo "<input type=\"hidden\" id=\"". $row["id"] . "\" name=\"id\" value=\"". $row["id"] ."\">\n";
echo "<input type=\"hidden\" id=\"". $row["id"] ."\" name=\"valore\" value=\"1\">\n";
echo "<button type=\"submit\" id=\"". $row["id"] ."\" width=\"210\" class=\"btn btn-info\" >Accendi\n</button>\n";
echo "</form>\n";
echo "</td>\n";
else:
echo $row["nome"].": ACCESO";
//echo " - ". $row["id"];
echo "</td>\n";
echo "<td align=\"rigth\" colspan=\"2\">\n";
echo "<form id=\"". $row["id"] ."\" action=\"post.php\" method=\"post\">\n";
echo "<input type=\"hidden\" id=\"". $row["id"] . "\" name=\"login\" value=\"". $row["log"] ."\">\n";
echo "<input type=\"hidden\" id=\"". $row["id"] . "\" name=\"id\" value=\"". $row["id"] ."\">\n";
echo "<input type=\"hidden\" id=\"". $row["id"] ."\" name=\"valore\" value=\"0\">\n";
echo "<button type=\"submit\" id=\"". $row["id"] ."\" width=\"210\" class=\"btn btn-info\">Spegni\n</button>\n";
echo "</form>\n";
echo "</td>\n";
endif;
break;
case 1:
echo $row["nome"]." = ".$row["valore"];
$tmA = $row["valore"] - 1;
$tmB = $row["valore"] + 1;
//echo " - ". $row["id"];
echo "</td>\n<td align=\"rigth\">\n";
echo "<form id=\"". $row["id"] ."\" action=\"post.php\" method=\"post\" >\n";
echo "<input type=\"hidden\" id=\"". $row["id"] . "\" name=\"login\" value=\"". $row["log"] ."\">\n";
echo "<input type=\"hidden\" id=\"". $row["id"] . "\" name=\"id\" value=\"". $row["id"] ."\">\n";
echo "<input type=\"hidden\" id=\"". $row["id"] . "\" name=\"valore\" value=\"". (string)$tmA ."\">\n";
echo "<button type=\"submit\" id=\"". $row["id"] ."\" width=\"100\" class=\"btn btn-info\">\n-</button>\n";
echo "</form>\n";
echo "</td>\n<td align=\"rigth\">\n";
echo "<form id=\"". $row["id"] ."\" action=\"post.php\" method=\"post\" >\n";
echo "<input type=\"hidden\" id=\"". $row["id"] . "\" name=\"login\" value=\"". $row["log"] ."\">\n";
echo "<input type=\"hidden\" id=\"". $row["id"] . "\" name=\"id\" value=\"". $row["id"] ."\">\n";
echo "<input type=\"hidden\" id=\"" . $row["id"]."\" name=\"valore\" value=\"". (string)$tmB ."\">\n<button type=\"submit\" id=\"". $row["id"] ."\" width=\"100\" class=\"btn btn-info\">+</button>\n";
echo "</form>\n";
echo "</td>\n";
break;
case 2:
break;
//echo "</tr>";
}
echo "</tr>\n";
}
echo "</table>\n";
echo "</center>\n";
}
}
?>
post.php
Codice PHP:
<?php
$login = $_POST["login"];
//$user = $_POST['user'];
//echo $nome;
$link=mysqli_connect("xxx.xxx.xxx.xxx","login","password");
mysqli_select_db($link,"bancadati");
$id=$_POST["id"];
$valore=$_POST["valore"];
if ($id != 0)
{
$sql="UPDATE robot set valore = '$valore' where id = '$id'";
if($link->query($sql)===TRUE){
//echo "DATA updated";
}
}
?>
VIPREGOAIUTATEMIIIIIIII
@}-,-'-----
Gianfranco