Anishgiri
07-08-2010, 09:03 AM
I have found an auto suggest script that is working. I am trying to add some codes in order for html select form to pass its value to ajax.js then from there it will pass the value to script_page.php something like these,
$str2= strtolower($_GET['content2']);
so I can create at script_page.php a condition on what sql query to do.
But to no avail, it does not pass the value. I get warning of undefined index content2. Below is the 2 main files.
Ajax.js
subject_id = '';
function handleHttpResponse() {
if (http.readyState == 4) {
if (subject_id != '') {
document.getElementById(subject_id).innerHTML = http.responseText;
}
}
}
function getHTTPObject() {
var xmlhttp;
/*@cc_on
@if (@_jscript_version >= 5)
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}
@else
xmlhttp = false;
@end @*/
if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
try {
xmlhttp = new XMLHttpRequest();
} catch (e) {
xmlhttp = false;
}
}
return xmlhttp;
}
var http = getHTTPObject(); // We create the HTTP Object
function getScriptPage(div_id,content_id)
{
subject_id = div_id;
content = document.getElementById(content_id).value;
http.open("GET", "script_page.php?content=" + escape(content), true);
http.onreadystatechange = handleHttpResponse;
http.send(null);
if(content.length>0)
box('1');
else
box('0');
}
function highlight(action,id)
{
if(action)
document.getElementById('word'+id).bgColor = "#C2B8F5";
else
document.getElementById('word'+id).bgColor = "#F8F8F8";
}
function display(word)
{
document.getElementById('text_content').value = word;
document.getElementById('box').style.display = 'none';
document.getElementById('text_content').focus();
}
function box(act)
{
if(act=='0')
{
document.getElementById('box').style.display = 'none';
}
else
document.getElementById('box').style.display = 'block';
}
search_page.php
<?php
include('config.php');
$str = strtolower($_GET['content']);
if(strlen($str))
{
$sel = mysql_query("select * from table1 where fname like '".trim($str)."%'");
if(mysql_num_rows($sel))
{
echo " <table align =\"center\" border =\"0\" width=\"50%\">\n";
if(mysql_num_rows($sel))
{
echo "<script language=\"javascript\">box('1');</script>";
while($row = mysql_fetch_array($sel))
{
$fname = str_ireplace($str,"<b>".$str."</b>",($row['fname']));
echo "<tr id=\"word".$row['id']."\" onmouseover=\"highlight(1,'".$row['id']."');\" onmouseout=\"highlight(0,'".$row['id']."');\" onClick=\"display('".$row['fname']."' );\" >\n<td>".$fname."</td> \n</tr>\n";
}
}
echo "</table>";
}
}
else
{
echo "<script language=\"javascript\">box('0');</script>";
}
?>
index.php page have select form like this. I tried adding something, and I think I am not doing it right, cause it does not pass the value.
<Select NAME="search_field" onClick="getScriptPage2('select_content')" id="select_content" >
<Option VALUE="fname">First Name</option>
<Option VALUE="lname">Last Name</option>
</Select>
I added this to ajax.js, but it does not work.
function getScriptPage2(select_id)
{
subject_id = div_id;
content2 = document.getElementById(select_id).value;
http.open("GET", "script_page.php?content2=" + escape(content2), true);
http.onreadystatechange = handleHttpResponse;
http.send(null);
}
Hope somebody can help me with this, I am just starting out with ajax and javascript. Thanks.
$str2= strtolower($_GET['content2']);
so I can create at script_page.php a condition on what sql query to do.
But to no avail, it does not pass the value. I get warning of undefined index content2. Below is the 2 main files.
Ajax.js
subject_id = '';
function handleHttpResponse() {
if (http.readyState == 4) {
if (subject_id != '') {
document.getElementById(subject_id).innerHTML = http.responseText;
}
}
}
function getHTTPObject() {
var xmlhttp;
/*@cc_on
@if (@_jscript_version >= 5)
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}
@else
xmlhttp = false;
@end @*/
if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
try {
xmlhttp = new XMLHttpRequest();
} catch (e) {
xmlhttp = false;
}
}
return xmlhttp;
}
var http = getHTTPObject(); // We create the HTTP Object
function getScriptPage(div_id,content_id)
{
subject_id = div_id;
content = document.getElementById(content_id).value;
http.open("GET", "script_page.php?content=" + escape(content), true);
http.onreadystatechange = handleHttpResponse;
http.send(null);
if(content.length>0)
box('1');
else
box('0');
}
function highlight(action,id)
{
if(action)
document.getElementById('word'+id).bgColor = "#C2B8F5";
else
document.getElementById('word'+id).bgColor = "#F8F8F8";
}
function display(word)
{
document.getElementById('text_content').value = word;
document.getElementById('box').style.display = 'none';
document.getElementById('text_content').focus();
}
function box(act)
{
if(act=='0')
{
document.getElementById('box').style.display = 'none';
}
else
document.getElementById('box').style.display = 'block';
}
search_page.php
<?php
include('config.php');
$str = strtolower($_GET['content']);
if(strlen($str))
{
$sel = mysql_query("select * from table1 where fname like '".trim($str)."%'");
if(mysql_num_rows($sel))
{
echo " <table align =\"center\" border =\"0\" width=\"50%\">\n";
if(mysql_num_rows($sel))
{
echo "<script language=\"javascript\">box('1');</script>";
while($row = mysql_fetch_array($sel))
{
$fname = str_ireplace($str,"<b>".$str."</b>",($row['fname']));
echo "<tr id=\"word".$row['id']."\" onmouseover=\"highlight(1,'".$row['id']."');\" onmouseout=\"highlight(0,'".$row['id']."');\" onClick=\"display('".$row['fname']."' );\" >\n<td>".$fname."</td> \n</tr>\n";
}
}
echo "</table>";
}
}
else
{
echo "<script language=\"javascript\">box('0');</script>";
}
?>
index.php page have select form like this. I tried adding something, and I think I am not doing it right, cause it does not pass the value.
<Select NAME="search_field" onClick="getScriptPage2('select_content')" id="select_content" >
<Option VALUE="fname">First Name</option>
<Option VALUE="lname">Last Name</option>
</Select>
I added this to ajax.js, but it does not work.
function getScriptPage2(select_id)
{
subject_id = div_id;
content2 = document.getElementById(select_id).value;
http.open("GET", "script_page.php?content2=" + escape(content2), true);
http.onreadystatechange = handleHttpResponse;
http.send(null);
}
Hope somebody can help me with this, I am just starting out with ajax and javascript. Thanks.