Is there a way to reload a PHP page when a users clicks a PHP link and then have the page jump to a certain spot on the same page using PHP or JQuery like an HTML anchor?
Here is the PHP link below.
' . $user_name . '
You can just use a hash link, for example:
'.$user_name.'
If in that page you have anything with that ID, say:
<div id="section">
Then it'd scroll down to there, no jQuery or JavaScript needed...this is normal browser behavior. You can read more about it in the HTML4 spec here.
As an example, here's a link to the answer you're reading, see the auto-scroll when you visit it? :)
https://stackoverflow.com/questions/3163053#3163061
Related
I have 2 PHP pages. In the first page, I am clicking a button which redirects to another file via <a> tag and does its work(sending mail). but now I want once that page functionality is done it should come to the previous page without any user action. How can I do that?
include once is not the solution since it just calls other php. I need to come back to the same page, however.
Try with this
header('Location: ' . $_SERVER['HTTP_REFERER']);
You can use :
header('Location: '.$_SERVER['HTTP_REFERER']);
Here is the solution you can go back to the previous page using javascript.
There is one object in DOM called History you can use that in onload event:-
forexample https://www.w3schools.com/jsref/met_his_go.asp
window.History.go(-1);
I’m trying to store the content of a div to a variable.
Example:
<div class="anything">
<p>We don't know the content of this div</p>
</div>
I want to search for <div class="anything"> and store everything between opening and the end tag.
We also want to avoid using absolute pathnames, so that it only searches the current HTML/PHP file for this div where the code is present.
Is this possible with PHP, or is this only possible with JavaScript ?
PHP is not that intelligent. He doesn't even know what he says.
PHP is a server-side language. It has absolutely NO clue about what the DOM (ie. what is displayed in your browser's window) is when it delivers a page. Yeah I know, PHP rendered the DOM, so how could it not know what's in there?
Simply put, let's say that PHP doesn't have a memory of what he renders. He just knows that at one particular moment, he is delivering strings of characters, but that's all. He kind of doesn't get the big picture. The big picture goes to the client and is called the DOM. The server (PHP) forgets it immediately as he's rendering it.
Like a red fish.
To do that, you need JavaScript (which is on the client's computer, and therefore has complete access to the rendered DOM), or if you want PHP to do this, you have to retrieve an full-rendered page first.
So the only way to do what you want to do in PHP is to get your page printed, and only then you can retrieve it with an http request and parse it with, in your case, a library such as simpleHtmlDom.
Quick example on how to parse a rendered page with simpleHtmlDom:
Let's say you know that your page will be available at http://mypage.com/mypage.php
$html = file_get_html('http://mypage.com/mypage.php');
foreach($html->find('div.anything') as $element)
echo $element->src . '<br>';
you probably need a combination of those.
In your Javascript:
var content = document.getElementsByClassName("anything")[0].innerHTML();
document.getElementByID('formfield').value(content);
document.getElementByID('hiddenForm').submit();
In your HTML/PHP File:
<form id="hiddenForm" action="path/to/your/script">
<input type="hidden" name="formfield" value="" />
</form>
In the script you defined in the form action:
if(!empty($_POST)){
$content = $_POST['formfield'];
// DO something with the content;
}
Alternatively you could send the data via AJAX but I guess you are new to this stuff so you should start slowly :)
Cheers!
steve
You could use JS to take the .innerHTML from the elements you wan and store them in .value of some input fields of a form and then use a submit button to run the PHP form handling as normal. Use .readOnly to make the input fields uneditle.
As per my client need , redirect the page without anchor tag and refresh page ,
and change the URL as per page appearance. I don't know any idea about this.
I can use ajax for page content. but I don't know the URL change option without
page refresh or redirect. how can I do this with ajax j-query. Any one guide me for this issue.thanks advance
sample url
www.samplesite.com/contact.php -> without anchor tag. and page refresh this url need to work on php.
I think you are looking for info about the new HTML5 History API ( pushstate ), this link has a detailed tutorial.
http://diveintohtml5.info/history.html
You can do this using below function.
if(pageurl!=window.location){
window.history.pushState({path:pageurl},'',pageurl);
}
You can use the following javascript functions
window.location.assign('http://www.samplesite.com/contact.php'); // keeps the current page in browser history
window.location.replace('http://www.samplesite.com/contact.php'); // replaces the current page in browser history
window.location = 'http://www.samplesite.com/contact.php'; // the same as assign() and is backward compatible even with the oldest browsers
jsfiddle
I have this code:
echo 'Send map to 2nd screen';
Id like to be able to press this link, which opens the case_util.php in another iframe and then goes to another link in its own iframe.
Sort of like a refresh/going back. However, it is a form, and pressing back retains the data in the form. I dont want this data in the form, hence I'd like it to navigate to the page.
Thanks :)
Syntax issues. document.location is not a function and is deprecated anyway.
Here is a better version
echo 'Send map to 2nd screen';
or easier to read since you want the href in the iframe named second .
...?> <a href="case_util.php?status=green&adr=<?php echo $adr; ?>" rel="nofollow" target="second"
onClick="window.location='create.php'" >Send map to 2nd screen</a>
Best answer:
Don't use iframes.
However if you are determined to use something as silly as an iframe, use something as silly as a meta redirect.
<meta http-equiv="refresh" content="0;url=http://example.com/"/>
;) seriously, just read about AJAX+jQuery instead:
http://net.tutsplus.com/tutorials/javascript-ajax/5-ways-to-make-ajax-calls-with-jquery/
Basically, I want the same effect as the oldschool html 'frameset' I think.
Take a look at this page please:
http://onomadesign.com/wordpress/identity-design/alteon-a-boeing-company/
If a user selects a project from industry -> transportation for example, I would like that the right scrollmenu keeps its initial state when the new project page comes up. So they won't get lost and have to click again to be in the same submenu section.
So, the right thumbnail navigation should stay in the same way, I don't want it to reload.
Do I have to do it with frames or iframes? Or can I make some kind of jQuery call to 'not reload' that div? Maybe PHP? I'm sorry, I am not a programmer from origin.
Update:
Guys, I managed to put the whole thumbnail navigation code into a seperate php file, called sidebar.php. Now this gets called in my single.php (Wordpress) by <?php get_sidebar(); ?>.
Should it now be easier to make this sidebar.php NOT refresh on page reload? I've been looking at cookies, php sessions, iframes.. but I can't get it to work.
Any more help would be greatly appreciated!
Facebook kinda does this without frames for optimization's sake. They take every single link and, if supported, using AJAX to load the page content without reloading the layout.
Obviously, this sort of thing may require significant restructuring of the internals of your app. Another option is to simply store the menu's state as a cookie on link click (see the jQuery Cookie plugin) and, on every reload, either have Javascript look at the cookie and dynamically restore the menu to its correct state, or use your internal PHP to read the cookie and decide what menu to display.
But if you get really desperate, you may end up falling back on frames. Sometimes that can be okay - but try everything else first :)
You also can detect what menu item was activated (you got the page request due to clicking on the corresponding link) and use this information to restore/select this menu item.
At least that is what I do and... No cookies or AJAX required!
You can use a technique known as "AHAH" Asynchronous HTML and HTTP. Essentially you're doing a jQuery
$.post("whatever.html",function(data) {
$("contentdivelement").html(data);
}
You can wrap this in a function like:
updateContent(sPage) {
$.post(sPage,function(data) {
$("contentdivelement").html(data);
}
}
This will load the content from your "frame" page into the div without reloading the page.
You can also bind to each of the navigation links and use their HREF as your path to load in your content div such as:
$(".menuLink").click(function() {
var menuLink = $(this).attr('href');
updateContent(menuLink);
/* prevents the browser from taking the parent to that link */
return false;
});
ADDITION:
Your menu may look like this:
<ul class="myMenu">
<li>Frame 1</li>
<li>Frame 2</li>
</ul>
Also,
If you want it to remember the page you're on you can use cookies or #anchors. There are many ways to add "tab" or "menu" anchors but one way would just be to use a jQuery plugin.
The most COMMON and TRENDY way to do it is to use #anchors. Your browser address bar ass #frame1 to the end so when the page is refreshed or reloaded it will load up "frame1" automatically with some additional code.
You can even called the anchor #/frame1.html and read the anchor in
$(document).ready(function() {
/* you'll need to either use a plugin or parse out the anchor from your current browser address bar */
updateContent(anchorContentVar);
});
Instead of updating your content using click-handlers I suggest a slightly different approach. Just replace your hyperlinks with this kind of link:
#info_page
Now set up a simple interval that reads out the current URL and updates the DIV accordingly:
__LOC = document.location.href;
setInterval(function(){
if (__LOC!=document.location.href) __LOC=document.location.href;
var fetchURL = __LOC.split("#")[1];
$.get( "/getcontent/"+fetchURL, function(d){ $("#mydiv").html( d ); } )
} 1000);
This allows visitors to use bookmarks as well.