Jump to content
  • Advertisement
Sign in to follow this  
CelticSir

[web] Ajax [Solved]

This topic is 3236 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
Seeing PHP is executed on the server, and JS is executed in the browser, you can't directly set a PHP variable with JS.

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
So the new href link syntax would be:


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

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!