Jump to content

Recommended Posts

Thank you. Yes I am done with the page refresh if possible, I'm not going to try to make that better anymore. I could use this other approach for many different things.
So I better keep pursuing it at least until I find out what I need can't be done with ajax.

Can someone tell me how to have php make a  XMLHttpRequest to some javascript elsewhere? Then I can access the javascript for a setTimeout, to avoid page refresh on the page the div is in. You don't have to give me all the code. I just need pieces here and there to head me in the correct direction.

Please let me know, thank you very much.
Thanks Ober, working on it but need more time. In the meantime this would fix it completely. Does anyone have a partial-page rendering function that I can put my php include in? That would be the easiest fix. Just as long as it doesn't wrap it so the php can't be searched by search engines.
I thought ajax was so you could refresh the whole page without all the usual that goes along with it. Didn't know it could refresh just part of a page.

To refresh just part of a page with ajax, does the part getting refreshed have to be surrounded in javascript? In other words can the javascript and ajax refresh a portion elsewhere on the page? By calling it by divider id or something? Please let me know, and thanks for all the input Ober.

Did you even look at the examples?  The whole point is to update a < div > with a specific id.  That div can be any size you want or anywhere on the page you want.  It doesn't have to be surrounded by JS... JS can sit in the header where JS normally sits.  You just make calls to it.

I suggest you go back through one of the tutorials and actually look at the code.
Ok... well ask specific questions about the examples then.  I would really suggest taking the code, copying it into your own pages and playing with it.  The best way to learn is to copy something that is working and change it until you break it.  Then if you can't fix it yourself, come and ask us.  Reading something and trying to comprehend it without actually using it isn't going to help anyone.
[url=http://www.phpfreaks.com/forums/index.php/topic,115581.0.html][/url]
For this Ajax link above, "A basic Start and working example" its fantastic for what it does.

I need to know how to manipulate the div only, using javascript. The example puts text into the divs and a lot more. If javascript and ajax can put text into divs, how can I get it to concentrate on the div id only? Once I know that I can figure out I can figure out the rest of it thanks to the example. Do you use getElementByID somehow with the div id name? Anyone who writes ajax and manipulates text into divs etc. should know. No one in the javascript forums know, because they don't have to learn about it, because many of them don't seem to do ajax much. If anyone knows please tell me. Thanks.
What are you trying to update in your page? Responce from another php file or a frames substitute?
Hopefully this can get you one step further, it starts with the javascript and contains div id to update and a link to process. It is based on the stickied thread but i've removed the url_encode javascript and textfieldcheck. Might be easier to figure out  :)

! remove spaces in [color=red]s c r i p t [/color]on line 4
[code]

<!-- start JS -->

<s c r i p t type="text/javascript" language="javascript">

function AjaxRequest(target_div,file)
{
var MyHttpRequest = false;
var MyHttpLoading = '<p>Loading...</p>'; // or use an animated gif instead: var MyHttpLoading = '<img src="loading.gif" border="0" alt="running" />';
var ErrorMSG = 'Sorry - No XMLHTTP support in your browser';

if(window.XMLHttpRequest)
{
try
{
MyHttpRequest = new XMLHttpRequest();
}
catch(e)
{
MyHttpRequest = false;
}
}
else if(window.ActiveXObject)
{
try
{
MyHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
try
{
MyHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e)
{
MyHttpRequest = false;
}
}
}
else
{
MyHttpRequest = false;
}

if(MyHttpRequest)
{
var random = Math.random() * Date.parse(new Date());
var file_array = file.split('.');
if(file_array[1] == 'php')
{
  var query_string = '?rand=' + random;
}
else if(file_array[1] == 'htm' || file_array[1] == 'html')
{
  var query_string = '';
}
else
{
  var query_string = '&rand=' + random;
}

MyHttpRequest.open("get", encodeURI(file + query_string), true);

MyHttpRequest.onreadystatechange = function ()
{
if(MyHttpRequest.readyState == 4)
{
document.getElementById(target_div).innerHTML = MyHttpRequest.responseText;
}
else
{
document.getElementById(target_div).innerHTML = MyHttpLoading;
}
}
MyHttpRequest.send(null);
}
else
{
document.getElementById(target_div).innerHTML = ErrorMSG;
}
}

</script>

<!-- END JS -->



<div id="update_div"></div>



<a href="#" onclick="javascript: AjaxRequest('update_div', 'check.php?var=whatever'); return false;">Update the div</a>


<! -- AjaxRequest explanation: ('first apply the div id to put result', 'then the file name you get results from') -->


<!-- more examples -->
<a href="#" onclick="javascript: AjaxRequest('update_div', 'info.html'); return false;">Update the div</a>
<a href="#" onclick="javascript: AjaxRequest('update_div', 'info.php'); return false;">Update the div</a>
<a href="#" onclick="javascript: AjaxRequest('update_div', 'info.php?switch=whatever'); return false;">Update the div</a>

[/code]

If your read the javascript bit, you see that we use document.getElementById() to point the result to it's location
Thank you very, very much for that! You don't know how glad I am to see you, I have looked through so many web pages or weeks without finding anything much about dividers and ajax. I hope you can please help me with some few more minor issues with this, then I'll be on my way, and send you some gifts for your help.

Right now a page elsewhere is being inserted into the div.

Lets see if I can explain what I need properly. Before you posted I don't know if I made myself very clear. I need to be able to manipulate just a divider with ajax. This is the opposite of what most people use ajax for but with a similar technique so I don't know why it shouldn't work. For instance, an external javascript function would be elsewhere.
Ajax would go to the server to connect to just the javascript function. The external javascript with ajax access would manipulate the divider so that anything in the divider would be on a timer. Thats pretty much it really.  Thanks.



If you want to start with readable content within the div you can do that without problems, and when you call to update the div through ajax the "default" content will be replaced by the new content that's being fetched.
[code]

<div id="update_div">

<?php

echo "Hello, this is default within the div and is being picked up by any search engine too<br />";
echo "But when the div is updated through AJAX, i will be invisible to the human eye but still found in the source";

?>

</div>

[/code]

Only way you can use javascript inside a remote file is to use it after it has being loaded within the div (onclick events etc).
This means that a onload feature can only be in the main file and not in the remote file (as far as i know it).
If you call a remote file like "hello.php?start=timer" it will execute php code or anything else server based you want in the remote file and then return results and put inside the result div.

But, can't you just start the timer on the main page onload just to tell ajax to start in 30 secs/refresh every 30 secs or whatever ?
Regarding the php, this is one of the better ajax articles I read but it didn't say how to make something viewable in the source code. [url=http://www.oracle.com/technology/pub/articles/schalk-ajax.html]http://www.oracle.com/technology/pub/articles/schalk-ajax.html [/url] Instead of carrying the text or xml in the form of a javascript onload function or similar. Where you can only read it on the page and not in the source. How can I get it to the div source instead? Thanks a lot Alpine.
I realize you don't respect me or don't want to listen to me or whatever... but you really don't understand the technology or how it works or anything about server/client interaction.  Go read something about any of this stuff.  You're talking in circles.
i dont want to sound rude here, but Tami, the reason why many of us "pick it up quicker than others" is because we started with the basics. I only personally started PHP about 3 years ago, and AJAX was not really something that ever got discussed, never mind used, so I/we had to make do with the traditional way of making the page refresh the normal way. So did most other sites - only recently have they really thrown everything at AJAX, and not without their problems. Nailing the basic/normal/old way (and a basic understanding of both PHP and Javascript as seperate entities) enabled us to at least understand about requests - then stuff like AJAX kinda just comes as a natural progression on top.
Again, I dont want to sound critical/assuming or rude, but generally when people want to use AJAX but dont know the basics, they:
a) dont really need to use AJAX
b) want to use it cos its a nice trendy bandwagon that everyone else is jumping on.

Ask yourself - do you really need it right now? Can you not get your site operational without it, and introduce it later when you understand it more? Even if you DO manage to get AJAX working, your lack of understanding of it will leave your site so full of security problems you'll have tonnes more issues in the long run. Seriously, AJAX is alot more than just refreshing a DIV with fresh data from the server. You'll eventually realise that its a whole topic of security ontop, too.

And reading through this thread, it's probably wise not to run circles around/dismiss ober on this (or many) subjects, as it's kinda well known around these parts that he's pretty educated as far as both PHP and AJAX are concerned, along with all its issues. You could learn alot just by taking his comments on board properly.

Cheers
Mark
To point on your problem, ajax cannot update the "normal" source code like you want it to.
I recomend you to try and learn how it works by playing around on your own first, and consider what the above pro's is also telling you. I've really tried to help you out here, but it seems to be at this point hopeless.

Sorry!
I found something and that does work for what I needed here. With all the 1000's of little tricks out there, you know that someone else might be able to help, and just one persons opinion doesn't always make it so.  Please let me post and let others respond on their own, they might have my answer. Give your opinion without ridiculing me, then let others give theirs. If no one can help me, I'll try something else. Please don't follow me around. Thanks.
Guest
This topic is now closed to further replies.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.