0

Hi I have never really bothered with javascript before but i am having alot of problems with a ajax php form my php code returns

if ($states==false)
{
$result = 'nostates';
$result = trim($result);
return $result;
exit;
}

And my javascript alert returns nostates but always executes the else statement when php returns nostates

document.getElementById("tempresult").innerHTML=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';
}

Any advice? this is driving me nuts :confused:

3
Contributors
10
Replies
11
Views
7 Years
Discussion Span
Last Post by shadiadiph
0

Assuming that you have already checked the cases, another possibility is whitespace.
Try an additional alert

alert('length' + stateval.length);

Edited by fxm: n/a

0

How can Javascript read what the PHP code has returned? PHP code returned is processed on server side and not on client side.

0

Good point about str .length I tried that already .length of nostates returns a value of 9?? thats why i trimmed it in php but i get the same result? I also tried changinging the return error to a number 69 and using the typeof doesn''t work could it be the new server I am using? It's really p@ss#ng me !ff

0

Trimming on the php side won't solve your problem.
I haven't tested yet, but after the assignment from .responseText, .innerHTML probably contains something like 'nostates\n'.

Edited by fxm: n/a

0

sorry i am more of a php programmer \n mmm i tied to trim(stateval); in javascript but it seemed to tell me stateval needed to be defined when it already had been. So how to trim(str) in javascript which to me seems illogical and was made by aliens.

0

The javascript string object has no trim() method.
If you are not using a framework which supplies one, you can either code one yourself or match in such a way that whitespace is irrelevant. In your situation this

if (/nostates/.test(stateval))

is arguably the smallest change to your existing code that will get the desired result.

0

So I am now going to argue that point with myself. :)
Depending on how the query is made, it may be possible to leave the comparison exactly as you originally wrote it and change the assignment instead

document.getElementById("tempresult").innerHTML=xmlHttp.responseHTML;
0

thanks fxm that worked great just wondering if it is a whitespace issue if so i might have to read up on trimming whitespaces and make my own trim() function.

Thanks alot

0

thanks for all the help fxm it was a whitespace issue trimmed it and now if(stateval=="nostates") works woohoo :)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.