Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


xmlHttp.responseText is empty if call triggered by carriage return

Posted on 2009-04-16
Medium Priority
Last Modified: 2012-05-06
I have a web menu where the user clicks  a search link which opens a small window where a ticket number can be keyed in.  The javascript to create the small window is below.  If the user enters the ticket number, then clicks on the button called 'search', it all works as one would expect.  If the user puts a ticket number in and presses the return key, the responseText comes back as empty.  The perl routine ticketExists.pl just returns the ticket number if it is valid.

The alert(url) shows the correct call to ticketExists.pl, whether it's triggered by the enter key or the click on the 'Search' button.  However, the return data is the ticket number only when the button is pressed, and not when the enter key is struck.  I want them to both give the same result.
function searchBox(){
  var myWin = open("","SearchWin","width=400,height=200,menubar=no,scrollbars=no,titlebar=no,toolbar=no,alwaysRaised=yes");
myWin.document.writeln('<script type="text/javascript">');
myWin.document.writeln('function doSearch(){');
myWin.document.writeln('var xmlHttp;');
myWin.document.writeln('try  {  xmlHttp=new XMLHttpRequest();  } catch (e) {');
myWin.document.writeln('  try    {    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");    }  catch (e) {');
myWin.document.writeln('    try      {      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");      }    catch (e){');
myWin.document.writeln('      alert("Your browser does not support AJAX!");');
myWin.document.writeln('      return false;');
myWin.document.writeln('      }');
myWin.document.writeln('    }');
myWin.document.writeln('  }');
myWin.document.writeln('xmlHttp.onreadystatechange=function() {');
myWin.document.writeln('  {');
myWin.document.writeln('  var ticketExists=xmlHttp.responseText;');
myWin.document.writeln('  alert("response " + ticketExists);');
myWin.document.writeln('  if (ticketExists != ""){');
myWin.document.writeln('	window.opener.location.href="enterIssues.pl?action=showIssue&fromstatus=All&id="+ticketExists;');
myWin.document.writeln('  } else {');
myWin.document.writeln('	alert("Ticket does not exist");');
myWin.document.writeln('  }');
myWin.document.writeln('  window.close();');
myWin.document.writeln( 'var ticket=document.getElementById("ticket").value;');
myWin.document.writeln( 'ticket = ticket.replace(String.fromCharCode(13),"");');
myWin.document.writeln( 'ticket = ticket.replace(String.fromCharCode(10),"");');
myWin.document.writeln('var url="ticketExists.pl?ticket="+ticket;alert(url);');
  myWin.document.writeln('<form name="searchForm" id="searchForm">');
  myWin.document.writeln('<table><tr><td>Ticket</td><td><input type="text" name="ticket" id="ticket" onKeyPress="if (event.keyCode==13){doSearch();}"></td></tr>');
  myWin.document.writeln('<tr><td></td><td><input type="button" name="search" id="search" value="Search" onClick="doSearch();">');
  myWin.document.writeln('<input type="button" name="cancel" id="cancel"value="Cancel" onClick="window.close();"></td></tr></table></form>');

Open in new window

Question by:jpopowski
LVL 18

Accepted Solution

Hube02 earned 1000 total points
ID: 24164556
I think the problem is that when the user hits enter the form is submitting and not calling your JavaScript function.

Try adding the following to the form tag

onsubmit="doSearch(); return false"

Author Closing Comment

ID: 31571294
Thank you so much!  I been struggling with this for a couple of evenings and just didn't know what to try next.  I really appreciate it.

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There is basically two types of AJAX request in mootools. Request  and Request.HTML Request: Request is the basic XHR request class in MooTools. While not extremely useful on its own, it provides the basic functionality for both Request.HTM…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question