i have this code countdown javascript is work fine :
<div id="countdown"></div>
<script type="text/javascript">
$(document).ready(function() {
$('#countdown').countdown('2017/07/11 06:32:11', function(event) {
$(this).html(event.strftime('%H:%M:%S'));
});
});
</script>
but after get the time from database don't work this is the code after get time from database countdown don't show in id countdown
<?php
$auc = $DB_con->prepare("SELECT * FROM `auction` WHER ORDER BY id DESC LIMIT 1");
$auc->execute();
$row = $auc->fetch(PDO::FETCH_ASSOC);
?>
<div id="countdown"></div>
<script type="text/javascript">
$(document).ready(function() {
$('#countdown').countdown(<?php echo $row['timeauc']; ?>, function(event) {
$(this).html(event.strftime('%H:%M:%S'));
});
});
</script>
You have a pair of quotes missing:
$('#countdown').countdown("<?php echo $row["timeauc"]; ?>", function(event) {
$(this).html(event.strftime('%H:%M:%S'));
});
But, to avoid a potential cross-site scripting vulnerability, you should escape the value instead:
$('#countdown').countdown(<?php echo json_encode($row["timeauc"]); ?>, function(event) {
$(this).html(event.strftime('%H:%M:%S'));
});
If nothing is showing up, first make sure $row["timeauc"] actually contains the date string:
var_dump($row["timeauc"]);
Related
Can anyone help me. I’m trying to make jquery go to function while click on a div. Function works but it goes always on the same ID, first one.
Here is the code:
<?php do { ?>
<div class=“editNews”></div>
<script>
$(function() {
$(“.editNews”).on(“click touchstart”, function(e) {
Here I would like to execute jquery and than go to
window.location = “inserNews.php?id=<?php echo $row_rsNews[“NewsID”]; ?>;
});
e.stopPropagation()
});
});
</script>
<?php } while ($row_rsNews = mysqli_fetch_assoc($rsNews))?>
Thank you very much
I'm using the turn.js to make a catalog. I grab the pages from the database with PHP and I want to make some control buttons like search specific page. How can I parse the value from the searchbox to take the place of number 10 here $("#flipbook").turn("page", 10); and run it?
<div class="flipbook-viewport">
<div class="container1">
<div class="flipbook">
<?php
if (isset($_GET['eb_catalogs_id'])){
$catalogos_id = $_GET['eb_catalogs_id'];
$query= "SELECT c_catalog_name FROM eb_catalogs WHERE c_id = $catalogos_id";
$test= $conn->query($query);
$catalogname =$test->fetchColumn();
$query1 = ("SELECT * FROM eb_catalog_".$catalogname."");
$pages = $conn->query($query1);
$i = 1;
foreach($pages as $page){
echo "<div class='p".$i."' style='background-image:url(https://untuneful-carload.000webhostapp.com/img/catalogs/".$catalogname."/".$page['eb_catalog_imgs'].")'></div>";
$i++;
}}else{
echo "nothing";
}
?>
</div>
</div>
</div>
<script type="text/javascript">
function loadApp() {
// Create the flipbook
$('.flipbook').turn({
width:922,
height:600,
elevation: 50,
});
}
// Load the HTML4 version if there's not CSS transform
yepnope({
test : Modernizr.csstransforms,
yep: ['js/turn.js'],
nope: ['js/turn.html4.min.js'],
both: ['css/basic.css'],
complete: loadApp
});
alert("The current page is: "+$("#flipbook").turn("page"));
</script>
here is the code of the page so far.. I know how to make an input field but I dont know how to parse the value of the input field to the function that jumps to the specific page the user wants.
If you pass the page number to next page, you may get it using $_post or $_get and execute it on page load:
<script>
$(window).on("load",function(){
$("#flipbook").turn("page", <?php echo ($_GET["page"]) ?>);
})
</script>
If you are in the same page and just want to flip the book, you can easily get the number using val():
<input id="pageNumber">
<button onclick="flipPage()">GO</button>
<script>
function flipPage(){
var pageNumber=$('#pageNumber').val();
$("#flipbook").turn("page", pageNumber);
}
</script>
I know that this might be answered already by someone, but all the answers I could find, are intended for people who already know what they are doing and they paste thousands of lines of code, so I want something more on a begginer's side.
I have a value that I want to update ever 15 seconds to show the most updated information from a mysql db. I managed to show the latest result every once you reload the webpage.
The code is the following (everything is on the same file index.php):
PHP part:
<?php
$link = mysql_connect('localhost', 'root', '');
$LastUpdate = mysql_query('SELECT dateTime FROM LastUpdate where id=1');
if (!$LastUpdate) {
die('Could not query:' . mysql_error());
}
HTML part:
<small id="result">Last Update: <?php echo mysql_result($LastUpdate, 0); ?></small>
Jscript part:
<script>
setInterval(function() {
var date='Last update: '+'<?php echo mysql_result($LastUpdate, 0); ?>';
document.getElementById("result").innerHTML = date;
}, 1000);
The script part is supposed to refresh the "result" div every 1000 milliseconds. The problem is that once the script is performed one time the value gotten from the query doesn't change, having to reload the page in order for it to refresh.
First off all. Client asks Server for Webpage -> The Server build your Page -> The Browser get the response and render your HTML page. So far so good. Your <?php echo mysql_result($LastUpdate, 0); ?> always has the same value. You have to make an AJAX call and please don't use mysql functions anymore. There are deprecated (https://secure.php.net/manual/de/function.mysql-connect.php). Use PDO instead and always escape your statements to prevent sql injections or use an ORM. Here is a quick and dirty solution:
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test</title>
<script src="https://code.jquery.com/jquery-3.0.0.min.js" integrity="sha256-JmvOoLtYsmqlsWxa7mDSLMwa6dZ9rrIdtrrVYRnDRH0=" crossorigin="anonymous"></script>
<script>
function ajaxCall() {
$.ajax({
url: "ajax.php",
dataType: "json"
}).done(function (data) {
$(this).addClass("done");
var date = 'Last update: ' + data.firstData.dateTime + ' - count: '+ data.secondData.count;
document.getElementById("result").innerHTML = date;
});
}
$(function () {
ajaxCall();
setInterval(ajaxCall, 1000);
});
</script>
</head>
<body>
<small id="result"></small>
</body>
</html>
ajax.php
<?php
$link = mysql_connect('localhost', 'root', '');
mysql_select_db('testdatabase', $link);
$LastUpdate = mysql_query('SELECT dateTime FROM LastUpdate where id=1') or die(mysql_error());
$firstData = mysql_fetch_assoc($LastUpdate);
$count = mysql_query('SELECT COUNT(Name) AS count FROM users, LastUpdate WHERE lastConnection = dateTime') or die(mysql_error());
$secondData = mysql_fetch_assoc($count);
$returnValues = array(
'firstData' => $firstData,
'secondData' => $secondData
);
echo json_encode($returnValues);
response example (JSON)
{
"firstData": {
"dateTime": "2016-06-16 00:00:00"
},
"secondData": {
"count": "1"
}
}
I am trying to reload a page and retrieve data automatically after 10 seconds or preferably less than that. Please i tried so many codes and it didn't work.
Here is what i am using now...
// script //
<script>
$(document).ready(function(){
// setInterval(function() {
// $("#load_msgs").load("load.php");
// }, 10000);
//var updateInterval = setInterval(function() {
//$('#load_msgs').load('load.php?p=<?php echo $username; ?>');
//},1000);
//});
var autoLoad = setInterval(
function ()
{
$('#load_msgs').load('load.php').fadeIn("slow");
}, 10000);
});
</script>
// php / html div id panel
<div class = 'container me' id = "load_msgs"> </div>
// php load.php file
<?php
include('config.php');
include('func.php');
//$username = $_GET['q'];
$o = "--DATABASE QUERY--";
$z = mysql_query($o) or die(mysql_error());
while ($roow = mysql_fetch_array($z)) {
$date = $roow['date'];
echo $roow['message']. " <span class = 'lil'>" . time_elapsed_string($date)."</span> \n <br />";
}
?>
I'm assuming from your comments that you can navigate directly to load.php and the data is echo'd. If that is the case, here's how I would set it up:
(It's not clear if you trying to load a file every X seconds, or just once after X seconds, Here is an example of both)
index.php:
<?php
$username='someUser'
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Load From PHP</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
var username = encodeURI( <?php echo isset($username) ? "'".$username."'" : ''; ?> );
$(function(){
var url='load.php?username='+username;
// load once after 2 seconds
setTimeout(function(){ $.get(url, function(response){ $('#load_msgs').html(response); } ); }, 2000);
// load every 2 seconds
setInterval(function(){ $.get(url, function(response){ $('#load_msgs_repeat').append(response+'<br>'); } ); }, 2000);
});
</script>
</head>
<body>
<h4>Div loaded once</h4>
<div id="load_msgs"></div>
<br>
<h4>Div loaded every 2 seconds</h4>
<div id="load_msgs_repeat"></div>
</body>
</html>
load.php:
<?php
$username= isset($_GET['username']) ? $_GET['username'] : null;
echo $username ? 'here is some data loaded from load.php for user: '.$username : 'here is some data loaded from load.php, but no user was provided' ;
?>
Here is a functioning example
The problem may be in config.php or func.php. Are you connecting to the database before running your query?
Add these two lines to the start of load.php:
ini_set("display_errors", "On");
error_reporting(E_ALL);
Then visit load.php in your browser. You should get some helpful error messages.
window.setTimeout(function(){window.location.replace("page.php")},5000);
I would like to use a value from PHP in JavaScript, but it's not working. I fetch data from a database and store it in a PHP variable and for conditional formatting. I need to also use the data in JavaScript for validation.
The value required by me is $date = date('Y-m-d'); If I pass the value by json Encode the alert function is not working...
$(document).ready(function()
{
alert("Hi");
var array = php print(json_encode($date));
});
Whereas if I just alert the function without...
var array = php print(json_encode($date)); ;
...it works fine.
<script type="text/javascript" src="assests/js/jquery-1.10.2.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
alert("Hi");
var array = '<?php print(json_encode($date)); ?>';
});
</script>
</head>
<body>
<form action="majorprocess.php">
<?php
$dbconn = mysql_connect('localhost', 'root','' );
if(!$dbconn)
{
die(mysql_error());
}
else
{
$date = date('Y-m-d');
<script type="text/javascript" src="assests/js/jquery-1.10.2.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
alert("<?php echo json_encode(date('Y-m-d')); ?>");
});
</script>
What is the resulting HTML?
There could be a PHP error outputting instead of valid javascript.
Try putting the PHP section at the top maybe?
When asking a question it is always best to post any outputs you are getting so people can help.
try this one
</head>
<body>
<form action="majorprocess.php">
<?php
$dbconn = mysql_connect('localhost', 'root','' );
if(!$dbconn)
{
die(mysql_error());
}
else
{
$date = date('Y-m-d');
}
?>
<script type="text/javascript" src="assests/js/jquery-1.10.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var array = '<?php print(json_encode($date)); ?>';
alert(array);
});
</script>
You are using the PHP $date variable before setting its value. Make sure to move the php block before you try to output the date:
<?php
$dbconn = mysql_connect('localhost', 'root','' );
if(!$dbconn)
{
die(mysql_error());
}
else
{
$date = date('Y-m-d');
}
?>
<script type="text/javascript" src="assests/js/jquery-1.10.2.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
alert("<?php echo json_encode($date) ?>");
});
</script>