PHP to AJAX - a little off topic

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I have a good PHP script that I would like to use and want to convert
it to Ajax. It's basically a text only search program so Ajax would
work perfect. My questions is what is the easiest way to convert it
over. I'm kind of a NOOB to all of this and have messed with PHP, AJAX
etc. a little bit here and there.

If someone can please give me some advise that would be great.


Re: PHP to AJAX - a little off topic

Quoted text here. Click to load it

You are confused. AJAX is a scripting 'technique', not a language,
hence you can't 'convert' your php script to ajax.
What might work for you is scripting your front end (html form?) to
send/receive requests to your PHP script dynamically.

This is a good place to start.

Kailash Nadh |

Re: PHP to AJAX - a little off topic

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.  

Re: PHP to AJAX - a little off topic

Quoted text here. Click to load it

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....)

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

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

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.

Re: PHP to AJAX - a little off topic

 Thanks for the information.

I just have one more question. If I keep it a PHP script, how can I
add another php script like a yellow search highlighter? I have the
yellow php highlighter script, but do not know how to include it?

Re: PHP to AJAX - a little off topic

Quoted text here. Click to load it

well ok, this is a funny thing to do unless its a spellchecker.
you would have to send the complete text to be searched via ajax, and
return the same (but with some wrapped in tags which will of course be
the yellow parts) and replace the original text.

If you do it this way you may as well just use javascript:

function highlightWord(node,word)
    // Iterate into this nodes childNodes
    if (node.hasChildNodes)
        var hi_cn;
        for (hi_cn=0;hi_cn<node.childNodes.length;hi_cn++)

    // And do this node itself
    if (node.nodeType == 3)
    { // text node
        tempNodeVal = node.nodeValue.toLowerCase();
        tempWordVal = word.toLowerCase();
        if (tempNodeVal.indexOf(tempWordVal) != -1)
            pn = node.parentNode;
            if (pn.className != "highlight")
                // word has not already been highlighted!
                nv = node.nodeValue;
                ni = tempNodeVal.indexOf(tempWordVal);
                // Create a load of replacement nodes
                before = document.createTextNode(nv.substr(0,ni));
                docWordVal = nv.substr(ni,word.length);
                after = document.createTextNode(nv.substr(ni+word.length));
                hiwordtext = document.createTextNode(docWordVal);
                hiword = document.createElement("span");
                hiword.className = "highlight";

function googleSearchHighlight()
    if (!document.createElement) return;
    ref = document.referrer;
    ref = (ref) ? ref :;
    if (ref.indexOf('?') == -1) return;
    qs = ref.substr(ref.indexOf('?')+1);
    qsa = qs.split('&');
    for (i=0;i<qsa.length;i++)
        qsip = qsa[i].split('=');
            if (qsip.length == 1) continue;
            if (qsip[0] == 'q' || qsip[0] == 'p')
            { // q= for Google, p= for Yahoo
            words = unescape(qsip[1].replace(/\+/g,' ')).split(/\s+/);
                for (w=0;w<words.length;w++)

i think thats all you need for

which will highlight the word hello

with the rules set by

    color : #000;
    background-color : yellow;

if i'm wrong i'm wrong, but i think that will work, credit goes to
others for the bulk of that highligting code.

Site Timeline