How to use .load() for refresh div in same page? - javascript

I am trying to make an on-line user content which refresh it self and calls every time php function.
I used
<div class="onlineFriends">
<?php
$members = find_online_friends($_SESSION['id']);
foreach($members as $member):?>
<div class="user" href=<?php echo ($member['f_id']); ?> rel="popup_name" >
<img src=<?php
$avatars = find_avatar($member['f_id']);
foreach($avatars as $avatar)
echo ($avatar['src']) ?>
/>
</div>
<?php endforeach; ?>
</div>
<script>
$(function(){
var refreshId = setInterval(function() {
$('.onlineFriends ').load("# .onlineFriends ").fadeOut("slow", function () {
$(this).fadeIn("slow");
});
}, 50000);
});
</script>
This works good. But .load() function I think loads first all entire page and then calls .onlineFriends. I can see it with firebug on console. it returns all page source code as GET answer. My question is it will make slow down ? Because I will use this method 5 times for other div contents and each time each functions will load full page.
Also I tried to create separate .php file but I have in php code some dependences and I cant run this function in another file.

Any request to your page will result to it's full code. You can specify in php to send back only wanted part of page when specific GET or POST data is present and use $.get() or $.post() to get them.
Because I will use this method 5 times
On the same page? Then it's better to replace them from the one of $.get() and $.post() data during callback function.

Create other page friends.php
and in page index use cod java/ajax and use jquery-min
<script src="js/jquery-1.10.1.min.js"></script>
$(document).ready(function() {
$("#center").load("friends.php?var=<?php echo $member['f_id']; ?>");
var refreshId = setInterval(function() {
$("#center").load('friends.php?var=<?php echo $member['f_id']; ?>');}, 9000);
$.ajaxSetup({ cache: false });
your div html use
<div class="center"></div>
your friends.php use
<?php include"your bd here" $jab = $_GET['var'];
'select bd'
echo $resultyourneed['f_id'] ?>

Related

how to call php function through hypertext link

I want to call a PHP function passing an argument on clicking a hyperlink (text with <a> tag) on the same page, i.e. href='#' onclick='loadpic($id).
Where $id is the variable to be passed to PHP function.
You can do that by ajax easily. Create a loadpic function in javascript instead and then pass $id to the php file via ajax and do something when you get the result.
Your javascript file should look like this-
(make sure you've inserted a link for jquery in your index file)
function loadpic(id_param){
$.post("filename.php",
{
id: id_param
},
function(data){
alert("Data received:" + data);
});
};
The data parameter in function(data) would be the value that you've echoed in the php file.
If you want to use onclick() method in your HTML element then you have to write your method is javascript and make an AJAX call to PHP. THis is one simple way to do this using php and javascript. And for this please see javascript and ajax with PHP.
But if you dont want to use javascript then here is a simple way and very simple code.
<?php
if(isset($_GET['myMethod'])){
myMethod($_GET['id']);
}
function myMethod($id){
echo $id."<br>";
}
?>
<?php
$myId = 4;
?>
<a href="index.php?id=<?php echo $myId ?>&myMethod" >MyLink</a>
Note: Copy this code and paste into a file called index.php because notice the hyper link tag and its attribute href="index.php".
Also note that you can not directly call php method using js like onclick() from your HTML tag. You should use AJAX or something. But using above demo code you can easily call myMethod() which is a PHP method using the hyper link tag.
In your case:
<?php
if(isset($_GET['call_loadpic'])){
loadpic($_GET['id']);
}
function loadpic($id){
echo "My loadpic method has been called! and the id is".$id."<br />";
}
?>
<!-- your other codes -->
<?php
$id = 4;
?>
<a href="this-same-file-name.php?id=<?php echo $id ?>&call_loadpic" >MyLink</a>
This is because your php function is in the same page and you want to call that function using same file and hyperlink in the same file. (According to your question)
You still will have to make javascript handle the exception because php has no onclick.
onClick=\"loadpic("'.$id.'");"\
//this will only send a php variable javascript will still have to loadpic();

Can't refresh one specific div after ajax load (MVC system)

I already checked all others answers from that questions, but no one seems to work.
He's my problem, I try to reload a div where I got a php variable.
I got MVC system, so i can't call other files when i want.
My controller doesn't receive arguments from ajax call ($_POST and $_FILES are not isset).
When I try to do other stuff after ajax call, it works (example: unlink a file)
<?php
$test = 'lksd';
...
if( some conditions )
{
$test= 'piof';
unlink(../images/myImage.jpg);
}
?>
...
<div id="test">
<?php
echo $test;
?>
</div>
And my ajax call is in a jquery plugin (http://www.dropzonejs.com/)
I found where i can do my stuff, and then i call this function:
$('#test').load(window.location + "#test");
My div doesn't refresh but my file is deleted, if you have any idea of what can i do to solve this problem...
Thanks in advance
Create the PHP detail you want to stand alone. Call it something like getDetail.php:
<?php
$test = 'lksd';
...
if( some conditions ) {
$test= 'piof';
}
echo $test;
?>
In your page, you can now load that content at any time:
<html>
<body>
<div id="test">
</div>
Reload
<script>
$(document).ready(function(){
$("#test").load("getDetal.php");
$("#reloadDetail").click(function(){
$("#test").load("getDetail.php");
});
});
</script>
</body>
</html>

need javascript or jquery function to wrap php code (window.setInterval bootstrap list group)

Thanks for all the answers, seems like AJAX is the solution, I'll give it a try. But what about JSON? Isn't JSON an even better solution? If it is, why is AJAX more preferable?
I'm looking for a way to update this part of php code every 5 seconds, which would regenerate this bootstrap list group. what would be a good way to do it? I figure I couldn't just wrap it in window.setInterval, and refreshing the entire page is not an option. Thanks in advance.
<?php
$i=0;
// Display all room
foreach ($rooms as $room) {
$room_num = $room['room_num'];
$room_type = $room['room_type'];
$note = $room['note'];
echo '
<a class="list-group-item" >
<h4 class="list-group-item-heading" id="room_num' .$i. '" ><p>'.$room_num." - " .$room_type.'</p></h4>
<p class="list-group-item-text" id="note' .$i. '" ><p>'.$note.'</p></p>
</a>
';
$i++;
}
$rooms = "";
getList();
?>
All on the same 'page.php'
php part:
<?
if ($_POST["whatever"])
{
echo 'your shizzle here'
exit;
}
?>
Javascript part: (with jquery)
<script>
setInterval(function()
{
$.post( "page.php", { whatever: 1}, function( data ) {
document.getElementById('someid').innerHTML = data;
});
},5000);
</script>
html part
<div id = "someid"></div>
Another way to do it could be using an iframe :) But iframes won't be used in the future I think.
Basically when you write php code inside javascript, it always run once, when the page is loaded. After this you just writing php code to the browser which is simply do not understand (Php is processed on the server, and the output is Html, Css, and Javascript, which the browser can interpret)
So, if you need to update data from the server without reloading the page, the only way to do this is with Ajax Requests, that basically connect to the server within the page and get data from it.
In your case, Save the PHP code which ever you want to execute in a file say php_temp.php
Now just do
setInterval(function(){
$.get("php_temp.php", function(data){
console.log(data) // data stores whatever php_temp.php echoes
});
},5000);
more on Ajax: Ajax Basics

Passing HTML content through jQuery AJAX and PHP

I want to display the content of Division in one page to another.
<div id="box-cont" class="box-content">
<?php
echo $stat;// Contains multiple images with strings
?>
</div>
Here $stat will display multiple images with few contents. And i am using jQuery AJAX to display this html in another page.
var bcont = $('#box-cont').html();
$.ajax({
type:"POST",
url:"abc.php",
success: function(data) {
document.location.href='def.php?bcont='+bcont;
}
});
And i am getting this html in def.php as
$_GET['bcont'];
This is not working for me..
Thanks in advance
A shortcut method would be to use sessions to pass the html from one page to another.
<div id="box-cont" class="box-content">
<?php
echo $stat;
$_SESSION['stat'] = $stat; // make sure session_start(); is present on this page
?>
</div>
Then, in the success handler of your ajax call
$.ajax({
type:"POST",
url:"abc.php",
success: function(data) {
window.location.href='def.php';
}
});
Finally, in def.php
session_start();
echo $_SESSION['stat'];
Note: This is not an ideal approach but will do the job for you
Ok, you want to pass the html of #box-cont to def.php, right?
You're mixing post and get. Read: http://api.jquery.com/jquery.post/
and you'll find you need to use the data-parameter.
There's something odd with the success-part. You don't reload the frame/div
where def.php is sitting. What you've done is passing data to the server
but not getting anything back, (if I read your attempts correctly).
In fact there's currently little use for a server-via here, from what you
describe it can all be done at client / JS.

Auto refreshing div with javascript

Trying to refresh a div every 60 seconds, using the following javascript:
<script type="text/javascript">
var auto_refresh = setInterval(
function ()
{
$('#load').fadeOut('slow').load('index.html #load').fadeIn("slow");
}, 60000);
</script>
Here is the corresponding div:
<div class = "well" id="load">
<center><h3><i class="icon-bar-chart"></i> Mt.Gox</h3></center>
<center><h3>Ƀ1 = <?php echo $usd; ?> USD</h3></center>
<hr>
<center><h3><i class="icon-bar-chart"></i> BTC-e</h3></center>
<center><h3>Ƀ1 = <?php echo $usde; ?> USD</h3></center>
</div>
The div is currently not refreshing at all.
Thanks for any help!
Ok that's because you are using the script incorrectly., You need to use callbacks as below
<script type="text/javascript">
var auto_refresh = setInterval(
function ()
{
$('#load').fadeOut('slow',$(this).load('index.html #load',
function(){
$(this).fadeIn("slow");
})
)
}, 60000);
</script>
Edit: I misunderstood the code a bit so ignore my original comment below!
Try to remove the #load in the url. It's perfectly valid according to the jQuery API, but I think in this case it can cause reduncancy.
That is, make it like this instead:
$('#load').fadeOut('slow').load('index.html').fadeIn("slow");
When you call .load() on an element it will load into that element, if you also specify the same element in the Url to load - I think it can cause a problem.
Please see this link: Origin null is not allowed by Access-Control-Allow-Origin
Also that issue, I think that you shouldn't load html data via file:/// URL. Why don't you load a div in the same file. Forexample:
$('#load').fadeOut('slow').load($('#test').css('display', 'inline')).fadeIn("slow");
and the div tag is like this:
div id="test" style="display:none;"

Categories