How to setInterval on php? - javascript

echo '
<script type="text/javascript">
setInterval( myfunction , 1000);
myfunction(){
window.location = "../index.php";
}
</script>';
if I'm just call window.location = "../index.php" without setInterval it really works I have seen many codes but no one can help me thank you.

If you want to redirect or reload webpage after certain amount of time then event can only be triggered form the client side. You can achieve this using java script and html meta tags.
1: Using HTML
<meta http-equiv="refresh" content="0";url=http://www.domain.com/new-page.html">
you can set the value of content to the time
2: Using Javascript
<script type="text/javascript">
setInterval(function(){
window.location = "../index.php";
} , 1000);
</script>

Redirect the page after 1 second. It runs one time
<script type="text/javascript">
setTimeout(function(){
window.location = "../index.php";
}, 1000);
</script>
If you write in php, use it
<?php
//code here
?>
<script type="text/javascript">
setTimeout(function(){
window.location = "../index.php";
}, 1000);
</script>
<?php
//code here
?>
or use setInterval instead of setTimeout

Related

How to redirect after reCAPTCHA verification?

I am using Google reCAPTCHA on my site. If the captcha is success I want to redirect to another website.
The code I used is:
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
<script type="text/javascript">
if (grecaptcha.getResponse() == "success") {
function Redirect() {
window.location = "example.com";
}
document.write("You will be
redirected to a new page in 5 seconds "); setTimeout('Redirect()',
1000);
}
} else {
alert("You can't proceed!");
</script>
<body>
<div data-type="image" class="g-recaptcha" data-sitekey="public site key"></div>
</body>
But it's not working.
Edited
I just don't understand JavaScript much . That's why I was asking and still don't understand it .
What it means and what does I have to do with it. https://www.google.com/recaptcha/api/siteverify
Can you give me an example of whole code
First thing is if you want to redirect use window.location.href = "example.com" and second you are showing user that page will redirect in 5 seconds but you are only setting 1 seconds time and third don't call the function in setTimeout only pass the function as callback to it. And please write this entire code as recaptcha's callback so that it can be called when recaptcha gets verified from server.
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
<script type="text/javascript">
if (grecaptcha.getResponse() == "success") {
function Redirect() {
window.location.href = "example.com"; // Correct here
}
document.write("You will be redirected to a new page in 5 seconds ");
setTimeout(Redirect,5000); // Correct here
}
else alert("You can't proceed!");
</script>
<body>
<div data-type="image" class="g-recaptcha" data-sitekey="public site key"></div>
</body>

How can I load all the website content before call a JavaScript function in php

<?php
$homepage = file_get_contents('https://finance.yahoo.com/quote/0016.HK/news p=0016.HK"');
echo $homepage;
?>
<script type="text/javascript">
function test(){
.....
}
The script was executed before the website load all the contents and thus, I can't get all the content after running the script.
How can I fully get the content from the website first and then run the script? thanks!
<script type="text/javascript">
document.addEventListener( 'DOMContentLoaded', function( event ) {
function test(){
console.log("DOM loaded");
}
});
</script>

How to include a php page through javascript and update it every so often? [duplicate]

I got this code from a website which I have modified to my needs:
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
</head>
<div id="links">
</div>
<script language="javascript" type="text/javascript">
var timeout = setTimeout(reloadChat, 5000);
function reloadChat () {
$('#links').load('test.php #links',function () {
$(this).unwrap();
timeout = setTimeout(reloadChat, 5000);
});
}
</script>
In test.php:
<?php echo 'test'; ?>
So I want test.php to be called every 5 seconds in links div. How can I do this right?
Try this out.
function loadlink(){
$('#links').load('test.php',function () {
$(this).unwrap();
});
}
loadlink(); // This will run on page load
setInterval(function(){
loadlink() // this will run after every 5 seconds
}, 5000);
Hope this helps.
Try using setInterval and include jquery library and just try removing unwrap()
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
var timeout = setInterval(reloadChat, 5000);
function reloadChat () {
$('#links').load('test.php');
}
</script>
UPDATE
you are using a jquery old version so include the latest jquery version
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
Try to not use setInterval.
You can resend request to server after successful response with timeout.
jQuery:
sendRequest(); //call function
function sendRequest(){
$.ajax({
url: "test.php",
success:
function(result){
$('#links').text(result); //insert text of test.php into your div
setTimeout(function(){
sendRequest(); //this will send request again and again;
}, 5000);
}
});
}
you can use this one.
<div id="test"></div>
you java script code should be like that.
setInterval(function(){
$('#test').load('test.php');
},5000);
<script type="text/javascript">
$(document).ready(function(){
refreshTable();
});
function refreshTable(){
$('#tableHolder').load('getTable.php', function(){
setTimeout(refreshTable, 5000);
});
}
</script>

How to make jQuery code not work for logged in Wordpress users?

I have this code in my header.php :
<script type="text/javascript" src="/js/jquery.js"></script>
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery("a")
.filter(function() {
var href = jQuery(this).attr('href');
// return true if href exists and matches the regular expression
return href && href.match(/mywebsite\.com\/(page)\//);
})
.click(function(){
jQuery("a").attr("target","_blank");
url = jQuery(this).attr('href');
jQuery(this).attr('href','/te3/out.php?l=click&u=' + escape(url));
});
});
</script>
How can I make this code only work for users that are not logged in? (I'm using wordpress self-hosted)
Right now the code opens every '/page/*' in a new tab and makes it go through /te3/out.php but I would like that not to happen to logged in users.
I'm pretty new to this so please make it easy to understand.
Thanks a lot!
Simply put condition that if user is not logged in then use script else not.For that you can use is_user_logged_in.
<script type="text/javascript" src="/js/jquery.js"></script>
<?php
if ( !is_user_logged_in() ) :
?>
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery("a")
.filter(function() {
var href = jQuery(this).attr('href');
// return true if href exists and matches the regular expression
return href && href.match(/mywebsite\.com\/(page)\//);
})
.click(function(){
jQuery("a").attr("target","_blank");
url = jQuery(this).attr('href');
jQuery(this).attr('href','/te3/out.php?l=click&u=' + escape(url));
});
});
</script>
<?php
endif;
?>

Javascript not working in JQuery loaded DIV

I load pages into a div , my question is i cant execute javascript code in page1.php.. How can i execute code when i load page in to div.. thanks for your help..
here is my codes
index.php :
<script language="JavaScript" type="text/javascript">
function swapContent(cv) {
$("#content").html('<img class="loader" src="images/loader.gif"/>').show();
var url = "load.php";
$.post(url, {contentVar: cv} ,function(data) {
$("#content").html(data).show();
});
}
</script>
Button1
Button2
<div id="content"></div>
load.php :
<?php
if(isset($_GET['contentVar']))
{
$contentVar = $_GET['contentVar'];
}else{
$contentVar = $_POST['contentVar']; }
if ($contentVar == "page1") {
include("page1.php");
} else if ($contentVar == "page2") {
include("page2.php");
}
and example for page1.php
<script type="text/javascript" src="nicEdit.js"></script>
<script type="text/javascript">
bkLib.onDomLoaded(function() { nicEditors.allTextAreas() });
</script>
<textarea name="message" id="message" cols="45" rows="5"></textarea>
Data returned from Ajax is treated like plain text, so any Javascript within it is not executed by default. See this article.
Try using .load() instead of $.post() :
function swapContent(cv) {
$("#content").html('<img class="loader" src="images/loader.gif"/>').show();
var url = "load.php";
$("#content").load(url, {contentVar: cv});
}

Categories