I would like to create a PHP page which accepts an arguement like so:
http://localhost/page.php?topic=Foo
and then pulls data from an SQL Database where topic=Foo but then automatically checks for new data every 10 seconds and refreshes a DIV tag using Ajax. I've tried and nothing works. Any help?
EDIT: here is the code I've used:
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
ext = <?php $_GET[feedtitle] ?>
$(document).ready(function() {
$("#responsecontainer").load("response.php?ext=" + ext);
var refreshId = setInterval(function() {
$("#responsecontainer").load('response.php?ext=' + ext);
}, 9000);
$.ajaxSetup({ cache: false });
});
</script>
</head>
<body>
<div id="responsecontainer">
</div>
</body>
EDIT: I can do the SQL bit, it's just the getting the arguement to the response.php im having issues with.
EDIT: I have new code, but its still not working:
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
function gup( name )
{ name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null )
return "";
else
return results[1];
}
var feed = gup('f');
$(document).ready(function() {
$("#responsecontainer").load("response.php?ext=" + feed);
var refreshId = setInterval(function() { $("#responsecontainer").load('response.php? ext=' + feed); }, 9000);
$.ajaxSetup({ cache: false });
});
</script>
</head>
<body>
<div id="responsecontainer">
</div>
</body>
So, you need to
Get escaped URL parameter
,
output the jquery $.post function's result data and then you just need to know
How to refresh page with jQuery Ajax? and do an
AJAX Div Retrieval every 60 seconds?
I hope that helps :)
Related
am trying to find out how to add this script to my php file , as i try echo it ,its not working.
am sure there is a way to add this, its my first time adding these scripts to php file ,maybe il learn something today and remmeber it thanks.
var counter = 1;
var auto_refresh = setInterval(
function () {
var newcontent= 'Refresh nr:'+counter;
$('#divID').html(newcontent);
counter++;
}, 5000);
and the html div this is not needed to add to php as i have own div in php file
<div id="divID"></div>
Close the php tags, open a script tag, paste your script there, close your script tag, re-open your php tag. ;)
I recommend putting your js at the end of the file tho.
<?php
// your php here
?>
<div id="divID"></div>
<?php
// some php if you want to
?>
<script type="text/javascript">
//Your javascript here
var counter = 1;
var auto_refresh = setInterval(
function () {
var newcontent= 'Refresh nr:'+counter;
$('#divID').html(newcontent);
counter++;
}, 5000);
</script>
Normally, you won't put javascript in your php file... But if there's no other solution:
<?php
echo"
<script type=\"text/javascript\">
var counter = 1;
var auto_refresh = setInterval(
function () {
var newcontent= 'Refresh nr:'+counter;
$('#divID').html(newcontent);
counter++;
}, 5000);
</script>
";
In PHP pass js code into variable
<?php
$script = "<script type='text/javascript'>
var counter = 1;
var auto_refresh = setInterval(
function () {
var newcontent= 'Refresh nr:'+counter;
$('#divID').html(newcontent);
counter++;
}, 5000); </script>";
?>
In HTML where you want to show/add
<html>
<head>
<?php echo $script; ?>
</head>
</html>
You can put it like this or you can put <script> part in the head of your page or in js file and include in page.
<?php
// code
?>
<script>
$( document ).ready(function() {
var counter = 1;
var auto_refresh = setInterval(
function () {
var newcontent= 'Refresh nr:'+counter;
$('#divID').html(newcontent);
counter++;
}, 5000);
});
</script>
<div id="divID"></div>
Note: you also need jquery. If you don't use one you can include this in your page also
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
I just tested it and it's working perfectly. Currently my counter is
Refresh nr:51
I am looking for a simple code that will forward the user to another URL specified in the URL:
Example:
http://example.com/index.php?URL=anothersite
the user lands on http://example.com/index.php and after 5 seconds is forwarded to anothersite
can someone help me out?
The JS example:
setTimeout(function(){
var pair = (window.location.search.split("&")[0] || "").split("=");
if( (pair[0] || "").toUpperCase() != "?URL") return;
window.location = pair[1];
},5000);
Try to set the redirect target in the querystring, then use javascript to redirect!
URL: http://example.com/index.php?URL=anothersite
<html>
<head>
<script>
setTimeout(function() {
var match = /[\?&]url=([^&]*)/.exec(location.search);
if(match && match[1]) location.replace(match[1]);
}, 5000);
</script>
</head>
<body>
</body>
</html>
The above example is for pure html, but if you can use php, that'll be more convenient to get the querystring parameter by $_GET['url']:
<html>
<head>
<script>
setTimeout(function() {
location.replace('<?=$_GET['URL']?>');
}, 5000);
</script>
</head>
<body>
</body>
</html>
Am trying to refresh data stored in a div every 10 seconds using jQuery.
My HTML code is:
<!DOCTYPE html>
<head>
<title>Untitled Document</title>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function(){
setInterval(function() {
$("#latestData").load("getLatestData.php #latestData");
}, 10000);
});
</script>
</head>
<body>
<div id = "latestData">
</div>
</body>
</html>
And the PHP code I am using (temporarily as I know this won't change due to the same "data"):
<?php
echo "test";
?>
However, it isn't even showing "test" on the html page.. could anyone suggest where I have gone wrong?
Many thanks
jQuery load method works in a different way. Try reading its documentation.
You don't have to specify destination element ID twice, remove the second one, like this:
$("#latestData").load("getLatestData.php");
Here's a way that will solve what you want to achieve, using the $.get method in jQuery:
$(document).ready(function () {
setInterval(function() {
$.get("getLatestData.php", function (result) {
$('#latestData').html(result);
});
}, 10000);
});
If you want to refresh message count just use this code:
$(document).ready(function () {
setInterval(function () {
$("#ID").load();
}, 1000);
});
I'm a bit new to Ajax.
I've got a Ajax file which includes a php file and refreshes it every X seconds. That code for AJAX.PHP is:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>
<script language="JavaScript">
setInterval( "SANAjax();", 10000 ); ///////// 10 seconds
$(function() {
SANAjax = function(){
$('#dataDisplay').fadeOut("slow").load('rand.php').fadeIn("slow");
}
});
</script>
<div id="dataDisplay"></div>
This code is working fine.
Bbut the html of AJAX.PHP page shows as it is code, while I want the output html of rand.php not javascript code given above. How can i do it?
suppost "rand.php" html out put (source) is: < html >< body >this is body <\ body ><\ html >.
I want this html to be shown on the html source of AJAX.PHP.
How can i do it?
If I mess any thing let me know, I'll try to make it more clear.
You need to run it. Not insert only as file.
$.post("rand.php", { },
function(data) { $('#dataDisplay').html(data) });
updated:
SANAjax = function(){
$.post("rand.php", { },
function(data) { $('#dataDisplay').fadeOut("slow").html(data).fadeIn("slow"); });
}
<script type="text/javascript">
var auto_refresh = setInterval(
function ()
{
$('#load_rand').load('rand.php').fadeIn("slow");
}, 10000); // refresh every 10000 milliseconds
</script>
<div id="load_rand"> </div>
Don't forget to add the jquery library
I just get the parameters from PHP GET method and use them using jQuery. There is no output when run the page.
<?php
if (isset($_GET['url'])){
$url = $_GET['url'];
$url = explode(" " , $url);
echo end($url);
exit;
}
?>
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.js"></script>
<script type="text/javascript">
$('input[type=text]').change(function (){
if ($(this).val() !== ''){
var url = $(this).val():
$.post('grab.php?url='+url+'', function (data){
window.open(data, 'Download', 'width=10,height=10');
$(this).html('');
});
}
});
</script>
</head>
<body>
<input type="text" style="width:100%;height:20px;"/>
</body>
</html>
I'm new to designs and hope mistake is there.
First of all put your document in standards mode (by using a proper doctype at the beginning, which means HTML 4/XHTML 1 strict or HTML 5). Then you can use error console for debugging.
I found following error
Unexpected token: ':' on line 7,
The colon should be a semicolon.
var url = $(this).val():
And then, the actual reason why nothing is happening is because the input is non-existent when the script is invoked/cached. You need to execute it after the DOM has been constructed.
$(document).ready(function() {
// content
});
Final code.
$(document).ready(function() {
$('input[type=text]').change(function (){
if ($(this).val() !== ''){
var url = $(this).val();
$.post('grab.php?url='+url+'', function (data){
window.open(data, 'Download', 'width=10,height=10');
$(this).html('');
});
}
});
});
put it in $(document).ready() like this:
$(document).ready(function() {
$('input[type=text]').change(function (){
if ($(this).val() !== ''){
var url = $(this).val():
$.post('grab.php?url='+url+'', function (data){
window.open(data, 'Download', 'width=10,height=10');
$(this).html('');
});
}
});
});
I think using a .change event on a text input isn't advisable. Usually .blur and .focus are more appropriate.
$(document).ready(function() {
$('input[type=text]').blur(function(){
var currentInput = this;
if ($(currentInput ).val() != ''){
var url = $(currentInput ).val();
$.post('grab.php?url='+url, function(data){
window.open(data, 'Download', 'width=10,height=10');
$(currentInput).val('');
});
}
});
});