Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-23-2012, 05:53 AM   PM User | #1
Rickkap
New to the CF scene

 
Join Date: Aug 2010
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Rickkap is on a distinguished road
Spaces are removed with AJAX

Hi all,

This is really out of my league and i could do with some help.

I am trying to check the database to see if a some text exists using a form.

I came across this coding and it is perfect for what i need. I have incorporated it into my web site and the problem I'm having is when i type in a sentence to be checked, the end result is the same as what i input in with all the spaces removed. example "The cat ran up the hill" the result i get back when i click on check database is "Thecatranupthehill".

'm no good with AJAX so any help in trying to solve this issue would be greatly appreciated.

HTML FORM
Code:
<form method="post" action="javascript:void(0);" name="form1"> 

<table cellspacing="0"> 

<tr> 

<th><label for="newuserid">Sentence :</label></th> 

<td>	
<textarea rows="5" type="question" name="question" cols="82" id="question" tabindex="1" 
onKeyUp="OnChangedQuestion();"/></textarea></td> 
<td>
<input id="btnCheckAvailability" type="button" disabled="disabled" value="Check Database" 
onClick="OnCheckAvailability();"></td> 

<td><div id="Available"></div></td> 
</tr> 
</table> 

</form>
JAVASCRIPT
Code:
<script type="text/javascript">
//If our user enters data in the username input, then we need to enable our button 
function OnChangedQuestion() 
{ 
if(document.form1.newuserid.value == "") 
{ 
document.form1.btnCheckAvailability.disabled = true; 
} 
else 
{ 
document.form1.btnCheckAvailability.disabled = false; 
} 
}

function OnCheckAvailability() 
{ 
if(window.XMLHttpRequest) 
{ 
oRequest = new XMLHttpRequest(); 
} 
else if(window.ActiveXObject) 
{ 
oRequest = new ActiveXObject("Microsoft.XMLHTTP"); 
} 

oRequest.open("POST", "checkdatabase.asp", true); 
oRequest.onreadystatechange = UpdateCheckAvailability;

oRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
oRequest.send("strCmd=availability&strQuestion=" + document.form1.newuserid.value); 
}

function UpdateCheckAvailability() 
{ 
if(oRequest.readyState == 4) 
{ 
if(oRequest.status == 200) 
{ 
document.getElementById("Available").innerHTML = oRequest.responseText; 
} 
else 
{ 
document.getElementById("Available").innerHTML = "Error"; 
} 
} 
} 

</script>
ASP
Code:
<%
' Form Variables 
dim strCmd 
dim strQuestion

strCmd = trim(Request.Form("strCmd").Item) 
Session("strQuestion") = trim(Request.Form("strQuestion").Item)


If Session("strQuestion") ="The cat ran up the hill" Then
Response.Write("Error This Record Exists.") 
else 
Response.Write("Checked!!!!") 
end if 

Response.end
%>

Thanks in advance.

Rick

Last edited by Rickkap; 02-23-2012 at 05:57 AM..
Rickkap is offline   Reply With Quote
Old 02-23-2012, 05:12 PM   PM User | #2
Rowsdower!
Senior Coder

 
Rowsdower!'s Avatar
 
Join Date: Oct 2008
Location: Some say it's everything.
Posts: 2,007
Thanks: 5
Thanked 395 Times in 388 Posts
Rowsdower! has a spectacular aura aboutRowsdower! has a spectacular aura aboutRowsdower! has a spectacular aura about
First: Where is the "newuserid" input located? You use it twice in your javascript but it doesn't appear to exist in the form.

Second: You should need two "=" signs here in order to check (rather than assign) the variable's value:
Code:
If Session("strQuestion") ="The cat ran up the hill" Then
Patch up those two problems and see if the issue persists. This being an ASP script the most I can do is general syntax on that front. I can help with javascript errors but I am not ASP-savvy.

One last item, at which I am only guessing:

Code:
strCmd = trim(Request.Form("strCmd").Item) 
Session("strQuestion") = trim(Request.Form("strQuestion").Item)
Based on the VERY little ASP code I have seen I don't think that highlighted part is correct.


So those things all look wrong to me. The trouble is that so far I don't see how any of those items would strip whitespace between words...But again, my ASP knowledge could not fill a paper cup. Post back with the new results and we'll see if there has been any progress.
__________________
The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
See Mediocrity in its Infancy
It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
Seek and you shall find... basically:
validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting
Rowsdower! is offline   Reply With Quote
Old 02-23-2012, 07:20 PM   PM User | #3
Rickkap
New to the CF scene

 
Join Date: Aug 2010
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Rickkap is on a distinguished road
Hello

The newuserid is located here:

Code:
><label for="newuserid">
This is in the form.

In ASP the IF statement is there to check and not assign a variable


The code:
Code:
strCmd = trim(Request.Form("strCmd").Item) 
Session("strQuestion") = trim(Request.Form("strQuestion").Item)
Has been removed from the ASP file but really all this does is remove white spaces at the begining and end of the string

I am 99% sure this problem is being generated by the Javascript sending the sentence as a whole string and not as i type it in. My knowlege of Javascript is zero and i don't know how to fix this problem.

Regards

Rick
Rickkap is offline   Reply With Quote
Old 02-23-2012, 07:45 PM   PM User | #4
Rickkap
New to the CF scene

 
Join Date: Aug 2010
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Rickkap is on a distinguished road
After further investigation i think the problem lies in this:


Code:
document.getElementById("question").value;
What does this code do?
Rickkap is offline   Reply With Quote
Old 02-23-2012, 08:53 PM   PM User | #5
Rowsdower!
Senior Coder

 
Rowsdower!'s Avatar
 
Join Date: Oct 2008
Location: Some say it's everything.
Posts: 2,007
Thanks: 5
Thanked 395 Times in 388 Posts
Rowsdower! has a spectacular aura aboutRowsdower! has a spectacular aura aboutRowsdower! has a spectacular aura about
Quote:
Originally Posted by Rickkap View Post
Hello

The newuserid is located here:

Code:
><label for="newuserid">
This is in the form.
Either I'm making another ASP mistake or else you didn't read my question very carefully! At this point, I'm honestly not sure which it is.

Where is the input element with ID of "newuserid" located? What you have there is a label element with a "for" attribute pointing to a non-existent input element. Even if the label had an ID of "newuserid" requesting its value would get you a javascript error since it doesn't have a value property to call upon.

Then your javascript goes on to reference the value of the element retrieved by the ID "newuserid" - which is an empty set, really. No element in the page has that ID. So unless this is another coding oddity of ASP I think you will need to pay attention to this one.

Quote:
Originally Posted by Rickkap View Post
...In ASP the IF statement is there to check and not assign a variable...
Crap! Can't argue with that one. Like I said, I have nil in the way of ASP experience. Good to know, though.

Quote:
Originally Posted by Rickkap View Post
...The code:
Code:
strCmd = trim(Request.Form("strCmd").Item) 
Session("strQuestion") = trim(Request.Form("strQuestion").Item)
Has been removed from the ASP file but really all this does is remove white spaces at the begining and end of the string...
Again, possibly showing my ASP ignorance, but if you remove BOTH of those two lines of code how do you have anything at all in the Session("strQuestion") variable you are working with later in the script? It looks to me like you just need to remove the ".Item" part from each of those two lines (I guess you can still delete the first of those two lines, though, since you never use the strCmd variable anywhere).

As for the trim functionality, I figured as much (given the "trim" function) but the format looked wrong for the variable being passed to the trim function. For all I know, using ".Item" may split the string and trim each piece before reassembling. I wouldn't know with ASP - it could do any number of different things. But in any case it doesn't match sample form handling code I have seen so far in my life.

My deeper confusion about your problem is that, from what I can see, regardless of your input you should be receiving only one of two possible responses (either "Error This Record Exists." or "Checked!!!!") if your ASP code is actually working. Yet you are reporting an entirely separate response.

This would seem to indicate that either 1) you are not ever getting submitted to your ASP page and something else is putting a response into your target div, or 2) you have another problem somewhere within your ASP script that is causing it to vomit your output back to you rather than test it correctly in the "if/else" statements and return what it ought to return. Even if the whitespace was not removed it would still be a problem to receive your own statement back since it is not one of the two things that is supposed to be sent back.

If it was *just* a problem of your javascript sending the wrong variable value you would still be seeing either the "error" message or the "checked" message, wouldn't you? Or is ASP completely in bizarro world?
__________________
The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
See Mediocrity in its Infancy
It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
Seek and you shall find... basically:
validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

Last edited by Rowsdower!; 02-23-2012 at 09:10 PM.. Reason: typo
Rowsdower! is offline   Reply With Quote
Old 02-23-2012, 09:00 PM   PM User | #6
Rowsdower!
Senior Coder

 
Rowsdower!'s Avatar
 
Join Date: Oct 2008
Location: Some say it's everything.
Posts: 2,007
Thanks: 5
Thanked 395 Times in 388 Posts
Rowsdower! has a spectacular aura aboutRowsdower! has a spectacular aura aboutRowsdower! has a spectacular aura about
Quote:
Originally Posted by Rickkap View Post
After further investigation i think the problem lies in this:


Code:
document.getElementById("question").value;
What does this code do?
This code searches the DOM for an element whose id attribute is equal to "question" and then accesses that element's value property and returns it if present or returns undefined if not present.

But where is this used in your script? Is there anything else that hasn't been posted yet that is in play with this form of yours?
__________________
The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
See Mediocrity in its Infancy
It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
Seek and you shall find... basically:
validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting
Rowsdower! is offline   Reply With Quote
Old 03-18-2012, 03:50 PM   PM User | #7
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
The core problem is that parameter sent via Ajax need to be URI encoded in order to preserve special characters (including the space character which needs to be encoded to %20). This can be achieved using the encodeURIComponent() method.

Code:
oRequest.send("strCmd=availability&strQuestion=" + encodeURIComponent(document.form1.newuserid.value));
You don't have to change anything in ASP. The encoded characters will automatically be decoded.
devnull69 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:18 PM.


Advertisement
Log in to turn off these ads.