Ajax Talk - Asynchronous javascript and xml discussion board
 FAQ   Search    Usergroups      Register   Profile   Check your messages   Log in 
Signup now to enjoy a range of free membership advantages !!
Ajax Talk - Moderators Wanted
Send PHP code to div

 
Post new topic   Reply to topic    Ajaxtalk.com Forum Index -> PHP
View previous topic :: View next topic  
Author Message
ephracis
New one
New one


Joined: 10 Oct 2007
Posts: 2
Location: Sweden

PostPosted: Wed Oct 10, 2007 10:18 am    Post subject: Send PHP code to div Reply with quote

I have been searching for an easy way to learn Ajax but it is too much information and I just do not know where to start. Very overwhelming.

My goal is pretty simple. I have a file called code.php:
Code:

<b>
<?php
echo "Moo";
?>
</b>


and I have my index.php
Code:

<html>
<head></head>
<body>
The cow goes:
<div id="mydiv></div>
</body>
</html>


Now I just want to put Javascript inside the index.php file which should put the code from code.php inside the div which is called "mydiv".

Very simple goal but everywhere I look on the net people talk about very complicated frameworks and stuff and I get overwhelmed by all of it.

I was hoping that maybe I could get some help here. I really want to learn Ajax but I do not want to learn *all* of it right away.
Back to top
View user's profile Send private message
evan_carothers
Light Frequent Poster
Light Frequent Poster


Joined: 11 Oct 2007
Posts: 9
Location: River Falls, WI

PostPosted: Thu Oct 11, 2007 9:03 pm    Post subject: Reply with quote

So you want the code in code.php file to be inserted depending on a user's action, I assume, otherwise you would just use an include. There are a lot of easy frameworks out there for Ajax. Prototype is my favorite though. To use prototype, you would include download the prototype js file from prototypejs.org to your web directory, then include it into the index.php file in the head section like so:

<script type="text/javascript" language="Javascript" src="/YourScriptDirectory/Prototype.js"></script>

Then you would have a javascript function that can reside in an external js file or be included in your index.php file that would be called whenever you needed it, probably from a button click, input onchange, etc that would use prototype's ajax Updater function to send the asynchronous request and populate the division. Something similar to this:

function updateWithAjax(){
var url = "/Code.php";
new Ajax.Updater('mydiv', url, { method: 'get' });
}

The Ajax.Updater function will automatically populate the division with id 'mydiv' when the request is returned, and it's that easy. If you need more options, such as doing more than populating a div on success, or passing variables in, you need to use the Ajax.Request function. Prototype's documentation on their website is pretty good, and there are all kinds of great prototype ajax tutorials out there.

Let me know if you need any more help! You could use your own ajax functions rather than using a framework like prototype, but the advantage is that prototype has been extensively tested to be cross-browser compatible. You do get a decent amount of overhead if you're only using it for it's ajax capabilities, and there may be a more lightweight solution for you, but this should work fine.
Back to top
View user's profile Send private message Send e-mail
ephracis
New one
New one


Joined: 10 Oct 2007
Posts: 2
Location: Sweden

PostPosted: Fri Oct 12, 2007 11:01 pm    Post subject: Reply with quote

Thank you so much! Now I think that I can go out and investigate myself how to send arguments to the page.

This was not so complicated as all the other stuff I found while googling for Ajax and PHP. Razz Just download one file and off I go. Smile

Now I just hope that there are some good documentation for Prototype.

Again, thanks.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Ajaxtalk.com Forum Index -> PHP All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
 Advertisements
 XML & Copyright Notice 
    


Powered by phpBB © 2001, 2005 phpBB Group

What is according to Wikipedia: Ajax Web2.0 XML E4X


Link to our site:

  


del.icio.us digg spurl wists simpy newsvine blinklist furl blogmarks yahoo! myweb smarking ma.gnolia segnalo reddit fark technorati cosmos


Normal Bookmark


Partner Button Links:
Codecrunch.com: Webmaster Tutorials    One Ajax


Partner Text Links:
quomon | web20log | ajaxmatters | tableless | 456bereastreet | music charts | gift ideas | competitions | free xbox 360 | link trade ?


Credits:
it consultancy & project management lead generation zoekmachine optimalisatie steal these buttons seo expert