5 Replies - 5085 Views - Last Post: 07 November 2008 - 04:02 PM

#1 animedude123  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 80
  • Joined: 03-August 07

Read URL and Load in Ajax

Posted 05 November 2008 - 10:27 PM

Hey,

I currently have a page that has a small bit of content pulled from another page that I would like to load with ajax over and over again. I'm using ajax because I don't want it to refresh the page.

The other page that is being loaded though needs to be able to get variables from the url bar through php.

So my main problem is that I have a main page with a url that looks like index.php?user=1 and I need the page loaded into it by ajax to be able to access that user=1.

The script I'm using for the ajax loading was found on DynamicDrive: DynamicDrive

So does anyone know a way for me to include an outside page (user.php for example) into index.php using ajax, keep the ability for it to be reloaded every few mins without page refreshes, and have the included page able to read the user=1?

Any help would be great!

Is This A Good Question/Topic? 0
  • +

Replies To: Read URL and Load in Ajax

#2 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 205
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: Read URL and Load in Ajax

Posted 06 November 2008 - 07:51 AM

It seems like the problem you are trying to solve is rather complicated. If you could supply us with an example and/or the code you are using it might make it easier to help or find a better solution.

Is the page you are loading via AJAX in the same domain as the index page?
You can use AJAX to load a page in a div, span or iframe, though i don't really recommend iframes.

Why do you want the loaded page to read anything? if the page you are loading is a .php file, you could format the loaded page server side by passing the arguments to it directly as what you are attempting to do is going to be in the range of difficult to impossible.
Was This Post Helpful? 0
  • +
  • -

#3 animedude123  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 80
  • Joined: 03-August 07

Re: Read URL and Load in Ajax

Posted 06 November 2008 - 04:49 PM

Wow, I finally got it :D Took a lot of messing around and some hidden form fields, but I got it and it seems to work perfectly.

Ended up using a normal php include for the original load so it could get the url variable.

Then also used a hidden value in a form with the url's value to be used in the ajax script. All is now working well :)
Was This Post Helpful? 0
  • +
  • -

#4 mocker  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 50
  • View blog
  • Posts: 466
  • Joined: 14-October 07

Re: Read URL and Load in Ajax

Posted 06 November 2008 - 06:07 PM

It's usually considerate to post the solution to your own thread if you find it, so that anyone else stumbling upon this question can also be helped without starting a new thread.

What it sounds like you are doing is calling a php page, and then having that page keep something on it updated via ajax, with the ajax request depending on a variable you sent it.

Ex.
You go to index.php?user=1

In this page lets say you have a div that you want updated via ajax
<div id='dvDynamic'> AJAX CONTENT HERE</div>



You didn't mention what you wanted to trigger the update, so I'll assume you want it triggered when the page loads, and then every.. 3 seconds afterwards

For pretty much anything using AJAX, I use the prototype javascript library. It is one file you download and include, and then makes things much easier (prototypejs.com )
$('dvDynamic')
is an easy way to say
document.getElementById('dvDynamic')
and prototype has support for older browsers/netscape that do not use getElementById

Prototype also has class to automatically an element on your webpage, every X seconds - exactly what you need
function startUpdates(){
  var url = 'dynamicpage.php?var1=<?php echo $_GET['user'] ?>';
  new Ajax.PeriodicalUpdater('dvDynamic', url, {
  method: 'get', frequency: 3
});

}



This will automatically update the contents of 'dvDynamic' with whatever it gets back from a request to 'dynamicpage.php?var=1' (if 1 is what the user is). frequency: 3 means it updates every 3 seconds

Now, to get the ball rolling.. in your body tag
<body onload='startUpdates();'>



annd.. you have a div that is updating every 3 seconds with content based upon the user variable.

If you have any interest in using POST and stuffing some more variables into the ajax request.. like, having the ajax request send the current settings in a form on your page with each request so you can update a div based upon options a user choose without doing a reload.. it only takes a couple more lines of code.

This post has been edited by mocker: 06 November 2008 - 06:09 PM

Was This Post Helpful? 0
  • +
  • -

#5 animedude123  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 80
  • Joined: 03-August 07

Re: Read URL and Load in Ajax

Posted 07 November 2008 - 03:52 PM

View Postmocker, on 6 Nov, 2008 - 05:07 PM, said:

It's usually considerate to post the solution to your own thread if you find it, so that anyone else stumbling upon this question can also be helped without starting a new thread.

What it sounds like you are doing is calling a php page, and then having that page keep something on it updated via ajax, with the ajax request depending on a variable you sent it.

Ex.
You go to index.php?user=1

In this page lets say you have a div that you want updated via ajax
<div id='dvDynamic'> AJAX CONTENT HERE</div>



You didn't mention what you wanted to trigger the update, so I'll assume you want it triggered when the page loads, and then every.. 3 seconds afterwards

For pretty much anything using AJAX, I use the prototype javascript library. It is one file you download and include, and then makes things much easier (prototypejs.com )
$('dvDynamic')
is an easy way to say
document.getElementById('dvDynamic')
and prototype has support for older browsers/netscape that do not use getElementById

Prototype also has class to automatically an element on your webpage, every X seconds - exactly what you need
function startUpdates(){
  var url = 'dynamicpage.php?var1=<?php echo $_GET['user'] ?>';
  new Ajax.PeriodicalUpdater('dvDynamic', url, {
  method: 'get', frequency: 3
});

}



This will automatically update the contents of 'dvDynamic' with whatever it gets back from a request to 'dynamicpage.php?var=1' (if 1 is what the user is). frequency: 3 means it updates every 3 seconds

Now, to get the ball rolling.. in your body tag
<body onload='startUpdates();'>



annd.. you have a div that is updating every 3 seconds with content based upon the user variable.

If you have any interest in using POST and stuffing some more variables into the ajax request.. like, having the ajax request send the current settings in a form on your page with each request so you can update a div based upon options a user choose without doing a reload.. it only takes a couple more lines of code.


Yea, your solution is pretty much what I did.

I also needed it to be done when a form was submitted, and that was where my 2nd problem came. For some reason the script I used on the form didn't want to read the variables in the url (even if the url being used was xxx.php?user=1), and so that is where the hidden form input came in.
Was This Post Helpful? 0
  • +
  • -

#6 mocker  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 50
  • View blog
  • Posts: 466
  • Joined: 14-October 07

Re: Read URL and Load in Ajax

Posted 07 November 2008 - 04:02 PM

If your form is submitted via POST, then any data you put in the url is ignored, since forms can be either POST or GET (the url variables), but not both.

If you want to have form data be sent via ajax without actually submitting and reloading the page, you can either generate a query string for the url, like with a GET request, or you can make the query string and pass it to XMLHttpRequest.send(query) if you are sending as post, and the variables will get passed through in the request like normal. Prototype can also take any javascript hash-like object and use it as the data parameters, which makes it easy to send POST data with your ajax requests.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1