php update main page when div content is updated - php

I have a "home" page: home.php, that contains a "banner" for my website. On this banner is a box that shows the user a number, (think Facebook notifications..)
The actual page that loads, home.php, is only a placeholder, as most of what happens on the "page" happens in <div> elements that exist inside home.php. When the user takes an action on a page inside a <div> element on home.php, I'd like the "notification" to refresh, but I'm not sure how to do that since home.php is not ever refreshing itself - only the div content is refreshing, (ajax, etc.)
In my home.php, the code to display this is:
<div class="navbar-userData">
<?php
$count = count($_SESSION['cost']);
echo "<img class=\"hoverButton\" id=\"hoverButton\" src=\"../../img/button.png\">";
echo $count;
?>
</div>
The user will take actions that will push an additional item onto the end of $_SESSION['cost'], and when they do that, the the count of $_SESSION['cost'] will increase, but it will not refresh to the user until they refresh the page, (which I don't want.)
Is there a way for me to trigger this bit of code to auto-refresh, or use jquery in my ajax success call to reload <div class="navbar-userData">?

Related

Using jquery load() to include php page that submits a html table back to the main page

I load foo.php into index.php. foo.php has a form that I submit to get a table of results. If I use php 'include' I can submit and get results back but if I use jquery load() I don't see the results.
This works:
<div class='loaded' id="foo_loaded">
<?php include('foo.php'); ?>
</div>
This doesn't:
$("#foo_loaded").load('foo.php');
In both situations the original html before I submit will show. When I submit though only the one method will generate the table.
I'd prefer to use jquery for the faster loads and because when using php 'include' the pages which get loaded as a hidden div don't get hidden until after everything loads which isn't pleasing to the eye.
For example. With the following code I want to load home if the form hasn't been submitted, but if submitted I want it to hide home and show foo. Instead if I'm using php 'include', home first loads and then hides after the page is finished.
<?php
if(isset($_POST['submit'])){
echo '$(".loaded").hide();';
echo '$("#foo_loaded").show();';
}
else {
echo '$(".loaded").hide();';
echo '$("#home_loaded").show();';
}
?>
I ended up using iframes.
The problem I was having with divs not hiding while the page loads was fixed by adding 'display:none' css in the style tag of the page rather than trying to work it with jquery.

Go to previous page after submitting a form to a PHP file with a div loaded to specific content

I have a logic problem maybe:
I have a page that has a div called #content,
in which I've loaded another page that has a form to submit data* (I have other pages loaded into the same #content but not "active" at the same time, because one of them are loaded on a click event via AJAX)
Data* that another PHP file processes.
when the process ends in that PHP file, I want to get back to the previous page so, I use:
> header('Location: '.$SERVER['HTTP_REFERER']);
But, (the problem comes up here.)
When I get back into my main page that holds that famous #content div. the page loaded into the #content isn't the exactly previous one, so this is very annoying when the user works on it, because they need to click again the desired button to be loaded into #content.
Any idea to solve this problem?
I dont know what you are in your code. I think the good solution is to use $_seesion and store your information inside. When you reload the main page, check simply is value exist.
Page 1
session_start();
if (isset($_SESSION['page2'])
{
echo "we come to page 2";
unset($_SESSION['page2'])
}
Page 2
session_start();
$_SESSION['page2'] = 1;
You can get more informations about session here:
https://www.php.net/manual/fr/reserved.variables.session.php

Refresh only a div ("content") by universal button working on every page

To be clear - Ive already checked other Questions about refreshing div and the ideas I found were not exactly what I look for.
My site is made of plenty pages with the same header and footer (top, bottom, menu on both sides). I use smarty templates, and the Whole action of every page happens in one <div id="content">.
My users use to refresh most of those pages many times to do an action they've already done once again. With refreshing browser loads again header, footer, viewed page etc. I would like to bring them the button (instead of F5) which will refresh just a current content page (e.g. account.php) without refreshing whole site.
One of plenty structure:
<?php
$title = 'OneOfPlenty';
require_once("includes/head.php");
{
Whole action
}
require_once("includes/foot.php");
?>
header.tpl ends with <div id="content"> then comes
onofplenty.tpl and then in
footer.tpl I got </div> (close the content)
Here comes the question: Is it even possible? Am I able to create such a flexible button which will recognize which page is being displayed and will "know" to refresh just the content of this page?
Any ideas and help will be aprreciated.
Thank you
TTed
You could do an Ajax call with jQuery to get the output html of the tpl file of the page.
You could use an Ajax call, e.g. by using the jQuery get() function, e.g. like this
$.get("includes/account.php", function(data) {
$("#content").html(data);
alert("Load was performed.");
});
If you saved some kind of variable, either to session or to a data-content on your div. Just so you know which page you are on. Say if you are on account.php you set $('#content').attr("data-content", "account"). Then if you press the refresh button you could use an ajax get on $('#content').attr("data-content") + 'php' to re-import the data. Could be done with a SESSION variable as well.

refreshing php page with button

i have simple coding problem. i have created a page with textbox and share button. the page also contains one Points up button.
i had a problem with that points up button that when the user click on that button and refresh the page ... a window ask for resend of information
for that i have used following code which works fine.
`header('Location: samepageurl.php');
exit;`
but the problem with above code is when user scroll down page and click the button. the page automatically scrolls up. and user have to manually scroll it down.
what i want is the page should refresh but it should be on the same location where it was.
if the problem is still unclear please refer the following images
You can set a fragment identifier.
eg:
<a name="points_up"></a> <!-- this needs to be near that button, the page will scroll exactly where the element is -->
and redirect him to:
header('Location: samepageurl.php#points_up');
die;
Mihai answer is correct, but as you said that fragment identifier is not working because each user has points up button, you can pass user id as a fragment identifier and make a hidden(display : none;) <a> tag and pass the user id in front of each user...
Like this:
You can set a prefix before a user id too (optional)
<a name="pu12345" style="display: none;"></a>
<?php
header('Location: whatever.php#pu12345');
exit;
?>
You can send the request via ajax instead relying on the normal form submission. That will not affect the scrolling of the current page.
Add this line at the bottom of your page before the the <\body> tag
<button id="PageRefresh">Refresh a Page in jQuery</button>
<script type="text/javascript">
$('#PageRefresh').click(function() {
location.reload();
});
</script>

Dynamically changing <div> content on link click with PHP?

I have an area on my webpage that is populated by different <div> containers of information. When a link in my navigation bar is pressed, one <div> will fade out and another will fade in. What I'd like to do is have one of these content <div>'s filled with dynamic information, and when a link is pressed on another one of the "pages" it would change which database to load the information from and then display, or fade in, that <div> with the new information.
Sudo:
View information
<div id = "dynamicDiv">
<?php include 'file.php' ?>
</div>
file.php
**Find which database to load information from and display content**
I thought about using $GLOBAL vars, but I'm not sure how to set those from a link, and also it wouldn't reload the div content.
I also considered using a form, but I'm not sure of the "correct" way of doing this would be, and also when the page is reloaded the <div> that is displayed by default would be loaded, not the <div id = "dynamicDiv>
Any suggestions/ideas are very much welcomed....
In this case you should use ajax.
AJAX is used for changing the page content from server without reloading the page.
You can use this JQUERY AJAX And JQUERY LOAD
$(document).ready(function(){
$("#changediv").load("load.php?id=1");
})
in load.php
$id=$_GET['id'];
// use that id for dynamic query in database
$query="SELECT *.....";
$result=mysql_query($query);
echo mysql_fetch_array($result);//somthing like that
All the word echoed in php become response in ajax.

Categories