• Advertisement
Sign in to follow this  

[web] Ajax [Solved]

This topic is 2993 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey, - p.s can some tell me what the code used to put my script in tags? I need help big time as my site has just completly buggered up lol! I just made a JS script which laoads data into a div (ajax style) which means page refreshes are not required... but currently it loads a $pagename.php "contents" in to the div - but i cannot have this method. What i need it to do - is generate a string with the $pagename.php and inject that to a variable into the div... heres what i got: This works as its meant to - but needs to be changed to suit my new method:
var bustcachevar=[[[[1]]]] //bust potential caching of external pages after initial request? ([[[[1]]]]=yes, [[[[0]]]]=no)
var loadedobjects=""
var rootdomain="http://"+window.location.hostname
var bustcacheparameter=""

function ajaxpage(url, containerid){
var page_request = false
if (window.XMLHttpRequest) // if Mozilla, Safari etc
page_request = new XMLHttpRequest()
else if (window.ActiveXObject){ // if IE
try {
page_request = new ActiveXObject("Msxml2.XMLHTTP")
}
catch (e){
try{
page_request = new ActiveXObject("Microsoft.XMLHTTP")
}
catch (e){}
}
}
else
return false
page_request.onreadystatechange=function(){
loadpage(page_request, containerid)
}
if (bustcachevar) //if bust caching of external page
bustcacheparameter=(url.indexOf("?")!=-[[[[1]]]])? "&"+new Date().getTime() : "?"+new Date().getTime()
page_request.open('GET', url+bustcacheparameter, true)
page_request.send(null)
}

function loadpage(page_request, containerid){
if (page_request.readyState == [[[[4]]]] && (page_request.status==[[[[200]]]] || window.location.href.indexOf("http")==-[[[[1]]]]))
document.getElementById(containerid).innerHTML=page_request.responseText
}

function loadobjs(){
if (!document.getElementById)
return
for (i=[[[[0]]]]; i<arguments.length; i++){
var file=arguments
var fileref=""
if (loadedobjects.indexOf(file)==-[[[[1]]]]){ //Check to see if this object has not already been added to page before proceeding
if (file.indexOf(".js")!=-[[[[1]]]]){ //If object is a js file
fileref=document.createElement('script')
fileref.setAttribute("type","text/javascript");
fileref.setAttribute("src", file);
}
else if (file.indexOf(".css")!=-[[[[1]]]]){ //If object is a css file
fileref=document.createElement("link")
fileref.setAttribute("rel", "stylesheet");
fileref.setAttribute("type", "text/css");
fileref.setAttribute("href", file);
}
}
if (fileref!=""){
document.getElementsByTagName("head").item([[[[0]]]]).appendChild(fileref)
loadedobjects+=file+" " //Remember this object as being already added to page
}
}
}




With that and this below is how it all works on the site so far:
<a href="javascript:ajaxpage(login.php','contentarea');">Link to login</a>
<div id="contentarea">;</div>




How ever i need to change this so instead of it just "loading the page contents" into the div i want the JS to do similiar idea of loading into the div but merely pass php into it so i can do this:
<a href="javascript:ajaxpage(login.php','contentarea');">Link to login</a>
<div id="contentarea">
<?php
include($name); 
//$name being "login.php" due to above href
//$name value needs to be made from the JS from getting the url
//in the HREF whilst not refreshing the page to do so.
?>
</div>



Can some one help me edit my JS to do what im trying to do ? I'm stuck at this point at trying to get it to do this final part. [Edited by - thefollower on December 8, 2009 11:53:07 PM]

Share this post


Link to post
Share on other sites
Advertisement
From the FAQ:

Code

When posting source code, you may want to use the [.source lang="cpp"]

[/source] tag, which will produce a formated, color coded, easy-to-read table. If you only want to preserve spacing, but not use the other features of the source tag, try using the tag instead.

You also have the option of utilizing the source tag to highlight different programming languages. If you do not specify the language, C++ will be assumed. The following languages are available:

C [.source lang="c"]
[/source]
C++ [.source lang="cpp"]
[/source]
C# [.source lang="c#"]
[/source] or [.source lang="cpp"]
[/source]
Java [.source lang="java"]
[/source]
javascript [.source lang="javascript"]
[/source]
JScript [.source lang="cpp"]
[/source]
Python [.source lang="python"]
[/source]
X86 Assembly [.source lang="asm"]
[/source]
Visual Basic [.source lang="vb"]
[/source]
VBScript [.source lang="vbscript"]
[/source]

Without the periods.




After looking over your code and reading your post a few times... I'm still not sure what you're trying to do. You say you're trying to generate a string and inject it into a variable in the div... and innerHTML is one of the div's variables. Is there any way you can clarify your objective?


EDIT:

I looked at your post again, and it looks like what you're trying to do is use AJAX to go to a PHP page, get some PHP code in string form, bring it back to the client, then execute it. If that is the case, then you can't do that. The reason being that PHP is a server side language. When the server gets a request for a PHP code, it executes all the PHP code on the page, then sends the result to the client. That means that once the page gets to the client and starts executing javascript code, you can't execute any more php code on the page.

EDIT2:

Another thing I noticed was that in:


<a href="javascript:ajaxpage(login.php','contentarea');">Link to login</a>
<div id="contentarea">;</div>




You're missing the first single quote by login.php.

Share this post


Link to post
Share on other sites
I will try my best. To explain - and yeh i fixed the single quote syntax.

Current the javascript does the following thing....

Say I have a div on a page like this:


<div id="contentarea"></div>






Now with that js script - i can use hrefs like this:

<a href="javascript:ajaxpage('login.php','contentarea');">Link to login</a>




What happens with the JS is - instead of reloading the whole page it just loads login.php into that div.

So say that div was on index.php - when a user clicks the link, the user is still on index.php but login.php is displayed because its loaded the php script into the div.

Now, onto what i wanted to try and do was to instead of loading the php script, i wanted to pass the string in the href in this case login.php and put it to a PHP variable.

So now the JS creates a php variable $link and assigns that variable with the string login.php.

Then instead of currently the JS loading the script into the div, i want it to just merely refresh the div only so that php variable can be used in this:



<a href="javascript:ajaxpage('login.php','contentarea');">Link to login</a>

<div id="contentarea">
<?php
include($link); // SET from JS which in this case would be "login.php"
?>
</div>




Does that make better sense ? Hope it does :)

And thanks for the quick reply! This site is awesome!

Share this post


Link to post
Share on other sites
Ok, I see what you're trying to do.

You'll need a generic PHP script (let's call it foo.php) that looks something like this:


<?php

function getParm ($parmName)
{
$retParm = null;
if(isset($_POST[$parmName]))
{
$retParm = $_POST[$parmName];
}
else
{
if(isset($_GET[$parmName]))
{
$retParm = $_GET[$parmName];
}
}

return ($retParm);
}

$link = getParm("link");
include($link);

?>



And then for the ajax call, you'd just want to send it to the url:



var page = "login.php";
rootdomain + "foo.php?link=" + page;


Share this post


Link to post
Share on other sites




function ajaxpage(pagename, containerid){
var page_request = false
if (window.XMLHttpRequest) // if Mozilla, Safari etc
page_request = new XMLHttpRequest()
else if (window.ActiveXObject){ // if IE
try {
page_request = new ActiveXObject("Msxml2.XMLHTTP")
}
catch (e){
try{
page_request = new ActiveXObject("Microsoft.XMLHTTP")
}
catch (e){}
}
}
else
return false
page_request.onreadystatechange=function(){
loadpage(page_request, containerid)
}

var url = rootdomain + "/foo.php";

if (bustcachevar) //if bust caching of external page
bustcacheparameter=(url.indexOf("?")!=-[[1]])? "&"+new Date().getTime() : "?"+new Date().getTime()
page_request.open('GET', url+bustcacheparameter + "&link=" + pagename, true)
page_request.send(null)
}




That should do it. Just pass the location of the php file you want to use, ie "login.php".

Share this post


Link to post
Share on other sites
No, it should be like this:



<a href="javascript:ajaxpage('login.php','contentarea');">Link to login</a>






The foo.php is built into the ajax function from my last post. If you want, you can modify it to include the whole thing, though... If you need your ajax function for something else.

Share this post


Link to post
Share on other sites
Quote:
Original post by MortusMaximus
No, it should be like this:

*** Source Snippet Removed ***

The foo.php is built into the ajax function from my last post. If you want, you can modify it to include the whole thing, though... If you need your ajax function for something else.




Okay thats good then i don't have to change my current scripts - sounds like your quiet the pro at this langauge :P

Thanks for the help Mortus! I will post back in a few hours to see how i get on encase i come across an error - but for now i must head off to work!

Thanks!

EDIT: if i wanted to make the function in the index.php where i include the .js rather than have the function on foo.php how would i go about doing that?

Share this post


Link to post
Share on other sites
Second edit:

Okay - although it works there is a problem :(


When the ajax gets the php into the DIV it doesn't act like its "included".. its more like added contents of a PHP script from out side the main page..

Say i had:

index.php:

<?php
include("connect.php");
include("functions.php");
?>
<script type="text/javascript" src="/js/jax.js"></script>


<a href="javascript:ajaxpage(login.php','contentarea');">Link to login</a>
<div id="contentarea">
</div>






Now if i click the link to load login.php it says:
Access denied for user 'nobody'@'localhost' (using password: NO)

Because the includes don't continue on through.. like it would if it was an include.

Any thoughts how i can solve that issue ? Because otherwise i'd have to have 2 connections which will add up over time with more users - is it possible at all?

Or will i have to live with it ? :P

[Edited by - thefollower on December 8, 2009 6:38:58 PM]

Share this post


Link to post
Share on other sites
When you execute a PHP script and put the contents in a div with ajax, you aren't putting the code itself into it, you're putting the result of the execution of the code in the div. Maybe that is causing the confusion?

Share this post


Link to post
Share on other sites
Yes i understand - so there is no way to have it so that my global includes on index.php can carry through into the executed script in the div?

Share this post


Link to post
Share on other sites
Yes i understand - so there is no way to have it so that my global includes on index.php can carry through into the executed script in the div?

Like in normal php:



include("connect.php");
include("login.php"); //this wouldnt need connection due to above connect link



But obviously this doesn't work in ajax because ajax doesn't work the same way as an "include" does... so i was wondering if there was a way to have it so it could display login.php and also allow it to detect the includes on the main page. Outside of the div.

I was hopeing the includes on inde.php would continue through as the execution of login.php came into the div...because as it currently doesn't, it means it increases total connect counts.

Share this post


Link to post
Share on other sites
I think I see what you're trying to do. Includes don't carry over that way, because of the way PHP is executed on the server. Once you're done with the initial PHP on a page and send it to the client, you're done with PHP for that page. AJAX can be used to call PHP after the page has already gone to the client without refreshing, but it is a completely different script.

That means that you can't have includes carrying over between PHP calls like that... but it also means that by including connect.php in your login.php doesn't mean making another connection to the server. The reason being that login.php is executed on your server ANYWAY, and the server doesn't need to "connect" to itself to include a new PHP file.

So what you need to do is just include all the include files you need for each script.

Share this post


Link to post
Share on other sites
So this is the only way i can do it then:


index.php

<?php
include("connect.php");
include("functions.php");
?>

<a href="javascript:ajaxpage(login.php','contentarea');">Link to login</a>
<div id="contentarea">
</div>



login.php

<?php
include("connect.php");
include("functions.php");
//rest of the page conent
?>



As you can guess though for every new php page i make theres 2 lines of duplicated includes which is a shame (as u can tell im obsessed with being efficient.) Wish there was a way so i wldn't need to include them twice.

How much of a work around would the ajax need to be to have it not require another 2 includes for it to still work ?

Share this post


Link to post
Share on other sites
includes.php

<?php
include("connect.php");
include("functions.php");
?>

index.php

<?php
include("includes.php");
?>
<a href="javascript:ajaxpage(login.php','contentarea');">Link to login</a>
<div id="contentarea">
</div>


login.php

<?php
include("includes.php");
//rest of the page conent
?>

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement