I have this php file which I am using to control relays attached to the gpio pins of a Raspberry pi. I works well except that the icons do not update as desired when the gpio pins change state. THey do update as desired if I click the RELOAD button on the browser. I have tried using:
location.reload(true) but it only works if I put it in the html portion of the page code as its own button. I want the location.reload(true) to execute whenever I click one of the gpio icons. Can someone help me with this? Thanks in advance from a newbie.
Here is the php code I am using:
<!DOCTYPE html>
<!--TheFreeElectron 2015, http://www.instructables.com/member/TheFreeElectron/ -->
<html>
<head>
<meta charset="utf-8" />
<title>Antenna Relays</title>
</head>
<body style="background-color: black;">
<h1><p style="color:white"><font size="7">
ANTENNA SELECTOR
</p>
</font></h1>
<!-- On/Off button's picture -->
<?php
$val_array = array(0,0,0,0);
//this php script generate the first page in function of the file
for ( $i= 0; $i<4; $i++) {
//set the pin's mode to output and read them
system("gpio mode ".$i." out");
exec ("gpio read ".$i, $val_array[$i], $return );
}
//for loop to read the value
$i =0;
for ($i = 0; $i < 4; $i++) {
//if off
if ($val_array[$i][0] == 0 ) {
echo ("<img id='button_".$i."' src='data/img/red/red_".$i.".jpg' onclick='change_pin (".$i.");'/>");
}
//if on
if ($val_array[$i][0] == 1 ) {
echo ("<img id='button_".$i."' src='data/img/green/green_".$i.".jpg' onclick='change_pin (".$i.");'/>");
}
}
?>
<!-- javascript -->
<script src="script.js"></script>
</body>
</html>
Related
i am creating a blog posting page for practice.
1) i want a pop up window 'javascript' on success full data submitting to phpmyadmin.
2) same on failure a popup window.
3)where exactly we should close the mysqli connection mysqli_close(); in a isset codding.
i have tried all onclick, onsubmit, two function on one onclick but all in vain
reasons: two javascripts function were not working on one button.
while onsubmit code typed the popup window appears but data does not success fully submits to phpmyadmin.
<html>
<head>
<title>
</title>
</head>
<link href="blogsup-main.css" type="text/css" rel="stylesheet"/>
<link href="blogsup.css" type="text/css" rel="stylesheet"/>
<body onload="refresh();">
<center>
<form method="post">
Name
Category
<option>Education</option>
<option>Society</option>
<option>Politics</option>
<option>Business</option>
<option>IT</option>
<option>Book</option>
<option>Other</option>
Heading
Sub heading *optional
Blog
*Send Email
</form>
</center>
<div id="popupdiv" class="popup-area">
<div class="popup-content">
<span class="close">×</span>
<center>
Blog Created Success Fully!pending approval.
Proceed
</center>
</div>
</div>
</body>
</html>
<?php
$user='root';
$password='';
$db='blogsup';
$con=mysqli_connect('localhost',$user,$password,$db);
mysqli_select_db($con,$db);
if(isset($_POST['submit'])){
$bloggername=$_POST['bloggername'];
$category=$_POST['category'];
$heading=$_POST['heading'];
$subheading=$_POST['subheading'];
$textarea=$_POST['textarea'];
$que="insert into blogposting (bloggername,category,heading,subheading,blogdate,blog) values ('$bloggername','$category','$heading ','$subheading', now(),'$textarea')";
$run=mysqli_query($con,$que);
if($run){
echo '<script type="text/javascript">
var popupdiv = document.getElementById("popupdiv");
popupdiv.style.display = "block";
return false;
</script>';
} else{
echo"Failed";
}
}
mysqli_close($con);
?>
<script type="text/javascript">
var nametextbox = document.getElementById('nametextbox');
var heading= document.getElementById('heading');
var blog = document.getElementById('blog');
var span = document.getElementsByClassName('close')[0];
function refresh(){
nametextbox.value='';
heading.value='';
subheading.value='';
blog.value='';
}
span.onclick = function() {
popupdiv.style.display = "none";
nametextbox.value='';
heading.value='';
subheading.value='';
blog.value='';
message.innerHTML = '';
}
</script>
When you submit a form there is no success or failure.You shouldn't add php and html and js code in the same page.Add the php code to php file which will be your action
e.g
<form action="anyfile.php" method="post">
In the php file add your business logic, like connect to db, execute queries and close the db connection.when the insert query is executed without errors then send a redirect back (yourMainPage?message='ok') to show a successful message like
<?php if(isset($_GET('message') == 'ok'){ ?>
<p>Data successfully inserted</p>
<?php } ?>
Also, your code should be more tidy. Hope i helped.
I'm learning Ajax using PHP as back-end. What I'm trying is fetching and updating the table values from database using setTimeout in JavaScript.
Here's my code:
(I've explained the problem below)
A. ex1.php
<?php
$a=mysqli_connect("localhost", "root", "", "ndb");
$query="SELECT name from tab3";
$queryrun=mysqli_query($a, $query);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="jquery-2.2.0.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script>
var update= setTimeout(myFunc2, 4000);
function myFunc2(){
var yhttp= new XMLHttpRequest();
yhttp.onreadystatechange=function(){
//if(yhttp.readyState==4 && yhttp.status==200)
};
yhttp.open("GET", "exresponse2.php", true);
yhttp.send();
}
</script>
<script>
var cmp= setTimeout(cmpFunc, 100);
function cmpFunc(){
var h="";
var h2="";
var cmp2= setTimeout(cmp2Func, 2000);
function cmp2Func(){
h= '<?php $height=mysqli_num_rows($queryrun); echo $height; ?>';
}
var cmp3 =setTimeout(cmp3Func, 8000);
function cmp3Func(){
h2= '<?php $height2=mysqli_num_rows($queryrun); echo $height2; ?>';
if(h==h2)
{
alert(h+" "+h2);
}
else
{
alert("Not same");
}
}
}
</script>
</head>
<body>
<div id="id1" style="float: left; width: 300px">
The names are displayed below:</div>
<div id="id2" style="float: left; width: 200px">
<button onclick="myFunc2()">Submit</button>
</body>
</html>
B. exresponse.php
<?php
$a=mysqli_connect("localhost", "root", "", "ndb");
$query="SELECT name from tab3";
$queryrun=mysqli_query($a, $query);
$names=array();
while($row=mysqli_fetch_assoc($queryrun))
{
$names[]= $row["name"];
}
$x="";
$count=0;
for($x=0; $x<25; $x++)
{
echo "Name: ".$names[$x];
echo "<br>";
}
?>
<html>
<head>
<script src="jquery-2.2.0.js"></script>
</head>
<title></title>
<body>
</body>
</html>
As you can see, when the page ex1.php is opened on browser, after 100 micro seconds cmpFunc executes, then after 2 seconds cmp2Func executes and variable h is assigned some value; and after 2 more seconds myFunc2 executes (which updates table on database). After that, cmp3Func executes and variable h2 is assigned value.
But every time, only if statement executes. But actually the table should be updated between h and h2 are assigned values, and they should have DIFFERENT values. Am I doing something wrong here?
The reason h and h2 are always the same is that when ex1.php is first rendered to the client, the values for the javascript are hardcoded for h and h2 at that instant in time, so they are the same every time you call that js function later.
I think a solution would involve moving the sql block for $query="SELECT name from tab3"; to another server page that you would then hit with an ajax call inside your cmp3Func function.
I would like to display a popup message when user is logged out, so I use
echo "<script>alert(\"You are logged out\");</script>";
But it doesn't work.
Below is my coding. Is there any logic problem in my coding?
<?php
session_start();
if(isset($_SESSION['Username']) == "admin")
{
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<style type="text/css">
#import "../CSS/Style.css";
#import "../CSS/Admin.css";
</style>
<title>Admin Home Page</title>
</head>
<body>
<div class="body"></div>
<?php
if(isset($_GET['id']) == "logout")
{
session_destroy();
echo "<script>alert(\"You are logged out\");</script>";
header("Location: ..\Main.php");
}
else
{
?>
<div class="menu">
Manage Staff
</div>
<div class="menu2">
Manage Account
</div>
<div class="logout">
Logout
</div>
<?php
}
}
else
{
?>
<center>
<p style="font-size:50px; font-weight:bold">Access Denied</p>
<p style="font-size:18px">Your request for this page has been denied because of access control</p>
</center>
<?php
}
?>
</body>
</html>
The session will be destroyed and will also redirect to Main.php, just the alert() will not come out.
You're doing an echo and then writing a relocate header. If you did your relocate in the javascript (after the user clicked the alert), it would probably work the way you expect it to.
echo "<script>alert('You are logged out'); window.location.href='..\Main.php';</script>";
Also, the way that you use isset will cause problems because isset returns true or false (it checks if a value is present), rather than returning the value.
So instead of
if(isset($_SESSION['Username']) == "admin")
You need to do:
if(isset($_SESSION['Username']) && $_SESSION['Username'] == "admin")
header("Location: ..\Main.php"); tells the browser to go to another page before it even shows the page... if you want the user to see the alert, try this:
session_destroy();
echo "<script>";
echo "alert('You are logged out');";
echo "window.location = '../Main.php';"; // redirect with javascript, after page loads
echo "</script>";
use this it will solve your problem!! first change your code from
if(isset($_SESSION['Username']) == "admin")
{
to
if(!empty($_SESSION['Username']) && ($_SESSION['Username']=="admin")){
and than use following code
if(!empty($_GET['id']) && ($_GET['id']=="logout"))
{
session_destroy();?>
<script>
alert("You are logged out");
window.location.href='..\Main.php';
</script>
<?php }?>
Try,
This must work,
And remove php header, replace with the following code.
echo "<script>alert('You are logged out');
location.href='..\Main.php';
</script>";
I have a button that allows authentication from twitter, everything works properly, but the only issue is that i wish to open the login-twitter.php page on a different tab, i tried using window.open in place of header but it didn't work. can anyone tell how it can be done
<?php
ob_start();
session_start();
if (isset($_SESSION['id'])) {
header("location: u_tasks.php");
}
if (array_key_exists("login", $_GET))
{
$oauth_provider = $_GET['oauth_provider'];
if ($oauth_provider == 'twitter')
{
header("Location: login-twitter.php");
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head></head>
<body>
<?
echo "<div class='col-md-9'>";
echo "<a href='?login&oauth_provider=twitter'><button style='background-color:#1dcaff; border-color:#1dcaff; color:white; height:30px; border-radius:10px;'>Go To Twitter</button></a>";
echo "</div>";
?>
</body>
</html>
i think it is helpful to you.
<button style='background-color:#1dcaff; border-color:#1dcaff; color:white; height:30px; border-radius:10px;'>Go To Twitter</button>
using header("Location:..."); can only do redirects. As far as I know, you'll have to do some amount of javascript or html to achieve your goal. Another problem is that browsers like to block automatically opened popups because 99% of the time automatic popups are used only by spammers. However, I'll show you a way to attempt to do this anyways:
<?php
$usingTwitter=0;
ob_start();
session_start();
if (isset($_SESSION['id'])) {
header("location: u_tasks.php");
}
if (array_key_exists("login", $_GET))
{
$oauth_provider = $_GET['oauth_provider'];
if ($oauth_provider == 'twitter')
{
$usingTwitter=1;
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<script>
<?php
if($usingTwitter){
echo "window.open('login-twitter.php');";
}
?>
<script>
</head>
<body>
<?
echo "<div class='col-md-9'>";
echo "<a href='?login&oauth_provider=twitter'><button style='background-color:#1dcaff; border-color:#1dcaff; color:white; height:30px; border-radius:10px;'>Go To Twitter</button></a>";
echo "</div>";
?>
</body>
</html>
Make sure you turn off popup blocking in your browser for this to work though!
I have a client who needs to output pages that are referenced by ID in a database. The pages must be output one after another (like a slide show), and there must be a slider transition effect. To make it "fun", the database will be changing as new pages are added, so a portion of the script to do all this must be run repeatedly to check the database.
I've got the pages to load, and I've got the transition working. The problem is, I can't get the polling to work because there's such a mess. That is, if I add a new page, I must manually reload the script - I need the script to detect new pages added to the database. I know that all I really need to do is just re-run the database query to generate the array, but with all the other stuff going on, I'm lost. I've tried setInterval() and even a while loop to no avail. At this point I'm sure I'm just missing something (hopefully) trivial, so I humbly ask for help. My code is VERY sloppy as I test, but here it is:
<?php
$community_id = 89;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
<?php
$con=mysqli_connect("localhost","username","password","db_name");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// get all the pages that pertain to the community
$uid_query = "SELECT uid FROM pages WHERE pid=" . $community_id;
// build the javascript array by grabbing all results from above query
$result = mysqli_query($con,$uid_query);
$counter = 0;
$baseURL = "http://www.website.com/index.php?id=";
echo "var frames = new Array;";
while($row = mysqli_fetch_array($result)) {
echo "frames[" . $counter . "]='" . $baseURL . $row['uid'] . "';";
$counter++;
echo "frames[" . $counter . "] = 5;";
$counter++;
}
mysqli_close($con);
?>
var i = 0, len = frames.length;
function ChangeSrc()
{
if (i >= len) { i = 0; } // start over
document.getElementById('frame').src = frames[i++];
setTimeout('ChangeSrc()', (frames[i++]*1000));
}
window.onload = ChangeSrc;
</script>
<style type="text/css">
body, html
{
margin: 0; padding: 0; height: 100%; overflow: hidden;
}
#iframe_container
{
position:absolute; left: 0; right: 0; bottom: 0; top: 0px;
}
</style>
</head>
<body>
<div id="iframe_container">
<iframe src="" name="frame" id="frame" width="100%" height="100%" frameborder="0"></iframe>
</div>
</body>
</html>
... and then any given page looks like this:
javascript:
$(document).ready(function(){
$('.hidden').slideDown(1000);
});
css:
.hidden{
display:none;
}
html:
<div class="hidden">
CONTENT GOES HERE
</div>