<?php include("news.php"); ?>
Hello, I include a news.php in my main page index.php. I want to refresh the news.php every 30 seconds, but not refresh the main page index.php, How to partial refresh a include php page? Thanks.
I've been searching for something similar and it's taken a while but I think this might work:
<script src="https://code.jquery.com/jquery-2.1.1.min.js" type="text/javascript"></script>
<script>
$(document).ready(function() {
setInterval(function() {
$("#screen").load('news.php')
}, 2000);
});
</script>
Another way to refresh an include (Valid only if the include only contains variables and their values):
<?php
$config = file_get_contents("news.php");
$config = str_replace("<?php", "", $config);
$config = str_replace("?>", "", $config);
eval($config);
?>
Very easy method to refresh include (only for backend single user, not for frontend):
<?php
$t = time();
rename("news.php", "news_${t}.php");
include("news_${t}.php");
rename("news_${t}.php", "news.php");
?>
Related
I write the below code to a footer.php file. This works but is applied for all the pages. I want to apply page scroll in one page only.
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery('body').animate({scrollTop: +400}, 1000);
});
</script>
Can any one help me on how to do it.
Thanks
Add a contidional statemnent on the footer.php file to only execute the jquery code if it is in only in the current page you wish for.
something like this example below
<?php
global $post;
$target_pageid = 7654;
$current_pageid = $post->ID;
if (current_pageid == $target_pageid){
?>
jQuery(document).ready(function() {
jQuery('body').animate({scrollTop: +400}, 1000);
});
<?php
}
?>
I'm trying out a more maintainable way to structure my code as learnt by some tutorials at nettuts+.
I've succeeded in making my header and footer files separate from the main content of each page so that they are loaded along with each different page, thus making it easier to manage changes to these two files.
I currently want to add some jQuery code to my footer, for only one page. This is where I've hit a wall in thinking. How can I allow this jQuery code to be present in my footer for only one page, and not all pages? Could someone please explain?
I was thinking of doing something like:
<?php if($title = "The jQuery Page"){?>
<script>
jquery code here....
</script>
<?php } ?>
But I think that is considered messy, as you are using one language to set off another.
Edit: Here's the requested code:
In my Controller I call the views:
$data['title'] = "The jQuery Page";
$this->load->view('header', $data);
$this->load->view('cool_page');
$this->load->view('footer');
Then in my footer I want to load a specific script in only this one pages footer:
<script src="<?php echo base_url();?>js/jquery-1.10.2.min.js"></script>
<script>
$(document).ready(function() {
/*cool jquery stuff*/
});
</script>
Try to load a view into the main view
/application/controllers/test.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Test extends CI_Controller {
public function index() {
$data = array(
'title' => "The jQuery Page",
'script' => TRUE // if you omit or comment this variable the 'views/script.php' is not loaded
);
$this->load->view('test', $data);
}
}
/application/views/test.php
<html>
<head>
<title><?php echo $title;?></title>
</head>
<body>
<?php if (isset($script)) { $this->load->view('script'); } ?>
</body>
</html>
/application/views/script.php
<script>
jquery code here....
</script>
You could specify JS to include as part of the data passed to the view:
CONTROLLER1
$this->data['js'][] = 'alert(\'Hi\');';
$this->data['js'][] = 'alert(\'Hey\');';
$this->load->view('footer', $this->data);
CONTROLLER2
$this->data['js'][] = 'alert(\'Yo\');';
$this->data['js'][] = 'alert(\'Sup\');';
$this->load->view('footer', $this->data);
Footer VIEW
if(isset($js) && is_array($js) && count($js) > 0){
echo '<script>';
foreach($js as $key=>$value){
$value;
}
echo '</script>';
}
I don't think there is a way around making a conditional 'if' statement around the script, so I used the suggested solution in my question.
Check the title to see if it is the desired page:
<?php if($title = "Photo Albums - Hands of Humanity"):?>
Then add the script to the DOM if it is so:
<script>
$(document).ready(function() {
//jquery stuff
});
</script>
<?php endif;?>
If there are other ways which are better for maintainability, I would be happy to hear them
I am redirecting to a new page after some specific time given by the user, like this :
html_meta_redirect( 'pagename?page_number='.$f_pageNumber, user_get_pref( 'refresh_delay' )*60 );
what are some alternate methods to implement this?
<?php
header('location:welcome.html');
?>
the header will redirect to the welcome.html page
Please note that there should not be any echo statements before the redirection
Try this:
<script>
function getLocation() {
window.location = 'placed your URL here';
}
window.setTimeout('getLocation', 5000) // 5000 = 5 seconds
</script>
I am new to jQuery. Basically I have a main page (with jQuery) and I want to include in a div another page that also has jQuery.
What I do is write in that particular div this code:
<?php
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/ong/new/index.php";
include_once($path);
?>
If I do this the jQuery in the child page stops working. The jQuery functions in the main page also stops working, but if I remove this link
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js" type="text/javascript"></script>
from the child page it works in the main page.
So, what am I doing wrong? How should you include a jQuery page in another?
It is because you are including jQuery multiple times. You could make small script to check if jQuery is present on the page, and only if is not present, load it:
// 'load-jquery.js'
window.onload = function () {
if (window.jQuery === undefined) {
var script = document.createElement('script');
script.src = 'path/to/jquery.min.js';
document.getElementsByTagName('head')[0].appendChild(script); // load jQuery
}
};
html
<div id="result"></div>
js
$('#result').load('<?php echo $path; ?>', function(data) {
$(this).append(data);
});
<?php
$go = $_GET['go'];
if(!empty($go)) {
if(is_file("page/$go.html")) include "page/$go.html";
else echo "<h1>Error 404</h1><p>Strona nie odnaleziona</p><p>Warunek ?go jest niepoprawny</p>";
}
else include "page/start.html";
?>
<script>
$.get('go', function(change) {
$('#center').load('page/<?php echo $go ?>.html');
});
</script>
I have somethng like this but it doesn't work. I just want that the page which is loaded (?go=name) won't refresh whole page
Firstly, that's vulnerable to an LFI vulnerability (Local File Inclusion). Consider what happens when someone enters: http://site.com/file.php?go=../../../../../../../../etc/passwd%00
Also, you can't just echo your filename and expect it to print it out...if you want to include the contents of the page in $go and print out $go, then use:
$go = urldecode([$_GET['go']);
$go = str_replace("/", "", $go);
$go = "page/$go.html";
EDIT: Actually, if you use my above code, they could still access files in the local directory, such as .htpasswd and .htaccess, so just don't let your users include any local files. There are better ways to solve the problem you have.
As for the AJAX, follow jeroen's advice.
You are mixing two ajax functions, $.get and .load and you are missing an event handler.
You will need something like this:
$('#go').live('change', function() {
$('#center').load('page/' + $(this).val() + '.html');
});
I have used live instead of change in case the go button is located in the refreshed section of the page.
Note that I am guessing that you want to refresh a section of your page based on a selection, the question / code isnĀ“t exactly clear about that...
Is this what you want to achieve?
<script type="text/javascript">
$(document).ready(function() {
var getgo = '<?php echo $_GET['go']; ?>';
if(getgo.length) {
$('#center').load('page/'+getgo+'.html');
}
});
</script>
<div id="center"></div>
It will load the contents of page/[your go param].html into #center.