shadiadiph Posted June 23, 2010 Share Posted June 23, 2010 I have been building an AJAX form my javascript knowledge is terrible but I am having a horribble time with javascript variables I assign the response var a = xmlHttp.responseText etc result which it gets fine why if i get a response from an xml node the javascript variable can compare if (var a =="error") { do this } else { do that} but with the .responseText reply will not recognise it and jump straight to the else I have tried returning a number as an error false as an error javascript just won't seem to read a returned variable properly I even tried .length==0 no joy??? Any tips? I'm beginning to think javascript and php were not supposed to coexist Quote Link to comment Share on other sites More sharing options...
haku Posted June 24, 2010 Share Posted June 24, 2010 It appears you think that the alphabet and punctuation, and line breaks shouldn't co-exist either. Quote Link to comment Share on other sites More sharing options...
shadiadiph Posted June 24, 2010 Author Share Posted June 24, 2010 Ha ha Haku so how would you validate the word error against something else in JavaScript if (var a =="error") doesn't work? By the way I read 4 different alphabets and speak 6 languages. Quote Link to comment Share on other sites More sharing options...
shadiadiph Posted June 24, 2010 Author Share Posted June 24, 2010 I don't get why if my php returns return "error"; and then my javascript xmlHttp.responseText; var length is 8 if i dont return anything it says it is 3 in length?? Quote Link to comment Share on other sites More sharing options...
haku Posted June 24, 2010 Share Posted June 24, 2010 By the way I read 4 different alphabets and speak 6 languages. Isn't that impressive. Let me give you a cookie. Anyways, my point was that if you don't write your problem in a manner that's easy to understand, you aren't likely to get help for it. Quote Link to comment Share on other sites More sharing options...
shadiadiph Posted June 24, 2010 Author Share Posted June 24, 2010 Ok I have a form it is sending country,state,city,postal step by step with AJAX to a php page it either returns "error" if the next set of data is not found and does not display the div. Problem is when i use var result = xmlHttp.responseText; if (result=="error') { //hide div } else { // display div } It doesn't recognise the word error but displays error as the else result I have tried var resultlen = result.length; it keeps returning 8 if I don't return anything the length is 3 my question is why can't javascript not recognise the string with the value of error? When I did this a different way using <xml> calling the node values it recognised the text string if (result=="error") etc it understood that but why not with xmlHttp.responseText; and how to deal with this? Quote Link to comment Share on other sites More sharing options...
haku Posted June 24, 2010 Share Posted June 24, 2010 When you say it returns 'error', you mean it returns the string "error", or it returns an error? Quote Link to comment Share on other sites More sharing options...
XeNoMoRpH1030 Posted June 24, 2010 Share Posted June 24, 2010 If I had to guess, it appears 3 extra characters are being added on (white space). I'd check your PHP. Quote Link to comment Share on other sites More sharing options...
shadiadiph Posted June 25, 2010 Author Share Posted June 25, 2010 I always trim everything and the word error is returned like this return "error"; has 5 characters even if i return nothing it says it's 3 in length it's weird. Quote Link to comment Share on other sites More sharing options...
brianlange Posted June 25, 2010 Share Posted June 25, 2010 have you considered using jquery for your ajax code. It'll simplify things. Quote Link to comment Share on other sites More sharing options...
shadiadiph Posted June 25, 2010 Author Share Posted June 25, 2010 I will look into it thanks Quote Link to comment Share on other sites More sharing options...
haku Posted June 26, 2010 Share Posted June 26, 2010 Are any of your documents encoded in a non-utf8 encoding? Is you javascript using another encoding? Quote Link to comment Share on other sites More sharing options...
shadiadiph Posted June 26, 2010 Author Share Posted June 26, 2010 I have all my docs using charset=ISO-8859-1" at the moment to read western state and city names my javascript at the moment is <script type="text/javascript" charset="ISO-8859-1" src="signup.js"></script> Quote Link to comment Share on other sites More sharing options...
shadiadiph Posted June 26, 2010 Author Share Posted June 26, 2010 right now here is my code the alert returns nostates when i select a country that has no states and when it has it returns the correct value but everytime it only executes the else statement regardless?? document.getElementById("tempresult").value=xmlHttp.responseText; var stateval; var stateval = document.getElementById("tempresult").value; alert('State Value' +stateval); if (stateval == "nostates") { document.getElementById('staterow').style.visibility='hidden'; document.getElementById('cityrow').style.visibility='hidden'; document.getElementById('postalrow').style.visibility='hidden'; getcitybycc(); } else { document.getElementById('staterow').style.visibility='visible'; document.getElementById('stateinput').innerHTML=xmlHttp.responseText; document.getElementById('cityrow').style.visibility='hidden'; document.getElementById('postalrow').style.visibility='hidden'; } I also get the same if i use document.getElementById('tempresult').innerHTML=xmlHttp.responseText; my php page if ($states==false) { $result = 'nostates'; $result = trim($result); return $result; exit; } i tried it with trim without trim still the same result Quote Link to comment Share on other sites More sharing options...
shadiadiph Posted June 27, 2010 Author Share Posted June 27, 2010 solved it at last for some unknown reason javascript was adding an unwanted white space so I am now using my javascript trim function and it works frustrating but fixed from now on i will trim everything. 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.