d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Javascript / Php Ajax > Need Some Quick Clarification Please
Add Reply New Topic New Poll
Member
Posts: 8,724
Joined: Oct 20 2007
Gold: 2,794.65
Jun 17 2012 05:08am
Okay so I'm trying to make a good ajax navigation setup that will use pushState / popstate html5 api.

- I'm trying to use no hash fragment
- Intact bookmarks
- Page refresh will bring you to the current state of the ajax nav

Code I got at the moment:

Javascript stuff
Code
$(function() {
$('nav a').click(function(e) {
 href = $(this).attr("href");

 loadContent(href); //function to call on nav click event

 history.pushState("", "", href); //append new href to end of url
 e.preventDefault();
});

window.onpopstate = function(event) { //on a url change (back / forward browser button)
 loadContent(location.pathname); //load content for that specific url
};
});

function loadContent(url){
$.ajax({
 type: "GET",
 url: "content_manager.php",  //make ajax request to this php file
 data: {"content_href" : url}, //send it the href
 success: function(data) {
  $("#content").html(data); //get returned data put it into the #content div
 }
});
 
$('li').removeClass('current'); //make it pretty
$('a[href="'+url+'"]').parent().addClass('current'); //make it pretty
}


Code
<?php

$content_href = $_GET["content_href"]; //get the content href sent by the ajax call

switch(content_href){ //switch
      //different cases to test for (yes I'm aware they do different things this is the bit where it's messing up so I'm experimenting)
case "portfolio": echo("<p>include/portfolio.php</p>"); break;
case "about": echo("<p>include/about.php"</p>); break;
case "resume": $content_href = "include/resume.php"; break;
case "contact": $content_href = "include/contact.php"; break;
default: echo("<p>include/404.php</p>"); //this one works but the others don't..
}
?>


So my question is does it work the same as I think it does?
Why does only the switch default statement work on a page refresh (popstate trigger)?
How would I go about sending a different file in the callback data? e.g I make the ajax request to the contnet_manager.php file rather than having that hold all the html stuff (gets messy really quickly), instead have it load the content from separate files?

Any useful resources / material you could link me to would be cool!

Thanks!

Member
Posts: 2,736
Joined: Nov 28 2009
Gold: 34.00
Jun 17 2012 05:23am
People really need to start putting their shit into fiddles.. It's common practice for getting help on IRC, and people here aren't better miracleworkers. :zzz:
Member
Posts: 2,736
Joined: Nov 28 2009
Gold: 34.00
Jun 17 2012 06:01am
I just facepalmed so hard on you. Are you even trying? Rofl. Spot the errors.

switch(content_href){ //switch

case "about": echo("<p>include/about.php"</p>); break;

Maybe it's time that you set up an actual testing area, where you can test your code and not just write code, ask on d2jsp for others to test it for you LOL

This post was edited by eagl3s1ght on Jun 17 2012 06:02am
Member
Posts: 8,724
Joined: Oct 20 2007
Gold: 2,794.65
Jun 17 2012 06:53am
Quote (eagl3s1ght @ Jun 17 2012 12:01pm)
I just facepalmed so hard on you. Are you even trying? Rofl. Spot the errors.

switch(content_href){ //switch

case "about": echo("<p>include/about.php"</p>); break;

Maybe it's time that you set up an actual testing area, where you can test your code and not just write code, ask on d2jsp for others to test it for you LOL


I wrote it in the browser, my actual files don't have those errors. Thanks anyways.

Code
<?php

$content_href = $_GET["content_href"];

switch($content_href){
case "portfolio": include("include/portfolio.php"); break;
case "about": include("include/about.php"); break;
case "resume": include("include/resume.php"); break;
case "contact": include("include/contact.php"); break;
default: include("include/404.php");
}
?>


when I do it the above way I get 404 errors when refreshing cuz it's a fake link. This is what I'm stuck on.

This post was edited by Atonement on Jun 17 2012 07:10am
Member
Posts: 2,736
Joined: Nov 28 2009
Gold: 34.00
Jun 17 2012 08:18am
Quote (Atonement @ 17 Jun 2012 14:53)
I wrote it in the browser, my actual files don't have those errors. Thanks anyways.

Code<?php

$content_href = $_GET["content_href"];

switch($content_href){
case "portfolio": include("include/portfolio.php"); break;
case "about": include("include/about.php"); break;
case "resume": include("include/resume.php"); break;
case "contact": include("include/contact.php"); break;
default: include("include/404.php");
}
?>

when I do it the above way I get 404 errors when refreshing cuz it's a fake link. This is what I'm stuck on.


You can't and shouldn't prevent F5 from refreshing the page. What you want to do is URL rewrite /content_manager.php?content_href=* to /*.
After all, you should let people refresh the page and its contents if they want to.

This post was edited by eagl3s1ght on Jun 17 2012 08:35am
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll