| | |
PHP Ajax Search
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Apr 2007
Posts: 4
Reputation:
Solved Threads: 0
Hi,
I'm trying to put together a PHP Ajax search against an mySQL database yet doesnt seem to work .. HELP!
I had to resort to using frameset which I dont really want to do.
what I'm trying to is perform a search against a mysql database and display the results on the same page. Want the ability to perform multiple search without a refresh or change of page.
so far this is what i have
Index.html
Searchuser.js
The code calls a search.php which performs the search against the database and creates the tables.
Search.php echos the tables using DIV .
Any ideas on how to improve this ?
I'm trying to put together a PHP Ajax search against an mySQL database yet doesnt seem to work .. HELP!
I had to resort to using frameset which I dont really want to do.
what I'm trying to is perform a search against a mysql database and display the results on the same page. Want the ability to perform multiple search without a refresh or change of page.
so far this is what i have
Index.html
PHP Syntax (Toggle Plain Text)
<html> <head> <script src="selectuser.js"></script> </head> <body> <div align="center" <form name="form" action="search.php" method="get"> <input type="text" name="q" /> <input type="submit" name="Submit" value="Search" /> <div align="right"> <select name="list"> <option value="10"> 10 </option> <option value="20"> 20 </option> <option value="50"> 50 </option> </select> </div> </form> </div> <div id="txtHint"><b>User info will be listed here.</b></div> </body> </html>
Searchuser.js
PHP Syntax (Toggle Plain Text)
var xmlHttpfunction showUser(str) { xmlHttp=GetXmlHttpObject() if (xmlHttp==null) { alert ("Browser does not support HTTP Request") return } var url="search.php" url=url+"?q="+str+"?list=" url=url+"&sid="+Math.random() xmlHttp.onreadystatechange=stateChanged xmlHttp.open("GET",url,true) xmlHttp.send(null) } function stateChanged() { if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { document.getElementById("txtHint").innerHTML=xmlHttp.responseText } }function GetXmlHttpObject() { var xmlHttp=null; try { // Firefox, Opera 8.0+, Safari xmlHttp=new XMLHttpRequest(); } catch (e) { //Internet Explorer try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } } return xmlHttp; }
The code calls a search.php which performs the search against the database and creates the tables.
Search.php echos the tables using DIV .
Any ideas on how to improve this ?
•
•
•
•
Hi,
I'm trying to put together a PHP Ajax search against an mySQL database yet doesnt seem to work .. HELP!
I had to resort to using frameset which I dont really want to do.
what I'm trying to is perform a search against a mysql database and display the results on the same page. Want the ability to perform multiple search without a refresh or change of page.
so far this is what i have
Index.html
PHP Syntax (Toggle Plain Text)
<html> <head> <script src="selectuser.js"></script> </head> <body> <div align="center" <form name="form" action="search.php" method="get"> <input type="text" name="q" /> <input type="submit" name="Submit" value="Search" /> <div align="right"> <select name="list"> <option value="10"> 10 </option> <option value="20"> 20 </option> <option value="50"> 50 </option> </select> </div> </form> </div> <div id="txtHint"><b>User info will be listed here.</b></div> </body> </html>
Searchuser.js
PHP Syntax (Toggle Plain Text)
var xmlHttpfunction showUser(str) { xmlHttp=GetXmlHttpObject() if (xmlHttp==null) { alert ("Browser does not support HTTP Request") return } var url="search.php" url=url+"?q="+str+"?list=" url=url+"&sid="+Math.random() xmlHttp.onreadystatechange=stateChanged xmlHttp.open("GET",url,true) xmlHttp.send(null) } function stateChanged() { if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { document.getElementById("txtHint").innerHTML=xmlHttp.responseText } }function GetXmlHttpObject() { var xmlHttp=null; try { // Firefox, Opera 8.0+, Safari xmlHttp=new XMLHttpRequest(); } catch (e) { //Internet Explorer try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } } return xmlHttp; }
The code calls a search.php which performs the search against the database and creates the tables.
Search.php echos the tables using DIV .
Any ideas on how to improve this ?
There are a couple of syntax errors in your JS.
Searchuser.js
PHP Syntax (Toggle Plain Text)
var xmlHttpfunction showUser(str) {
should be
PHP Syntax (Toggle Plain Text)
var xmlHttp function showUser(str) {
Probably best to end all your JS commands with the semi-colon. This allows more than one on a line without having syntax errors. (Useful if you want to compress your JS also)
The URL you are creating will have two ? in it.
PHP Syntax (Toggle Plain Text)
var url="search.php" url=url+"?q="+str+"?list=" url=url+"&sid="+Math.random()
I believe you want:
PHP Syntax (Toggle Plain Text)
var url="search.php"; url=url+"?q="+str+"&list="; url=url+"&sid="+Math.random();
or simpler:
PHP Syntax (Toggle Plain Text)
var url="search.php"; url += "?q="+str+"&list="; url += "&sid="+Math.random();
Last edited by digital-ether; Apr 11th, 2007 at 4:05 pm.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
•
•
Join Date: Jan 2009
Posts: 1
Reputation:
Solved Threads: 0
•
•
•
•
Hi,
I'm trying to put together a PHP Ajax search against an mySQL database yet doesnt seem to work .. HELP!
I had to resort to using frameset which I dont really want to do.
what I'm trying to is perform a search against a mysql database and display the results on the same page. Want the ability to perform multiple search without a refresh or change of page.
so far this is what i have
Index.html
PHP Syntax (Toggle Plain Text)
<html> <head> <script src="selectuser.js"></script> </head> <body> <div align="center" <form name="form" action="search.php" method="get"> <input type="text" name="q" /> <input type="submit" name="Submit" value="Search" /> <div align="right"> <select name="list"> <option value="10"> 10 </option> <option value="20"> 20 </option> <option value="50"> 50 </option> </select> </div> </form> </div> <div id="txtHint"><b>User info will be listed here.</b></div> </body> </html>
Searchuser.js
PHP Syntax (Toggle Plain Text)
var xmlHttpfunction showUser(str) { xmlHttp=GetXmlHttpObject() if (xmlHttp==null) { alert ("Browser does not support HTTP Request") return } var url="search.php" url=url+"?q="+str+"?list=" url=url+"&sid="+Math.random() xmlHttp.onreadystatechange=stateChanged xmlHttp.open("GET",url,true) xmlHttp.send(null) } function stateChanged() { if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { document.getElementById("txtHint").innerHTML=xmlHttp.responseText } }function GetXmlHttpObject() { var xmlHttp=null; try { // Firefox, Opera 8.0+, Safari xmlHttp=new XMLHttpRequest(); } catch (e) { //Internet Explorer try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } } return xmlHttp; }
The code calls a search.php which performs the search against the database and creates the tables.
Search.php echos the tables using DIV .
Any ideas on how to improve this ?
•
•
Join Date: May 2007
Posts: 81
Reputation:
Solved Threads: 1
something is wrong on this area:
what you're doing there is you're refreshing the whole page when the search button is triggered which defeats the purpose of ajax. Now you created search.js which will be responsible for contacting the search.php BUT the problem is, your interface does not call the JS FUNCTION for connecting.
here's my suggestion
now when the search button is click, it will call the showuser function from the javascript.... But there's still a problem, you need to pass the "NAME" to the server. At the moment the "NAME" is in the the text box and we're going to get that using javascript again.. so you might want to modify your searchUser function with something like:
you know AJAX is kindof long to explain, why dont you just follow the examples correctly provided on your tutorial? Because the way I see it you have syntax errors on your javascript.. oh and never ever do ajax if you don't know javascripting
html Syntax (Toggle Plain Text)
<form name="form" action="search.php" method="get"> <input type="text" name="q" /> <input type="submit" name="Submit" value="Search" />
what you're doing there is you're refreshing the whole page when the search button is triggered which defeats the purpose of ajax. Now you created search.js which will be responsible for contacting the search.php BUT the problem is, your interface does not call the JS FUNCTION for connecting.
here's my suggestion
html Syntax (Toggle Plain Text)
<form name="myform"> <input type="text" name="q" /> <input type="button" name="button" value="Search" onclick="showUser()" />
now when the search button is click, it will call the showuser function from the javascript.... But there's still a problem, you need to pass the "NAME" to the server. At the moment the "NAME" is in the the text box and we're going to get that using javascript again.. so you might want to modify your searchUser function with something like:
JavaScript Syntax (Toggle Plain Text)
function showUser() { var str = document.myform.q.value; xmlHttp=GetXmlHttpObject(); if (xmlHttp==null) { alert ("Browser does not support HTTP Request") return } //rest of the code here //...
you know AJAX is kindof long to explain, why dont you just follow the examples correctly provided on your tutorial? Because the way I see it you have syntax errors on your javascript.. oh and never ever do ajax if you don't know javascripting
Last edited by peter_budo; Jan 18th, 2009 at 2:09 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
![]() |
Similar Threads
- php drop down menu to search multiple sql tables (PHP)
- need help setting up a php/mysql search (PHP)
- it's simple but yet complicated...php and mysql search.. (PHP)
- Using Search Engine Friendly PHP URLs (PHP)
Other Threads in the PHP Forum
- Previous Thread: Problem with dynamic paths
- Next Thread: Difficulties in configuring wamp server, apache,php and mysql
| Thread Tools | Search this Thread |
apache api array beginner binary body broken buttons cakephp checkbox class cms code cron curl database date date/time display dynamic ebooks echo email error file files folder form forms function functions global google href htaccess html image include insert ip javascript joomla limit link list login mail mediawiki menu mlm msqli_multi_query multiple mycodeisbad mysql number oop parameter paypal pdf php phpincludeissue problem query radio random recourse recursion regex remote script search seo server sessions sms source sp space speed sql static subdomain syntax system table tag tutorial update upload url validator variable vbulletin video web webdesign white wordpress xml youtube






