Ajax woes

<updated see bottom>

Well it’s been a while since my last post, I’ve been working on a website that’s been taking up alot of my time and giving me a big headache.
Basically I started with a one page template that had a nice clean look. Unfortunately since I was last working on webpages, CSS has sprung up and taken over. So for every little change I had to copy an existing bit of code then change it, then refer to the CSS file and after alot of playing and going back and forth I finally got the template file looking how I needed it to to progress.

Now that I have my template page, I took that and created a few basic pages. To get the pages a bit more functional I changed the pages over to PHP. This worked well and now I had a whole login/register system, some basic pages displaying info, and some pages that interacted with a database.

All this was starting to come together and look good.
Things are never supposed to run smoothly though are they. The more I used the pages the more I thought that they need to load more smoothly, rather than going to a new page request for everything I needed to start using ajax.

So instead of finishing off the site as I was going, I decided to rewrite what I already had done to fit into an ajax framework.
Changing each of the tabs to pull the relevant page into the existing page was pretty simple. I got the tabs all working, highlighting the correct tab and changing along the way perfect.
Now the tricky bit, alot of the pages use forms and scripts. So I decided to try tackling the scripts first.
Basically I dont want to load all the script on the first load of the page as it’ll load up a lot of scripts that wont even be used. So I put the script in the page that gets pulled by ajax. seems like a simple solution.
However if the script isn’t present on page load the script isn’t recognised. ok I’ll stick the scripts to one side and work on the forms. oh no a problem, to use the forms inside ajax it needs a script to run.
So I fell back and decided just stick the script in the head of the first file at least it’ll get it all working. Nope that didn’t work either. As the elements aren’t present on the page when the script is first loaded the submit intercept stuff isn’t getting applied.

So I’ve started looking for a way to put scripts into ajax pages that will be relevant scripts for that page. I think this would be pretty common, but after hours of looking there is stuff out there mentioning it, but nothing explicitly saying how to have an initial script running to interpret other scripts inside an ajax page.

So there’s my ramblings for the night and the reason’s I stopped posting stuff for a few weeks. I don’t want to post details about the site just yet as it’s a complete work in progress, I have alot of the backend working but the entire site is currently ripped apart and non-functional. I know all the bits I want to get it to do, just not quite how to do them. I’ll solve it eventually, you’d have thought from the years working within IT I’d have loads of development contacts, unfortunately I have quite a few web designers, but none that do php/code development.

<update>
ok so after alot of searching around I found alot of stuff saying anything in a script should get interpreted as it’s loaded but that wasn’t happening for me. more and more searching I found a different way of loading the content instead of the httpxml or xmlhttp whatever it was, the following works for me:-

function loadajax(page,tab)
{
var page,tab;
$.ajax (
{
method: "get",
url: page,
dataType: "html",
success: function( strHTML ) {
$( "#content" ).html( strHTML );
}
}
);
}
</script>

Hey presto as the page gets loaded the script inside is now run too. Now to rework all the forms and stuff on the site.
<thought>I really do need to look at how you put script on a blog properly too.</thought>