Jump to content
Morsusy2k

jQuery load and url's

Recommended Posts

You would load content from php page like so

$.get('test.php' , function(data) {$('#content').html(data);});

not sure, what you mean about the other questions?

Share this post


Link to post
Share on other sites

Well the loading php page works like a charm with the code i added but that same #content div loads more pages. I would like to make it change address (url) when loaded. So it can be linkable. Eg. http://w3schools.invisionzone.com when loads this topic it turns into http://w3schools.invisionzone.com/index.php?showtopic=46574

Edited by Morsusy2k

Share this post


Link to post
Share on other sites

When the page loads check with if condition if $_GET['page'] is set with isset(), if true use php to add the value returned with $_GET to the jquey load function, if not load a default.

Share this post


Link to post
Share on other sites

Something like

<?php$thisQSvalue="";if(isset($_GET['page']) && !empty($_GET['page'])){$thisQSvalue = $_GET['page']; }?>

$(function(){<?phpif($thisQSvalue != ""){?>$('#content').load('<?php echo $thisQSvalue; ?>');<?php}else{?>$('#content').load('test.php');  // can be default page<?php}?>});

have not tested it, but should work.

Share this post


Link to post
Share on other sites

I'm confused..So it should be like: Inside body tags:

 <?php$thisQSvalue="";if(isset($_GET['page']) && !empty($_GET['page'])){$thisQSvalue = $_GET['page']; }?>

And in jQuery script:

 $('#menubutton1').click(function(){<?phpif($thisQSvalue != ""){?>$('#content').load('<?php echo $thisQSvalue; ?>');<?php}else{?>$('#content').load('test.php');  // can be default page<?php}?>}); 

For every menu button..?

Edited by Morsusy2k

Share this post


Link to post
Share on other sites

You have 2 options(1) if the menu button is anchor link as in <a href="http://site.com/index.php?page=page1.php>page1</a><a href="http://site.com/index.php?page=page2.php>page2</a> etc, the page will reload, and php will read the page#.php, and jquery will load the page using the value passed to it. This only happens when page is reloaded, or when the user types the url with querysting to specific page.(2) on click of anchor links, read href address, strip out all but page1.php, retrieve href address and use that to load that specific pages content without reloading (you will have to use e.preventDefault() to prevent page reloading) of page. you may have to include both, one for when user enters url the address box, and when user clicks menu button.

Edited by dsonesuk

Share this post


Link to post
Share on other sites

I though it could be easier, like:if there is something after "site.com/" strip it and load it and if not continue with default,when clicked on a menu div (cause my menu buttons are actually divs) add page name to "site.com/" eg. "site.com/index.php?page=pagename" and load it (via jQuery without reloading).. If its complicated, and by your post i guess it is,its not the question between life and death :D

Share this post


Link to post
Share on other sites

If you are going this route, i should do it so it reloads the page with querystring, using $_GET['page'] code method to use in a php include within content, so that will take care of when js is disabled, then use jquery to prevent link forcing reload, read href, strip and search for 'page' querystring value, and then apply to load(). Note the non js method would be used for the first loading of page as well anyway, to check if user entered a querystring load that page content, if not! load default page content.

Share this post


Link to post
Share on other sites

yes it is possible you can run itcontent is the i.d of <div> and it displays the information with out refreshing.

Share this post


Link to post
Share on other sites

Okay so i have done a little searching and i found that i can use "$_GET["page"]" command to get what is typed in address bar. So i guess i can use it like this:

$('#content').load('<?php $_GET['page'] ?>');

(or i can make it into variable first) But how to add "?page=test.php" in the address bar when clicked on a button (without reloading) in php or javascript?

Share this post


Link to post
Share on other sites
Okay so i have done a little searching and i found that i can use "$_GET["page"]" command to get what is typed in address bar.So i guess i can use it like this:
$('#content').load('<?php $_GET['page'] ?>');

yes! but that only reads that when page is loaded or is reloaded, you click another link, the page reloads with new query string, $GET reads the new querystring and load() shows the content that matches that querystring value.
But how to [u]add[/u] "?page=test.php" in the address bar when clicked on a button (without reloading) in php or javascript?

PHP will always involve a reload, JavaScript can read from a php, as when using load(), $.get, $.post, ajax etc. but it will change specific page content. with changing the address, It can only be done with HTML5, and its not cross browser compatible, yet!

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...