digital_priest Posted November 4, 2006 Share Posted November 4, 2006 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 Objectfunction 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 functionfunction suggestOver(div_value) { div_value.className = 'suggest_link_over';}//Mouse out functionfunction suggestOut(div_value) { div_value.className = 'suggest_link';}//Click functionfunction 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 Quote Link to comment Share on other sites More sharing options...
tomfmason Posted November 4, 2006 Share Posted November 4, 2006 can you please wrap that in the [nobbc][code][/code][/nobbc] tags.. All of that italic code is making my eyes cross..lol Quote Link to comment Share on other sites More sharing options...
digital_priest Posted November 4, 2006 Author Share Posted November 4, 2006 Sorry... new at this, too. [code]//Gets the browser specific XmlHttpRequest Objectfunction 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 functionfunction suggestOver(div_value) { div_value.className = 'suggest_link_over';}//Mouse out functionfunction suggestOut(div_value) { div_value.className = 'suggest_link';}//Click functionfunction setName(value) { document.getElementById('idName').value = value; document.getElementById('location_suggest').innerHTML = ''; document.getElementById('author_suggest').innerHTML = '';}[/code] Quote Link to comment Share on other sites More sharing options...
tomfmason Posted November 4, 2006 Share Posted November 4, 2006 every thing likes fine.. Can you please post a link to a live version.Tom Quote Link to comment Share on other sites More sharing options...
digital_priest Posted November 7, 2006 Author Share Posted November 7, 2006 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] Quote Link to comment Share on other sites More sharing options...
ober Posted November 7, 2006 Share Posted November 7, 2006 Can you throw some alerts in and tell us where it might be stopping or crapping out? That link doesn't show us anything that can actually be used. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.