Jump to content

Recommended Posts

Thanks for helping a noob learn a little ajax!

I've been using a couple of different tutorials to mix up a Google-suggest-like piece of an application. It's supposed to suggest a name of an author or location (depending on the form element) as the user types the name. I think my problem is in the js file. I've included it here. Can anyone find something that is broken? It worked at one time for a single form element, but doesn't work for two...?
[quote]//Gets the browser specific XmlHttpRequest Object
function getXmlHttpRequestObject() {
if (window.XMLHttpRequest) {
return new XMLHttpRequest();
}
else if(window.ActiveXObject) {
return new ActiveXObject("Microsoft.XMLHTTP");
}
else {
alert("Your browser is incompatible. Please upgrade.");
}
}

var suggestReq = getXmlHttpRequestObject();

//Called from keyup on the search textbox.
//Starts the AJAX request.
function nameSuggest(tableName) {
if (suggestReq.readyState == 4 || suggestReq.readyState == 0) {
var idName = 'sermon' + tableName;
var str = escape(document.getElementById(idName).value);
suggestReq.open("GET", 'namesuggest.php?tablename=' + tableName + '&searchfor=' + str, true);
if ('locations'==tableName) {
suggestReq.onreadystatechange = handleLocationSuggest;
}
elseif ('authors'==tableName) {
suggestReq.onreadystatechange = handleAuthorSuggest;
}
suggestReq.send(null);
}
}

//Called when the AJAX response is returned.
function handleLocationSuggest() {
if (suggestReq.readyState == 4) {
var ss = document.getElementById('location_suggest')
ss.innerHTML = '';
var str = suggestReq.responseText.split("\n");
for(i=0; i < str.length - 1; i++) {
//Build our element string.  This is cleaner using the DOM, but
//IE doesn't support dynamically added attributes.
var suggest = '<div onmouseover="javascript:suggestOver(this);" ';
suggest += 'onmouseout="javascript:suggestOut(this);" ';
suggest += 'onclick="javascript:setName(this.innerHTML);" ';
suggest += 'class="suggest_link">' + str[i] + '</div>';
ss.innerHTML += suggest;
}
}
}

function handleAuthorSuggest() {
if (suggestReq.readyState == 4) {
var ss = document.getElementById('author_suggest')
ss.innerHTML = '';
var str = suggestReq.responseText.split("\n");
for(i=0; i < str.length - 1; i++) {
//Build our element string.  This is cleaner using the DOM, but
//IE doesn't support dynamically added attributes.
var suggest = '<div onmouseover="javascript:suggestOver(this);" ';
suggest += 'onmouseout="javascript:suggestOut(this);" ';
suggest += 'onclick="javascript:setName(this.innerHTML);" ';
suggest += 'class="suggest_link">' + str[i] + '</div>';
ss.innerHTML += suggest;
}
}
}


//Mouse over function
function suggestOver(div_value) {
div_value.className = 'suggest_link_over';
}
//Mouse out function
function suggestOut(div_value) {
div_value.className = 'suggest_link';
}

//Click function
function setName(value) {
document.getElementById('idName').value = value;
document.getElementById('location_suggest').innerHTML = '';
document.getElementById('author_suggest').innerHTML = '';

}

[/quote]

Thanks for the many eyes!

--digital_priest
Link to comment
https://forums.phpfreaks.com/topic/26166-suggest-like-script-broken-no-errors/
Share on other sites

Sorry... new at this, too.
[code]//Gets the browser specific XmlHttpRequest Object
function getXmlHttpRequestObject() {
if (window.XMLHttpRequest) {
return new XMLHttpRequest();
}
else if(window.ActiveXObject) {
return new ActiveXObject("Microsoft.XMLHTTP");
}
else {
alert("Your browser is incompatible. Please upgrade.");
}
}

var suggestReq = getXmlHttpRequestObject();

//Called from keyup on the search textbox.
//Starts the AJAX request.
function nameSuggest(tableName) {
if (suggestReq.readyState == 4 || suggestReq.readyState == 0) {
var idName = 'sermon' + tableName;
var str = escape(document.getElementById(idName).value);
suggestReq.open("GET", 'namesuggest.php?tablename=' + tableName + '&searchfor=' + str, true);
if ('locations'==tableName) {
suggestReq.onreadystatechange = handleLocationSuggest;
}
elseif ('authors'==tableName) {
suggestReq.onreadystatechange = handleAuthorSuggest;
}
suggestReq.send(null);
}
}

//Called when the AJAX response is returned.
function handleLocationSuggest() {
if (suggestReq.readyState == 4) {
var ss = document.getElementById('location_suggest')
ss.innerHTML = '';
var str = suggestReq.responseText.split("\n");
for(i=0; i < str.length - 1; i++) {
//Build our element string.  This is cleaner using the DOM, but
//IE doesn't support dynamically added attributes.
var suggest = '<div onmouseover="javascript:suggestOver(this);" ';
suggest += 'onmouseout="javascript:suggestOut(this);" ';
suggest += 'onclick="javascript:setName(this.innerHTML);" ';
suggest += 'class="suggest_link">' + str[i] + '</div>';
ss.innerHTML += suggest;
}
}
}

function handleAuthorSuggest() {
if (suggestReq.readyState == 4) {
var ss = document.getElementById('author_suggest')
ss.innerHTML = '';
var str = suggestReq.responseText.split("\n");
for(i=0; i < str.length - 1; i++) {
//Build our element string.  This is cleaner using the DOM, but
//IE doesn't support dynamically added attributes.
var suggest = '<div onmouseover="javascript:suggestOver(this);" ';
suggest += 'onmouseout="javascript:suggestOut(this);" ';
suggest += 'onclick="javascript:setName(this.innerHTML);" ';
suggest += 'class="suggest_link">' + str[i] + '</div>';
ss.innerHTML += suggest;
}
}
}


//Mouse over function
function suggestOver(div_value) {
div_value.className = 'suggest_link_over';
}
//Mouse out function
function suggestOut(div_value) {
div_value.className = 'suggest_link';
}

//Click function
function setName(value) {
document.getElementById('idName').value = value;
document.getElementById('location_suggest').innerHTML = '';
document.getElementById('author_suggest').innerHTML = '';

}

[/code]
Thanks for taking a look at it.  :)

[url=http://www.amosglenn.com/sermons/sermonadmin.php]http://www.amosglenn.com/sermons/sermonadmin.php[/url]

While I'm here, I'll share the php page that the script uses to fetch the information from the database:

[code]
<?php
/* namesuggest.php */

include ("sermonconnect.inc");

//Make sure that a value was sent.
if (isset($_GET['tablename']) && $_GET['tablename'] != '') {
if (isset($_GET['searchfor']) && $_GET['searchfor'] != '') {
//Add slashes to any quotes to avoid SQL problems.
$tname = addslashes($_GET['tablename']);
$searchterm = addslashes($_GET['searchfor']);
$query = "SELECT *
FROM $tname
WHERE name like '%".$searchterm."%'
ORDER BY name";
$results = mysql_query($query);
if (!results) {
echo ("<p>Error collecting names: ".mysql_error()."</p>");
}
else {
while($suggest = mysql_fetch_array($results)) {
//Return each page title seperated by a newline.
echo $suggest['name'] . "\n";
}
}
}
}

?>
[/code]
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.