Click to See Complete Forum and Search --> : get content from file
gilyotina
04-17-2007, 01:05 PM
hi,
totally new to php, as only this morning they told me i needed a server-side solution and not only javascript. so here i am.
i have a link from my page which should bring content from another html file into the already-opened html.
can i put a php function, that onclick will be called, and display a <div> from another html file? that div will be marked by an id.
I saw the file_get_contents function. is this the answer?
tnx,
The Little Guy
04-17-2007, 02:00 PM
I am not sure I quite understand what you want to accomplish. could you explain a little more?
gilyotina
04-17-2007, 02:05 PM
it all started when i saw my html got bigger and bigger.
i have a lot of text on my site, and i used javascript to show/hide divs, creating a dynamic menu.
so to keep my base html sane, i decided to put the "big content" in another file, another html file, and to generate the specific content from it (once the user clicks the link).
i understand thats where php can assist me - getting content from another file and place that content on my page.
true? how?:confused:
The Little Guy
04-17-2007, 02:24 PM
You could use ajax, then you wouldn't even have to use PHP.
gilyotina
04-17-2007, 02:27 PM
how do i use ajax to generate content from another html file?
:confused: :confused: :confused:
(in the javascript forum they said i needed a server side solution)
The Little Guy
04-17-2007, 02:30 PM
The Javascript:
Read the comments I think I made them very helpful
<script type="text/javascript">
<!--
//Browser Support Code
function ajaxFunction(URL){
var ajaxRequest; // The variable that makes Ajax possible!
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong (User Probably Doesn't have JS or JS is turned off)
alert("You Browser Doesn't support AJAX.");
return false;
}
}
}
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
//This if satement will check if the status of the script
//If the readyState is not equal to 4 (The request is complete)
//It will display text that says loading...
if(ajaxRequest.readyState < 4){
//AJAX in the prenthacies, is what id element in the body will be changed.
document.getElementById('AJAX').innerHTML = "<h2>Loading...</h2>";
}
//Once the readyState is equal to four, this means that the request was sent,
//and successfully processed.
if(ajaxRequest.readyState == 4){
//This is where the output of the file we called and it will be placed
//in the div where we named the ID = AJAX
document.getElementById('AJAX').innerHTML = ajaxRequest.responseText;
}
}
//This section processes the data
ajaxRequest.open("GET", URL, true);
ajaxRequest.send(null);
}
//-->
</script>
<a href="javascript:ajaxFunction('mycode.html')">Get This Page</a>
<div id="AJAX"></div>
Resource from my site - http://snippets.tzfiles.com/snippet.php?id=27
gilyotina
04-17-2007, 02:36 PM
i mean, i say 'thanks'believing it is what i meant,
it will take me a while to understand...as im new to almost everything:o
so now learning php will be only for pleasure..
The Little Guy
04-17-2007, 02:38 PM
If you Need help with that just ask me.
gilyotina
04-17-2007, 02:46 PM
going over your code again, i remembered what was the issue -
the users that dont have js enabled.
actually, the way i built my site, it becomes really empty when you dont have js.
that was why i became woried a bit and thought of php as well.
but, i guess, this will go into ver2 of my site :)
tnx again
The Little Guy
04-17-2007, 02:50 PM
This won't work either if javascript isn't enabled
gilyotina
04-17-2007, 02:56 PM
yeh, this i understood, :)
only now ive decided not to care about those who disable js. :p
why would they want to do that, anyway? :eek:
The Little Guy
04-17-2007, 02:59 PM
You could check this out - http://snippets.tzfiles.com/snippet.php?id=31
Its a very simple php application to do this.
gilyotina
04-17-2007, 03:05 PM
i'll have a look.
i still try to figure out your ajax, as im not familiar w/xml.
tnx!
The Little Guy
04-17-2007, 03:08 PM
AJAX is actually javascript, so if it isn't enabled, it won't work.