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.
Thanks!
Re: PHP to AJAX - a little off topic
am 27.11.2007 00:04:51 von Kailash Nadh
On Nov 26, 10:43 pm, John wrote:
> Hello,
>
> 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.
>
> Thanks!
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.
http://www.w3schools.com/ajax/default.asp
Kailash Nadh | http://kailashnadh.name
Re: PHP to AJAX - a little off topic
am 27.11.2007 00:45:59 von John
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
am 27.11.2007 01:23:11 von shimmyshack
On Nov 26, 11:45 pm, John 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
li>
item 2>
) 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 '' . "\n";
exit;
?>
target="notverywellhiddenframe">
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
am 27.11.2007 02:25:02 von John
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
am 28.11.2007 16:50:56 von shimmyshack
On Nov 27, 1:25 am, John wrote:
> 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?
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
{
highlightWord(node.childNodes[hi_cn],word);
}
}
// 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";
hiword.appendChild(hiwordtext);
pn.insertBefore(before,node);
pn.insertBefore(hiword,node);
pn.insertBefore(after,node);
pn.removeChild(node);
}
}
}
}
function googleSearchHighlight()
{
if (!document.createElement) return;
ref = document.referrer;
ref = (ref) ? ref : location.search;
if (ref.indexOf('?') == -1) return;
qs = ref.substr(ref.indexOf('?')+1);
qsa = qs.split('&');
for (i=0;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
{
highlightWord(document.getElementsByTagName("body")[0],words [w]);
}
}
}
}
i think thats all you need for
http://www.example.org/letter.htm?q=hello
which will highlight the word hello
with the rules set by
..highlight
{
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.