AJAX error question

  • JonDoeCA
  • Newbie
  • Newbie
  • JonDoeCA
  • Posts: 7

Post 3+ Months Ago

Hi all,

I've got a question about an AJAX issue that has me stumped. Hopefully someone else knows what the issue is. Below is a very simple AJAX request that works just fine until I put a simple statement inside of it. For the sake of simplicity, I've modified this AJAX request instead of posting the more complicated one I'm using - but essentially this example and the real code I'm using are failing for the same reason, because of line #4, (var wlen = str.value.length). I had this working on my Mac, uploaded it to a Ubuntu server and it failed (no errors, just didn't work). Then for whatever reason, it stopped working on my Mac too.

JAVASCRIPT Code: [ Select ]
<script type="text/javascript">
function name_search(str)
{
var wlen = str.value.length;
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("name_div").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","/it/device/action/name_search_new.php?q="+str,true);
xmlhttp.send();
}
</script>
 
  1. <script type="text/javascript">
  2. function name_search(str)
  3. {
  4. var wlen = str.value.length;
  5. var xmlhttp;
  6. if (window.XMLHttpRequest)
  7.   {// code for IE7+, Firefox, Chrome, Opera, Safari
  8.   xmlhttp=new XMLHttpRequest();
  9.   }
  10. else
  11.   {// code for IE6, IE5
  12.   xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  13.   }
  14. xmlhttp.onreadystatechange=function()
  15.   {
  16.   if (xmlhttp.readyState==4 && xmlhttp.status==200)
  17.     {
  18.     document.getElementById("name_div").innerHTML=xmlhttp.responseText;
  19.     }
  20.   }
  21. xmlhttp.open("GET","/it/device/action/name_search_new.php?q="+str,true);
  22. xmlhttp.send();
  23. }
  24. </script>
  25.  


The name_search_new.php page grabs similar values to what is typed in the text box and returns those values. This page is irrelevant to this issue though because any php script will always fail with the line 4 new variable/assignment and it will always work with the variable/assignment gone.

If it helps, the request for this function is listed below... It also fails within a 'textarea' box too.
HTML Code: [ Select ]
<input type="text" name="name" id="name" value="" style="color:red" placeholder="Device/Item (Required)" onkeyup="name_search(this.value)" />
 
  1. <input type="text" name="name" id="name" value="" style="color:red" placeholder="Device/Item (Required)" onkeyup="name_search(this.value)" />
  2.  



Thanks in advance for your help!

Jay
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • JonDoeCA
  • Newbie
  • Newbie
  • JonDoeCA
  • Posts: 7

Post 3+ Months Ago

I got it working again. The issues is with 'str'. I would assume that "str.value.length" would determine the char count of str, but oddly enough, it's the name of the textbox that's needed here. I'd need to assign the variable 'tb' to the text box, then use tb.value.length for wlen. Long, hard lesson learned.
  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Posts: 9089
  • Loc: Seattle, WA & Phoenix, AZ

Post 3+ Months Ago

Thanks for sharing your solution, sorry nobody could help out before you fixed it. I am sure this could be helpful for someone else down the road :)

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 156 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.