Reply to Re: PHP to AJAX - a little off topic

Your name:

Reply:


Posted by shimmyshack on 11/27/07 00:23

On Nov 26, 11:45 pm, John <John_nos...@nnnnnnnnn.nowhere> wrote:
> I forgot to add a few things.
>
> I have a MYSQL database with all of the text inside it that I want to
> search for. I to create an ajax search box that will search for the
> text and display it on the screen.

basically the only thing that makes something ajax, is that instead of
the page refreshing and showing the results, you simply have some
javascript send the post payload in the background, the php script
takes that and sends it back to the javascript, which is then
responsible for inserting it into the page.
So you see the traditional model is that the page form is submitted to
the php script, that generates an html page with data inside it.
Now AJAX makes this easier by removing the need for the php script to
return a whole html page, you can simply have a script that returns
XML, JSON, or even a fragment of html (like a list <ul><liitem 1</
li><li>item 2></li></ul> ) which then gets inserted into the page.

so to demonstrate this - in a completely unacceptable way which i
would never use in production (check out some of the many libraries
for ajax routines from jquery, scriptaculous, moo, and so on....)

<?php
/*
* this is an ajax hello world
* filename: script.php
*/
header( 'Content-Type: text/html' );
echo '<script type="text/javascript">' . "\n";
echo 'parent.show( \'' .
htmlentities( addslashes($_GET['field']) .'\' )' . "\n";
echo '</script>' . "\n";
exit;
?>

<html>
<head>
<script type="text/javascript">
function show( string ){
document.getElementById( 'results' ).innerHTML = string;
}
</script>
</head>
<body>
<h1 id="results"></h1>
<form method="get" action="script.php"
target="notverywellhiddenframe">
<input type="text" value="hello world" name="field" />
<input type="submit" value="submit" />
</form>
<iframe src="" name="notverywellhiddenframe"></iframe>
</body>
</html>

untested, but when the user types stuff in and hits submit the form
makes a get request to script.php inside the iframe, which returns a
call to a javascript function in the parent document with an argument
of $_GET['field'] (whatever the user typed) which is then set inside
the h1 tag.

As I say just a proof of concept the actualy day to days methods are
much slicker than this heap of steaming... but it shows what the
principle is. "Converting" as you put it will mean understanding
whether you will actually benfit from using repeated calls to a
function, how you will handle failures, when you will make those calls
- onkeypress, or when theres something useful to search on - including
how often to call. You might well overload you server or database as
you scale up if you arent careful, you should use $_GET as well for
your queries as you arent changing the state of your app (which is the
only time post will be used). Also will you compromise your google
ranking. Are you going to add this behaviour on unobtrusively - thus
allowing your non javascript user agents (like google and assistive
devices) to use your app the "old fashioned way".
In general apps benefit from well thought out implementations of ajax
techniques - such as inserting a new mail when it arrives, or allowing
realtime sorting of data in a list back to a database - populating a
table in chucks based on scrolling position. But be careful you will
need to learn alot more than the basics to make your app useable,
failsafe, and friendly.

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация