how to set timer in pagination php - javascript

I want to use time in pagination.In my script when click on next the timer restarts..i think session will help me.but i don't understand how to collect correct time in session and display that in another page in pagination.This is my javascript code:
var myTime = "20";
function countDown() {
document.form.seconds.value = myTime;
if (myTime == 0)
{
location.href="abc.php";
}
else if (myTime > 0)
{
myTime--;
setTimeout("countDown()",2000);
}
}
and this is how i set the time in my php file:
<form name="form">
Time Left: <input type="text" name="seconds" size="3">
</form>

Using session is really very simple. Just include: session_start(); at the beginning of every PHP file of your script.
Then, in order to set a time variable, use this: $_SESSION['theTime'] = $currentDateTime;
where $currentDateTime = date('m/d/Y h:i:s a', time());
Now, this $_SESSION['theTime'] variable will be passed on to every PHP file if you include session_start(); at the beginning of each. To find out difference between two times, refer this: php: datetime() difference between 2 datetime with 2 variables

Related

How do I use a global session element in html script?

I want the script to read the global element, detect if its value equals 1 and if so, display a load of text but for some reason it doesn't seem to work.
var z = sessionStorage["system"]
if (z == 1) {
document.getElementById("titlesystem").innerHTML = "Your System";
document.getElementById("bodysystem").innerHTML = "Amazing!
}
<h2 id="titlesystem"></h2>
<p id="bodysystem"></p>
If I understand it right, you want to read some value stored in session (in php stored in $_SESSION) as a client from javascript.
First: session is server-side thing, clients don't know that they are in session.
But there is a way, how to send value.
This is what came to my mind:
<h2 id="titlesystem"></h2>
<p id="bodysystem"></p>
<input type="hidden" value=<?php echo $_SESSION["value"]?> id="mySessionValue">
<script>
var sessionValue = document.getElementById("mySessionValue").value;
if (sessionValue == 1)
{
document.getElementById("titlesystem").innerHTML = "Your System";
document.getElementById("bodysystem").innerHTML = "Amazing!"
}
</script>

How to call JS function with in HTML

I am working in view file of opencart. I have 2 text boxes which set date and time respectively. I have a checkbox, its value is 1 when checked and 0 when not checked.What I want is that date and time fields should be hidden if above check box is checked (Done till now).If checkbox is unchecked, show these fields as they are now(Done till now).
Current code:
HTML
<tr id="row_ship_date">
<td>
Shipment Date:
</td>
<td>
<input type="text" id="ship_date" name="ship_date" class="date" onchange="getServices();" />
</td>
</tr>
<tr id="row_ship_time">
<td>
Shipment Time:
</td>
<td>
<input type="text" id="ship_time" name="ship_time" class="time" />
</td>
</tr>
<?php
if (isset($current_date_shipment) && $current_date_shipment == 1) // check box value check{?>
<script type="text/javascript">
document.getElementById('row_ship_date').style.display='none';
document.getElementById('row_ship_time').style.display='none';
getServices();
</script>
<?php }
?>
JS:
function getServices() {
alert('test');
var ship_date = $('#ship_date').val();
var ship_time = $('#ship_time').val();
// code so on..
}
Current issues
1) I am unable to call getServices() if checkbox check is true (php check of if statement) because if its false it gets call onchange event of date and works fine.
2) how can I set current date and current time respectively for both text fields if this function is called under if statement (when both fields are hidden), something like:
function getServices() {
alert('test');
var ship_date = new Date();
var ship_time = currentTime();
// code so on..
}
The problem may be because you are trying to call the function before declaring it.
Try to wrap you code inside
<script>
// self executing function
(function() {
// put your code here
})();
</script>
i.e;
<?php
if (isset($current_date_shipment) && $current_date_shipment == 1) // check box value check
{?>
<script type="text/javascript">
(function() {
document.getElementById('row_ship_date').style.display='none';
document.getElementById('row_ship_time').style.display='none';
getServices();
})();
</script>
try this:
<?php
if (isset($current_date_shipment) && $current_date_shipment == 1) {
$now = new DateTime();
$date = $now->format('Y-m-d');
$time = $now->format('H:i');
?>
<script type="text/javascript">
$('#row_ship_date,#row_ship_time').hide();
$('#ship_date').val(<?php echo $date; ?>);// this should set date value,Use appropriate PHP function.
$('#ship_time').val(<?php echo $time; ?>); // this should set time value,Use appropriate PHP function.
getServices();
</script>
<?php } ?>
You dont need hidden fields to set any value but can set as shown above using the jQuery .val().
Just replace the "new date/time value here strings" with PHP variables containing the corresponding values or use the appropriate PHP functions to get it.
Also ensure that jQuery & your function definition of getServices exists before your PHP condition executes.
I guess your syntax has a problem. You can not invoke Javascript inside a PHP script. This part here:
<?php if (isset($current_date_shipment) && $current_date_shipment == 1) // check box value check{?>
<script type="text/javascript">
document.getElementById('row_ship_date').style.display='none';
document.getElementById('row_ship_time').style.display='none';
getServices();
// <---- notice also here you have no </script> tag :)
<?php }?>
I am just pointing out some minor syntax problem :)

Auto submit form error

I'm trying to make something like a web notepad that autosaves the changes every minute.
The notes are stored in a field "notas" for each user on the db.
Here a piece of code involved:
<html>
<head>
<script type='text/javascript'>
function submitForm(){
document.nota.submit();
}
function setTimeForSubmit(){
window.setTimeout("submitForm()",60000);//Expire after 60 Sec
}
</script>
</head>
<body onload="setTimeForSubmit()">
<form method="POST" name="nota" id="nota" action="notas.php">
<textarea name="txtNota" id="txtNota">
<?php
if(isset($_POST['txtNota'])){
$sql =
"UPDATE `login`
SET `notas`= ".$_POST['txtNota']."
WHERE login_id = ".$_SESSION['login_id']."";
$rs = $DB->Execute($sql);
}
$sql2 = "SELECT notas FROM login WHERE login_id=".$_SESSION['login_id']."";
$rs2 = $DB->Execute($sql2);
echo $rs2->fields[0];
echo $_SESSION['login_id'];
?>
</textarea>
<input type='submit' name='guardar' value="guardar"/>
</form>
</body>
</html>
I already checked every variable and query, and they are ok , but the UPDATE isn't working when I try to submit it this way. It works when done by itself, the $_POST and the $_SESSION have the correct values by themselves too, and the SELECT.
Any clues on why isn't it working? It doesn't throws any errors either
change
`notas`= ".$_POST['txtNota']."
to
`notas`= '".$_POST['txtNota']."'
you are missing '
I had a similar problem and this solution is working for me.
this uses a dirty bit and a wrapper around the form submit. This is because I ended up having several save triggers including handling destructive view switching events. And I didn't want to end up saving non change events.
Note: I think I found this solution on another SO question, maybe you can help me link it.
// PROP SHEET: save after a second using dirty bit for other save triggers
$('#props input, #props textarea, #props select').change(function() {
$('#props').attr('dirty', true);
console.log('change event fired',$(this));
delay(function(){
save_props();
}, 1000 );
});
var delay = (function(){
var timer = 0;
return function(callback, ms){
clearTimeout (timer);
timer = setTimeout(callback, ms);
};
})();

Fire Javascript alert when counter reaches 10 minutes

I have a page that updates whenever another entry is added to the database. It displays the timestamp, and then starts counting up using jQuery, along with moment.js to help with time formatting. This is the code for that:
$(function () {
var austDay = new Date('".$array['dateAdded']."');
$('#since".$array['id']."').countdown({since: austDay, significant: 1, layout: '{d<}{dn} {dl} {d>}{h<}{hn} {hl} {h>}{m<}{mn} {ml} {m>}{s<}{sn} {sl}{s>}'});
});
I need to be able to fire an alert whenever a given entry is 10 minutes past the timestamp date and time. I assume the script would need to continually check to see if that's the case. Any help would be greatly appreciated.
This is the solution I came up with for this problem, using a mix of Javascript and PHP.
$now = date("Y-m-d h:i:s");
$plusTen = date("Y-m-d, h:i:s", strtotime("+10 minutes", strtotime($array['dateAdded'])));
$difference = strtotime($plusTen) - strtotime($now);
$difference = round($difference,2) * 1000;
if($difference > 0){
echo '<script type="text/javascript">
setTimeout(function(){staleAlert();}, '.$difference.')
</script>';
}

how to pass a parameter to ajaxed loop script?

This function get a parameter and pass it to this this
$.arte({'ajax_url':'getRealTimeUpdates.php?activity_id='+id, 'on_success':updateLiveUpdates}).start();
[where this line of code feches the upadtes from server each a
specific time period as a jaxed loop]
when I call the function showLiveUpdates(id) with some parameter for example id=5, this line remember id=5 for all this function calls even different parameters !
I want each time I call the function with different id this line of code get the new id
{{ where arte is JQuery plug-in : Ajax Real Time Extension, this is its website click here}}
js code :
function showLiveUpdates(id)
{
//$.arte().stop();
$("#liveUpdates").fadeIn("slow");
$("#liveUpdates").html("");
$("#liveUpdates").append("<div><textarea rows='2' cols='49' id='txtLiveUpdates'></textarea></div>");
$("#liveUpdates").append("<div><input type='button' id='btnliveUpdatesShare' value='share' onClick='addComment("+id+","+getCookie("userId")+")'></div>");
last="";
$.arte({'ajax_url':'getRealTimeUpdates.php?activity_id='+id, 'on_success':updateLiveUpdates}).start();
}
I call it like this:
<input type='button' onClick='showLiveUpdates(<? echo $_GET["id"];?>)' />
edit
function updateLiveUpdates(data)
{
if(data!=last)
{
$("#liveUpdates").append("<div id='updates"+ii+"' style='display:none' >"+data+"</div>");
$("#updates"+ii).fadeIn();
last=data;
}
ii++;
}
getRealTimeUpdates.php
<?php
require("database.php");
$activity_id=$_GET["activity_id"];
$query = "SELECT id,comment from comments where activity_id=$activity_id order by id desc limit 0,1";
echo $query;
$result = mysql_query($query);
$row = #mysql_fetch_assoc($result);
echo $row["id"]."-".$row["comment"];
?>
How about this? Creating a function scope for the id variable:
function createData(id) {
return function () {
return {
'ajax_url':'getRealTimeUpdates.php?activity_id='+id,
'on_success':updateLiveUpdates
}
}
}
for (var id = 0; id < 5; i++) {
$.arte(createData(id)()).start();
}
EDIT 1 Just looked at the code at http://code.google.com/p/arte/source/browse/trunk/jquery-plugin-arte.js.
There is a shared _config property that gets overwritten, and the URL used is always _config['ajax_url'].
EDIT 2 I've posted a demo of this here to demonstrate your issue. So it looks like you cannot call this function multiple times, because the internal state is shared.
EDIT 3 I've updated a jsfiddle with a rough attempt at making this code work as you desire. The demo is here. The new code keeps no state and (seems to) successfully run 5 update loops before terminating them after 3 seconds.

Categories

Resources